using Valg.EVA.Skanning.ServicesModel.ElectionConfig.DataProvider;
using Valg.EVA.Skanning.ServicesModel.ElectionConfig.Model;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Prism.Commands;

namespace Valg.EVA.Skanning.UserInterfaceElements.EmergencyMode
{
    public class SelectMunicipalityViewModel : ViewModelBase, IConfigureEmergencyModeDetailModel
    {
        private readonly IMunicipalityDataProvider municipalityDataProvider;
        private bool isNextButtonEnabled;

        public SelectMunicipalityViewModel(IMunicipalityDataProvider municipalityDataProvider)
        {
            this.municipalityDataProvider = municipalityDataProvider;            
            Counties = new ObservableCollection<CountyWithMunicipalityViewModel>();

            CheckBoxIsCheckedCommand = new DelegateCommand(CheckBoxIsChecked);
        }

        public bool IsNextButtonVisible => true;

        public bool IsNextButtonEnabled
        {
            get { return isNextButtonEnabled; }
            set
            {
                if (isNextButtonEnabled != value)
                {
                    isNextButtonEnabled = value;
                    OnPropertyChanged();
                }
            }
        }

        public string NextButtonText => Resources.Language.LanguageString.Button_Next;

        public ElectionEvent CurrentElectionEvent { get; private set; }

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

        public ObservableCollection<CountyWithMunicipalityViewModel> Counties { get; }

        public DelegateCommand CheckBoxIsCheckedCommand { get; private set; }

        public void UpdateMunicipalityList()
        {
            if (CurrentElectionEvent != null)
            {
                Counties.Clear();

                var counties = municipalityDataProvider.GetAllCounties(CurrentElectionEvent.Id);
                foreach (var county in counties.OrderBy(p => p.Name))
                {
                    var municipalitiesInCounty = municipalityDataProvider.GetMunicipalitiesInCounty(CurrentElectionEvent.Id, county.Code);
                    var municipalityViewModels = MapToViewModel(municipalitiesInCounty);
                    var newViewModel = new CountyWithMunicipalityViewModel();
                    newViewModel.CurrentCounty = county;
                    newViewModel.MunicipalitiesInCounty.AddRange(municipalityViewModels);
                    Counties.Add(newViewModel);
                }
            }
        }

        private void CheckBoxIsChecked()
        {
            IsNextButtonEnabled = Counties.Any(p => p.MunicipalitiesInCounty.Any(q => q.IsChecked));
        }

        private IEnumerable<MunicipalityViewModel> MapToViewModel(List<Municipality> municipalitiesInCounty)
        {
            return municipalitiesInCounty.OrderBy(p => p.Name).Select(municipality => new MunicipalityViewModel(municipality));
        }

        public IEnumerable<MunicipalityViewModel> CurrentSelectedMunicipalities
        {
            get
            {
                var allMunicipalites = new List<MunicipalityViewModel>();
                Counties.Select(c => c.MunicipalitiesInCounty).ToList().ForEach(allMunicipalites.AddRange);
                return allMunicipalites.Where(p=> p.IsChecked);
            }
        }

        public void SetCurrentElectionEvent(ElectionEvent electionEvent)
        {
            CurrentElectionEvent = electionEvent;
        }
    }
}
