<commit_before>﻿using System;
using System.Windows.Forms;
using System.Windows.Threading;
using GitMind.Common.Tracking;
using GitMind.MainWindowViews;
using GitMind.Utils.UI;
using Application = System.Windows.Application;


namespace GitMind.Utils.Git.Private
{
	internal class CredentialService : ICredentialService
	{
		private readonly WindowOwner owner;


		public CredentialService(WindowOwner owner)
		{
			this.owner = owner;
		}


		public bool TryGetCredential(string url, string username, out IGitCredential gitCredential)
		{
			string message = $"Enter credentials for: {url}";

			// The key in Windows Credentials Store 
			string targetKey = $"GitMind:{url}";

			CredentialsDialog dialog = null;

			var dispatcher = GetApplicationDispatcher();
			if (dispatcher.CheckAccess())
			{

				dialog = ShowDialog(targetKey, username, message);
			}
			else
			{
				dispatcher.Invoke(() => dialog = ShowDialog(targetKey, username, message));
			}

			if (dialog != null)
			{
				gitCredential = new GitCredential(dialog);
				return true;
			}

			Log.Debug("Credentials dialog canceled");
			gitCredential = null;
			return false;
		}



		public void SetDialogConfirm(IGitCredential gitCredential, bool isConfirmed)
		{
			try
			{
				CredentialsDialog credentialsDialog = (gitCredential as GitCredential)?.Dialog;

				if (credentialsDialog == null)
				{
					Log.Debug("No dialog");
					return;
				}

				if (!isConfirmed)
				{
					// Provided credentials where not valid
					Track.Event("CredentialsDialog-Denied");
					credentialsDialog.Confirm(false);
					bool isDeleted = credentialsDialog.Delete();
					Log.Debug($"Deleted: {isDeleted}");
				}
				else if (credentialsDialog.SaveChecked)
				{
					// Provided credentials where valid and user want them to be cached
					Track.Event("CredentialsDialog-Confirmed");
					credentialsDialog.Confirm(true);
				}
				else
				{
					// User did not want valid credentials to be cached
					Track.Event("CredentialsDialog-NotCached");
				}
			}
			catch (ApplicationException e)
			{
				Log.Exception(e, "");
			}
		}



		private CredentialsDialog ShowDialog(string target, string username, string message)
		{
			CredentialsDialog dialog = new CredentialsDialog(target, "GitMind", message);
			dialog.SaveChecked = true;

			dialog.Name = username;

			// The credential dialog is only shown if there are no cached value for that user
			// The dialog contains a "save" check box, which when checked and credentials have been saved
			// will cache the credentials and thus skip showing the dialog the next time.
			// credentialDialog.AlwaysDisplay = true;
			if (dialog.Show(owner.Win32Window) == DialogResult.OK)
			{
				return dialog;
			}

			Log.Debug($"User canceled {target}, {username}, {message}");
			return null;
		}


		private static Dispatcher GetApplicationDispatcher() =>
			Application.Current?.Dispatcher ?? Dispatcher.CurrentDispatcher;
	}
}<commit_msg>Clean code<commit_after>﻿using System;
using System.Windows.Forms;
using GitMind.Common.Tracking;
using GitMind.MainWindowViews;
using GitMind.Utils.UI;


namespace GitMind.Utils.Git.Private
{
	internal class CredentialService : ICredentialService
	{
		private readonly WindowOwner owner;


		public CredentialService(WindowOwner owner)
		{
			this.owner = owner;
		}


		public bool TryGetCredential(string url, string username, out IGitCredential gitCredential)
		{
			string message = $"Enter credentials for: {url}";

			// The key in Windows Credentials Store 
			string targetKey = $"GitMind:{url}";

			CredentialsDialog dialog = null;

			UiThread.Run(() => dialog = ShowDialog(targetKey, username, message));

			if (dialog != null)
			{
				gitCredential = new GitCredential(dialog);
				return true;
			}

			Log.Debug("Credentials dialog canceled");
			gitCredential = null;
			return false;
		}



		public void SetDialogConfirm(IGitCredential gitCredential, bool isConfirmed)
		{
			try
			{
				CredentialsDialog credentialsDialog = (gitCredential as GitCredential)?.Dialog;

				if (credentialsDialog == null)
				{
					Log.Debug("No dialog");
					return;
				}

				if (!isConfirmed)
				{
					// Provided credentials where not valid
					Track.Event("CredentialsDialog-Denied");
					credentialsDialog.Confirm(false);
					bool isDeleted = credentialsDialog.Delete();
					Log.Debug($"Deleted: {isDeleted}");
				}
				else if (credentialsDialog.SaveChecked)
				{
					// Provided credentials where valid and user want them to be cached
					Track.Event("CredentialsDialog-Confirmed");
					credentialsDialog.Confirm(true);
				}
				else
				{
					// User did not want valid credentials to be cached
					Track.Event("CredentialsDialog-NotCached");
				}
			}
			catch (ApplicationException e)
			{
				Log.Exception(e, "");
			}
		}



		private CredentialsDialog ShowDialog(string target, string username, string message)
		{
			CredentialsDialog dialog = new CredentialsDialog(target, "GitMind", message);
			dialog.SaveChecked = true;

			dialog.Name = username;

			// The credential dialog is only shown if there are no cached value for that user
			// The dialog contains a "save" check box, which when checked and credentials have been saved
			// will cache the credentials and thus skip showing the dialog the next time.
			// credentialDialog.AlwaysDisplay = true;
			if (dialog.Show(owner.Win32Window) == DialogResult.OK)
			{
				return dialog;
			}

			Log.Debug($"User canceled {target}, {username}, {message}");
			return null;
		}
	}
}
{'fraction_non_alphanumeric': 0.09812070834839176, 'fraction_numerical': 0.0007228044813877846, 'mean_word_length': 3.4493569131832795, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2226756', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1558, 'n_words': 442}