using System;
using System.Runtime.CompilerServices;
using System.Windows.Data;
using System.Windows.Input;
using Valg.EVA.Skanning.ContainerExtensions;
using Valg.EVA.Skanning.UserInterfaceElements;
using Valg.EVA.Skanning.UserInterfaceElements.LightBox;
using Prism.Commands;
using Valg.EVA.Skanning.Identity;
using Valg.EVA.Skanning.JobManagement.Startup.Data;
using Valg.EVA.Skanning.UserInterfaceElements.Language;
using Valg.EVA.Skanning.Identity.Area;
using Valg.EVA.Skanning.Resources.Language;

namespace Valg.EVA.Skanning.JobManagement.Startup.UI.Counting
{
    public class StartCountingButtonViewModel : ReactiveViewModel, IVotingCategoryAwareViewModel, IPollingDistrictAwareViewModel
    {
        private readonly ITypedFactory typedFactory;
        private IIdentityProvider _identityProvider;

        public StartCountingButtonViewModel(ITypedFactory typedFactory, IIdentityProvider identityProvider)
        {
            this.typedFactory = typedFactory;
            _identityProvider = identityProvider;
            OpenDialogCommand = new DelegateCommand(OpenDialog);
            StartFinalCountingCommand = new DelegateCommand(StartFinalCounting);
        }

        public ICommand OpenDialogCommand { get; set; }
        public ICommand StartFinalCountingCommand { get; set; }
        public VotingCategoryType ActiveVotingCategory { get; set; }
        public bool HasOnlyOnePollingDistrict { get; set; }
        public string StartCountingButtonText
        {
            get
            {
                var countyArea = _identityProvider?.CurrentSelectedRole?.CountyArea;
                if (countyArea?.Level == AreaDescription.ElectionLevel.County)
                {
                    return JobManagementLanguageString.StartCounting_StartControlCounting_ButtonText;
                }
                return JobManagementLanguageString.StartCounting_StartFinalCounting_ButtonText;
            }
        }

        public PollingDistrictModel ActivePollingDistrict { get; set; }

        private void OpenDialog()
        {
            var viewModel = typedFactory.Create<StartCountingPopupViewModel>();
            viewModel.VotingCategoryType = ActiveVotingCategory;
            viewModel.PollingDistrict = ActivePollingDistrict;
            PublishEvent<ShowLightBoxEvent, LightBoxEventArgs>(new LightBoxEventArgs(typeof(StartCountingPopupView), viewModel));
        }

        private void StartFinalCounting()
        {
            var jobManagementApplication = typedFactory.Create<JobManagementApplication>();
            jobManagementApplication.StartFinalCounting(ActiveVotingCategory, ActivePollingDistrict);
        }

        protected override void SubscribeToViewModelSpecificEvents()
        {
            base.SubscribeToViewModelSpecificEvents();
            
        }
    }
}
