using System.Windows.Input;
using Valg.EVA.Skanning.Identity;
using System;
using Valg.EVA.Skanning.Identity.Area;
using Valg.EVA.Skanning.UserInterfaceElements;
using Prism.Commands;
using Valg.EVA.Skanning.JobManagement.Startup.Data;

namespace Valg.EVA.Skanning.JobManagement.Startup.UI.Counting
{
    public class StartCountingPopupViewModel : ReactiveViewModel
    {
        private readonly JobManagementApplication jobManagementApplication;
        private readonly IIdentityProvider identityProvider;

        public StartCountingPopupViewModel(JobManagementApplication jobManagementApplication, IIdentityProvider identityProvider)
        {
            this.jobManagementApplication = jobManagementApplication;
            this.identityProvider = identityProvider;
            StartFinalCountCommand = new DelegateCommand(() => StartFinalCount());
            StartPreliminaryCountCommand = new DelegateCommand(() => StartPreliminaryCount());
            CloseCommand = new DelegateCommand(() => CloseWindowAction());
        }

        public Action CloseWindowAction { get; set; }

        public VotingCategoryType VotingCategoryType { get; set; }

        public PollingDistrictModel PollingDistrict { get; set; }

        public ICommand StartFinalCountCommand { get; set; }

        public ICommand StartPreliminaryCountCommand { get; set; }

        public bool IsStartPreliminaryCountButtonVisible
        {
            get
            {
                var countyArea = identityProvider.CurrentSelectedRole.CountyArea;
                return countyArea == null || countyArea.Level != AreaDescription.ElectionLevel.County;
            }
        }

        public ICommand CloseCommand { get; set; }

        private void StartFinalCount()
        {
            jobManagementApplication.StartFinalCounting(VotingCategoryType, PollingDistrict);
            CloseWindowAction();
        }

        private void StartPreliminaryCount()
        {
            jobManagementApplication.StartPreliminaryCounting(VotingCategoryType, PollingDistrict);
            CloseWindowAction();
        }
    }
}
