using Valg.EVA.Skanning.Contracts.Security;
using Valg.EVA.Skanning.ServicesModel.ElectionConfig.Model;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Prism.Commands;

namespace Valg.EVA.Skanning.UserInterfaceElements.EmergencyMode
{
    public class CurrentValuesSummaryViewModel : ViewModelBase, IConfigureEmergencyModeDetailModel
    {
        private string electionEventText;
        private string countingAsText;
        private County currentCounty;
        private bool isCountyVisible;
        private bool isMunicipalityVisible;
        private string nameOfOperator;
        
        public CurrentValuesSummaryViewModel()
        {
            CurrentMunicipalities = new ObservableCollection<MunicipalityViewModel>();
        }

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

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

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

        public string ElectionEventText
        {
            get { return electionEventText; }
            private set
            {
                electionEventText = value;
                OnPropertyChanged();
            }
        }

        public string CountingAsText
        {
            get { return countingAsText; }
            private set
            {
                countingAsText = value;
                OnPropertyChanged();
            }
        }

        public ElectionEvent CurrentElectionEvent { get; private set; }

        public CountingAs CurrentCountingAs { get; private set; }

        public bool IsCountyVisible
        {
            get { return isCountyVisible; }
            private set
            {
                isCountyVisible = value;
                OnPropertyChanged();
            }
        }

        public bool IsMunicipalityVisible
        {
            get { return isMunicipalityVisible; }
            private set
            {
                isMunicipalityVisible = value;
                OnPropertyChanged();
            }
        }

        public County CurrentCounty
        {
            get { return currentCounty; }
            private set
            {
                currentCounty = value;
                OnPropertyChanged();
            }
        }

        public ObservableCollection<MunicipalityViewModel> CurrentMunicipalities { get; private set; }

        public void SetCurrentElectionEvent(ElectionEvent electionEvent)
        {
            CurrentElectionEvent = electionEvent;
            if (CurrentElectionEvent != null)
            {
                ElectionEventText = Resources.Language.LanguageString.EmergencyMode_CurrentSelectedValuesView_ElectionEventText + " " + CurrentElectionEvent.Name;
            }
        }

        public void SetCurrentCountingAs(CountingAs countingAs)
        {
            CurrentCountingAs = countingAs;
            switch (countingAs)
            {
                case CountingAs.County:
                    CountingAsText = Resources.Language.LanguageString.EmergencyMode_CurrentSelectedValuesView_CountingAsCounty;
                    break;

                case CountingAs.Municipality:
                    CountingAsText = Resources.Language.LanguageString.EmergencyMode_CurrentSelectedValuesView_CountingAsMunicipality;
                    break;

                default:
                    CountingAsText = "";
                    break;
            }
        }

        internal void SetCurrentCounties(County county)
        {
            CurrentCounty = county;
            IsCountyVisible = county != null;
        }

        internal void SetCurrentMunicipalities(IEnumerable<MunicipalityViewModel> municipalities)
        {
            CurrentMunicipalities.Clear();
            CurrentMunicipalities.AddRange(municipalities);
            IsMunicipalityVisible = CurrentMunicipalities.Count > 0;
        }

        public bool IsNextButtonEnabled => !string.IsNullOrWhiteSpace(NameOfOperator) && NameOfOperator.Length > 0;
        public string NextButtonText => Resources.Language.LanguageString.EmergencyMode_CurrentValuesSummaryView_ActivateEmergencyModeButtonText;
        public bool IsNextButtonVisible => true;
    }
}
