using System.Windows.Controls;
using System.Windows.Input;
using Prism.Commands;
using Valg.EVA.Skanning.Services.InstallClientCertificate;

namespace Valg.EVA.Skanning.UserInterfaceElements.InstallClientCertificate
{
    public class InstallClientCertificateWindowViewModel : ViewModelBase
    {
        private readonly IInstallClientCertificateService installClientCertificateService;
        private string pathToClientCertificate;
        private string password;
        private string installationResultInformation;
        private bool? installationSuccess;
        private string windowIcon;

        public InstallClientCertificateWindowViewModel(IInstallClientCertificateService installClientCertificateService)
        {
            this.installClientCertificateService = installClientCertificateService;
            SelectClientCertificateCommand = new DelegateCommand(SelectClientCertificate);
            InstallClientCertificateCommand = new DelegateCommand(InstallClientCertificate);
        }

        public string PathToClientCertificate
        {
            get
            {
                return pathToClientCertificate;
            }
            private set
            {
                pathToClientCertificate = value;
                OnPropertyChanged();
                OnPropertyChanged(nameof(IsReadyToInstall));
            }
        }

        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
                OnPropertyChanged();
                OnPropertyChanged(nameof(IsReadyToInstall));
            }
        }

        public string InstallationResultInformation
        {
            get
            {
                return installationResultInformation;
            }
            set
            {
                installationResultInformation = value;
                OnPropertyChanged();
            }
        }

        public bool ShowFailedIcon => IsFalse(installationSuccess);
        public bool ShowSuccessIcon => IsTrue(installationSuccess);

        public ICommand InstallClientCertificateCommand { get; private set; }
        public ICommand SelectClientCertificateCommand { get; private set; }
        public bool IsReadyToInstall => !string.IsNullOrEmpty(PathToClientCertificate) && !string.IsNullOrEmpty(Password);
        public string WindowIcon
        {
            get
            {
                return windowIcon;
            }
            set
            {
                windowIcon = value;
                OnPropertyChanged();
            }
        }

        private void SelectClientCertificate()
        {
            PathToClientCertificate = installClientCertificateService.SelectClientCertificate();
        }

        private void InstallClientCertificate()
        {
            var result = installClientCertificateService.InstallClientCertificate(PathToClientCertificate, Password);
            if (result.Success)
            {
                InstallationResultInformation = Resources.Language.LanguageString.InstallClientCertificate_SuccessText;
                installationSuccess = true;
            }
            else
            {
                InstallationResultInformation = result.ErrorMessage;
                if (result.ErrorMessage != null && result.ErrorMessage.ToLower().Contains("the specified network password is not correct"))
                {
                    InstallationResultInformation = Resources.Language.LanguageString.InstallClientCertificate_WrongPasswordText;
                }
                
                installationSuccess = false;
            }
            OnPropertyChanged(nameof(ShowFailedIcon));
            OnPropertyChanged(nameof(ShowSuccessIcon));
        }

        private static bool IsTrue(bool? hasValue)
        {
            return hasValue.HasValue && hasValue.Value;
        }

        private static bool IsFalse(bool? hasValue)
        {
            return hasValue.HasValue && !hasValue.Value;
        }
    }
}
