using System.Linq;
using System.Security;
using Valg.EVA.Skanning.Identity;
using Valg.EVA.Skanning.Services.SystemServices;
using System.Windows;
using Valg.EVA.Skanning.Services.EmergencyMode;
using Prism.Commands;
using Valg.EVA.Skanning.UserInterfaceElements.Login;

namespace Valg.EVA.Skanning.UserInterfaceElements.EmergencyMode
{
    public class EmergencyModeLoginViewModel : ViewModelBase, IEmergencyModeLoginViewModel
    {
        private string nameOfOperator;
        private readonly IClaimsAndRolesBuilder claimsAndRolesBuilder;
        private readonly IEmergencyModeService emergencyModeService;
        private readonly IIdentityProvider identityProvider;
        private readonly ISystemService systemService;
        private string windowIcon;

        public EmergencyModeLoginViewModel(IClaimsAndRolesBuilder claimsAndRolesBuilder, IEmergencyModeService emergencyModeService, 
            IIdentityProvider identityProvider, ISystemService systemService)
        {
            this.claimsAndRolesBuilder = claimsAndRolesBuilder;
            this.emergencyModeService = emergencyModeService;
            this.identityProvider = identityProvider;
            this.systemService = systemService;
            LogonCommand = new DelegateCommand<Window>(Logon);
        }

        private void Logon(Window window)
        {
            if (window != null)
            {
                var token = emergencyModeService.GetActiveToken();
                token = InsertUsername(token);
                claimsAndRolesBuilder.ParseToken(System.Text.Encoding.UTF8.GetBytes(token), null);

                if (identityProvider.Roles.Count > 0)
                {
                    systemService.InitElectionConfigDataCache(identityProvider.Roles.First().Area.ElectionEventId);
                }

                Result = LoginWindowLoginResult.LoginFromEmergencyMode;
                window.Close();
            }
        }

        public LoginWindowLoginResult Result { get; private set; }
        public string WindowIcon
        {
            get
            {
                return windowIcon;
            }
            set
            {
                windowIcon = value;
                OnPropertyChanged();
            }
        }

        public string NameOfOperator
        {
            get { return nameOfOperator; }
            set
            {
                nameOfOperator = value;
                OnPropertyChanged();
                OnPropertyChanged(nameof(IsLogonButtonEnabled));
            }
        }

        public DelegateCommand<Window> LogonCommand { get; private set; }

        public string Title => Resources.Language.LanguageString.EmergencyMode_LogonWindow_Title;

        public string YourNameLabel => Resources.Language.LanguageString.EmergencyMode_LogonWindow_YourNameLabel;

        public string LogonButtonText => Resources.Language.LanguageString.EmergencyMode_LogonWindow_LogonButtonText;

        public string Description1 => Resources.Language.LanguageString.EmergencyMode_LogonWindow_Description1;

        public string Description2 => Resources.Language.LanguageString.EmergencyMode_LogonWindow_Description2;

        public bool IsLogonButtonEnabled => !string.IsNullOrWhiteSpace(NameOfOperator) && NameOfOperator.Length > 4;

        private string InsertUsername(string token)
        {
            var escapedNameOfOperator = SecurityElement.Escape(NameOfOperator);
            var newToken = token.Replace("[operatorName]", escapedNameOfOperator);
            return newToken;
        }
    }
}
