using Valg.EVA.Skanning.Contracts.Security;
using Valg.EVA.Skanning.ServicesModel.ServiceContracts.EmergencyMode.Write;
using Valg.EVA.Skanning.Services.EmergencyMode;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Prism.Commands;

namespace Valg.EVA.Skanning.UserInterfaceElements.EmergencyMode
{
    public class EmergencyModeConfigurationWindowViewModel : ViewModelBase
    {
        private readonly IEmergencyModeService emergencyModeService;
        private IConfigureEmergencyModeDetailModel currentDetailViewModel;
        private string windowIcon;

        public EmergencyModeConfigurationWindowViewModel(IEmergencyModeService emergencyModeService,
            SelectElectionEventViewModel selectElectionEventView,
            SelectCountingAsViewModel selectCountingAsViewModel,
            SelectCountyViewModel selectCountyViewModel,
            SelectMunicipalityViewModel selectMunicipalityViewModel,
            CurrentValuesSummaryViewModel currentValuesSummaryViewModel,
            ErrorViewModel errorViewModel)
        {
            this.emergencyModeService = emergencyModeService;

            AdvanceToNextStep = new DelegateCommand(AdvanceToNextStepHandler);

            
            SelectElectionEventViewModel = selectElectionEventView;
            ConfigureDetailsViewModels.Add(SelectElectionEventViewModel);

            SelectCountingAsViewModel = selectCountingAsViewModel;
            ConfigureDetailsViewModels.Add(SelectCountingAsViewModel);

            SelectCountyViewModel = selectCountyViewModel;
            ConfigureDetailsViewModels.Add(SelectCountyViewModel);

            SelectMunicipalityViewModel = selectMunicipalityViewModel;
            ConfigureDetailsViewModels.Add(SelectMunicipalityViewModel);

            CurrentValuesSummaryViewModel = currentValuesSummaryViewModel;
            ConfigureDetailsViewModels.Add(CurrentValuesSummaryViewModel);

            ErrorViewModel = errorViewModel;
            ConfigureDetailsViewModels.Add(ErrorViewModel);

            if (HasSeveralElectionEvent)
            {
                CurrentDetailViewModel = SelectElectionEventViewModel;
            }
            else
            {
                CurrentDetailViewModel = SelectCountingAsViewModel;
            }
        }

        private void AdvanceToNextStepHandler()
        {
            if (CurrentDetailViewModel == CurrentValuesSummaryViewModel)
            {
                ActivateEmergencyMode();
            }
            else
            {
                UpdateCurrentValuesOnViewModels();

                var skipIncrement = 1;
                if (CurrentDetailViewModel == SelectCountingAsViewModel)
                {
                    if (SelectCountingAsViewModel.CurrentCountingAs == CountingAs.County)
                    {
                        SelectCountyViewModel.UpdateCountyList();
                    }
                    else if (SelectCountingAsViewModel.CurrentCountingAs == CountingAs.Municipality)
                    {
                        SelectMunicipalityViewModel.UpdateMunicipalityList();
                        skipIncrement = 2;
                    }
                }
                else if (CurrentDetailViewModel == SelectCountyViewModel)
                {
                    skipIncrement = 2;
                }

                var currentIndex = ConfigureDetailsViewModels.IndexOf(CurrentDetailViewModel);
                CurrentDetailViewModel = ConfigureDetailsViewModels[currentIndex + skipIncrement];
            }
        }

        public ErrorViewModel ErrorViewModel { get; }

        public SelectElectionEventViewModel SelectElectionEventViewModel { get; }

        public SelectCountingAsViewModel SelectCountingAsViewModel { get; }

        public SelectCountyViewModel SelectCountyViewModel { get; }

        public SelectMunicipalityViewModel SelectMunicipalityViewModel { get; }

        public CurrentValuesSummaryViewModel CurrentValuesSummaryViewModel { get; }

        public Action CloseWindow { get; set; }

        private bool HasSeveralElectionEvent => SelectElectionEventViewModel.ElectionEvents.Count > 1;

        public ObservableCollection<IConfigureEmergencyModeDetailModel> ConfigureDetailsViewModels { get; } = new ObservableCollection<IConfigureEmergencyModeDetailModel>();

        public IConfigureEmergencyModeDetailModel CurrentDetailViewModel
        {
            get { return currentDetailViewModel; }
            set
            {
                currentDetailViewModel = value;
                OnPropertyChanged();
            }
        }

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

        private void ActivateEmergencyMode()
        {
            var createTokenParams = new CreateEmergencyModeTokenParams
            {
                ElectionEvent = SelectElectionEventViewModel.CurrentElectionEvent,
                CountingAs = SelectCountingAsViewModel.CurrentCountingAs,
                CreatedBy = CurrentValuesSummaryViewModel.NameOfOperator
            };

            if (createTokenParams.CountingAs == CountingAs.County)
            {
                createTokenParams.SelectedCounty = SelectCountyViewModel.CurrentSelectedCounty;
            }
            else if (createTokenParams.CountingAs == CountingAs.Municipality)
            {
                createTokenParams.SelectedMunicipalities = SelectMunicipalityViewModel.CurrentSelectedMunicipalities.Select(p => p.Municipality).ToList();
            }

            var result = emergencyModeService.CreateEmergencyTokenAndActivate(createTokenParams);

            if (result.WasSuccessful)
            {
                CloseWindow();
            }
            else
            {
                CurrentDetailViewModel = ErrorViewModel;
            }
        }

        private void UpdateCurrentValuesOnViewModels()
        {
            SelectCountyViewModel.SetCurrentElectionEvent(SelectElectionEventViewModel.CurrentElectionEvent);
            SelectMunicipalityViewModel.SetCurrentElectionEvent(SelectElectionEventViewModel.CurrentElectionEvent);
            CurrentValuesSummaryViewModel.SetCurrentElectionEvent(SelectElectionEventViewModel.CurrentElectionEvent);
            CurrentValuesSummaryViewModel.SetCurrentCountingAs(SelectCountingAsViewModel.CurrentCountingAs);
            CurrentValuesSummaryViewModel.SetCurrentCounties(SelectCountyViewModel.CurrentSelectedCounty);
            CurrentValuesSummaryViewModel.SetCurrentMunicipalities(SelectMunicipalityViewModel.CurrentSelectedMunicipalities);
        }
    }
}
