using System.ComponentModel;
using System.Linq;
using System.Reflection;
using Valg.EVA.Skanning.Services.EmergencyMode;
using System.Windows;
using System.Windows.Input;
using Prism.Commands;
using Valg.EVA.Skanning.ContainerExtensions;
using Valg.EVA.Skanning.UserInterfaceElements.ApplicationTermination;
using Valg.EVA.Skanning.UserInterfaceElements.EmergencyMode;
using Valg.EVA.Skanning.UserInterfaceElements.InstallClientCertificate;
using Valg.EVA.Skanning.UserInterfaceElements.LightBox;
using Valg.EVA.Skanning.UserInterfaceElements.Login;

namespace Valg.EVA.Skanning.UserInterfaceElements.Welcome
{
    public class WelcomeViewModel : ViewModelBase
    {
        private readonly ITypedFactory typedFactory;
        private bool showEmergencyModeConfiguration;
        private bool showEmergencyModeLogin;
        private string windowIcon;

        public WelcomeViewModel(IEmergencyModeService emergencyModeService, ITypedFactory typedFactory)
        {
            this.typedFactory = typedFactory;
            Quit = new DelegateCommand(Cancel);
            StartEmergencyModeConfigurationCommand = new DelegateCommand<Window>(StartEmergencyModeConfiguration);
            StartEmergencyModeLoginCommand = new DelegateCommand<Window>(StartEmergencyLogin);
            StartInstallClientCertificateCommand = new DelegateCommand<Window>(StartInstallClientCertificate);
            StartIdPortenLoginCommand = new DelegateCommand<Window>(StartIdPortenLogin);

            ShowEmergencyModeLogin = emergencyModeService.IsEmergencyModeActive();


            var assembly = GetType().Assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute)).FirstOrDefault();
            var asDescriptionAttribute = assembly as AssemblyDescriptionAttribute;
            if (asDescriptionAttribute != null)
            {
                if (asDescriptionAttribute.Description.Contains("prerelease: true"))
                {
                    WelcomeImage = "pack://application:,,,/Valg.EVA.Skanning.Assets;component/Images/eva_logo_prerelease.png";
                    OnPropertyChanged(nameof(WelcomeImage));
                }
            }

        }

        public string WelcomeImage { get; set; } = "pack://application:,,,/Valg.EVA.Skanning.Assets;component/Images/eva_logo.png";

        public ICommand Quit { get; private set; }

        public ICommand StartEmergencyModeConfigurationCommand { get; private set; }

        public ICommand StartEmergencyModeLoginCommand { get; private set; }

        public ICommand StartInstallClientCertificateCommand { get; private set; }

        public ICommand StartIdPortenLoginCommand { get; private set; }

        public WelcomeResult Result { get; private set; }

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

        public bool ShowEmergencyModeConfiguration
        {
            get { return showEmergencyModeConfiguration; }
            set
            {
                showEmergencyModeConfiguration = value;
                OnPropertyChanged();
            }
        }

        public bool ShowEmergencyModeLogin
        {
            get { return showEmergencyModeLogin; }
            set
            {
                showEmergencyModeLogin = value;
                OnPropertyChanged();
            }
        }

        private void StartIdPortenLogin(Window window)
        {
            if (window != null)
            {
                Result = WelcomeResult.ShowIdPortenLogin;
                window.Close();
            }
        }

        private void StartEmergencyLogin(Window window)
        {
            if (window != null)
            {
                window.Close();

                var loginWindow = typedFactory.Create<EmergencyModeLoginWindow>();
                loginWindow.ViewModel.WindowIcon = WindowIcon;
                PublishEvent<ShowLightBoxEvent, LightBoxEventArgs>(new LightBoxEventArgs(loginWindow));

                Result = loginWindow.Result == LoginWindowLoginResult.LoginFromEmergencyMode 
                    ? WelcomeResult.EmergencyModeLoginCompleted : WelcomeResult.EmergencyModeLoginCancelled;
            }
        }

        private void StartEmergencyModeConfiguration(Window window)
        {
            if (window != null)
            {
                window.Close();

                var emergencyModeWindow = typedFactory.Create<EmergencyModeConfigurationWindow>();
                emergencyModeWindow.ViewModel.WindowIcon = WindowIcon;
                PublishEvent<ShowLightBoxEvent, LightBoxEventArgs>(new LightBoxEventArgs(emergencyModeWindow));

                Result = WelcomeResult.ShowEmergencyModeConfiguration;
            }
        }

        private void StartInstallClientCertificate(Window window)
        {
            if (window != null)
            {
                window.Close();

                var clientCertificateWindow = typedFactory.Create<InstallClientCertificateWindow>();
                clientCertificateWindow.ViewModel.WindowIcon = WindowIcon;
                PublishEvent<ShowLightBoxEvent, LightBoxEventArgs>(new LightBoxEventArgs(clientCertificateWindow));

                Result =  WelcomeResult.ShowInstallClientCertificate;
            }
        }

        private void Cancel()
        {
            Result = WelcomeResult.Cancelled;
            PublishEvent<ApplicationTerminateRequestedEvent, System.EventArgs>(System.EventArgs.Empty);
        }
    }
}
