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

namespace Valg.EVA.Skanning.UserInterfaceElements.EmergencyMode
{
    public class SelectElectionEventViewModel : ViewModelBase, IConfigureEmergencyModeDetailModel
    {
        private bool isNextButtonEnabled;

        public SelectElectionEventViewModel(IElectionConfigDataProvider electionConfigDataProvider)
        {
            ElectionEvents = electionConfigDataProvider.GetAllElectionEvents();
            RadioButtonSelectedCommand = new DelegateCommand<ElectionEvent>(RadioButtonSelected);

            SetValuesIsPossible();
        }

        public bool IsNextButtonVisible => true;

        public DelegateCommand<ElectionEvent> RadioButtonSelectedCommand { get; private set; }

        public List<ElectionEvent> ElectionEvents { get; }

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

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

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

        public ElectionEvent CurrentElectionEvent { get; private set; }

        private void SetValuesIsPossible()
        {
            if (ElectionEvents != null && ElectionEvents.Count == 1)
            {
                CurrentElectionEvent = ElectionEvents.First();
            }
        }

        private void RadioButtonSelected(ElectionEvent selectedElectionEvent)
        {
            CurrentElectionEvent = selectedElectionEvent;
            IsNextButtonEnabled = true;
        }
    }
}
