using Valg.EVA.Skanning.Contracts.CommonEnums;
using System;
using Prism.Commands;

namespace Valg.EVA.Skanning.JobManagement.Startup.UI.Counting
{
    public class ClearBoxPopupViewModel : ReactiveViewModel
    {
        private readonly IJobManagementApplication jobManagementApplication;

        public ClearBoxPopupViewModel(IJobManagementApplication jobManagementApplication)
        {
            this.jobManagementApplication = jobManagementApplication;

            ClearContentCommand = new DelegateCommand(ClearContentHandler);
            ResolveLockedBallotsCommand = new DelegateCommand(ResolveLockedBallots);
            SetBoxAsEmptyCommand = new DelegateCommand(SetBoxAsEmpty);
        }

        public void Initialize(string boxName, Data.BoxCountingDetailsModel boxCountingDetailsModel)
        {
            BoxName = boxName;
            BoxCountingDetailsModel = boxCountingDetailsModel;
            NumberOfLockedBallots = jobManagementApplication.GetLockedBallotsForBoxCounting(boxCountingDetailsModel.Id);
        }

        public int NumberOfLockedBallots { get; private set; }
        public bool ShowResolveLockedBallotsLink
        {
            get { return NumberOfLockedBallots > 0; }
        }
        public string ResolveLockedBallotsText
        {
            get { return string.Format(Valg.EVA.Skanning.Resources.Language.JobManagementLanguageString.ClearBoxPopup_ResolveLockedBallotsText, NumberOfLockedBallots); }
        }

        public Data.BoxCountingDetailsModel BoxCountingDetailsModel { get; private set; }

        public string BoxName { get; private set; }

        public DelegateCommand ClearContentCommand { get; private set; }

        public DelegateCommand ResolveLockedBallotsCommand { get; private set; }

        public DelegateCommand SetBoxAsEmptyCommand { get; private set; }

        public Action CloseWindowAction { get; set; }

        public string Title
        {
            get { return BoxName; }
        }

        private void ClearContentHandler()
        {
            jobManagementApplication.ClearContentOfBoxCounting(BoxCountingDetailsModel);
            CloseWindowAction();
        }

        private void ResolveLockedBallots()
        {
            jobManagementApplication.ResolveLockedBallots(BoxCountingDetailsModel);
            CloseWindowAction();
        }

        private void SetBoxAsEmpty()
        {
            jobManagementApplication.SetBoxAsEmpty(BoxCountingDetailsModel);
            CloseWindowAction();
        }

        public bool CanRegisterEmptyBox
        {
            get
            {
                return BoxCountingDetailsModel.BoxCountingState == BoxCountingState.RegisteredNotScanned || BoxCountingDetailsModel.BoxCountingState == BoxCountingState.ReadyForScan;
            }
        }
    }
}
