using System;
using System.Windows.Input;
using Valg.EVA.Skanning.Scan.ReadSoftContracts.Events;
using Prism.Commands;
using Valg.EVA.Skanning.UserInterfaceElements;

namespace Valg.Eva.Skanning.Scan.Presentation.ScanInProgress
{
    public class BatchIsCompleteViewModel : ViewModelBase
    {
        private readonly ICommand scanMoreBallotsFromSameBoxCommand;
        private readonly ICommand deleteAllScannedBallotsFromBoxAndScanAgainCommand;
        private readonly ICommand allBallotsInBoxAreScannedCommand;
        private bool? isScanMoreBallotsButtonFocused;
        private int numberOfBallotsScanned;

        public BatchIsCompleteViewModel()
        {
            scanMoreBallotsFromSameBoxCommand = new DelegateCommand(RequestStartScanning);
            deleteAllScannedBallotsFromBoxAndScanAgainCommand = new DelegateCommand(DeleteAllAndScanAgain);
            allBallotsInBoxAreScannedCommand = new DelegateCommand(AllBallotPapersAreScanned);
        }

        private void AllBallotPapersAreScanned()
        {
            PublishEvent<AllBallotPapersAreScannedEvent,AllBallotPapersAreScannedEventArgs>(new AllBallotPapersAreScannedEventArgs());
        }

        private void DeleteAllAndScanAgain()
        {
            PublishEvent<DeleteAllInBoxAndScanAgainEvent, DeleteAllInBoxAndScanAgainEventArgs>(new DeleteAllInBoxAndScanAgainEventArgs(true));
        }

        private void RequestStartScanning()
        {
            
            PublishEvent<ScanMoreBallotsWasRequestedEvent, EventArgs>(EventArgs.Empty);
            PublishEvent<ScanJobRequestedStartedEvent, ScanJobRequestedStartedEventArgs>(new ScanJobRequestedStartedEventArgs());
        }

        public string BatchIsCompleteText
        {
            get { return Valg.EVA.Skanning.Resources.Language.ScanLanguageString.BatchIsComplete; }
        }

        public string NumberOfBallotsSoFar
        {
            get { return string.Format("{0}: {1}", Valg.EVA.Skanning.Resources.Language.ScanLanguageString.NumberOfBallotsSoFar, NumberOfBallotsScanned); }
        }

        public int NumberOfBallotsScanned
        {
            get { return numberOfBallotsScanned; }
            set
            {
                numberOfBallotsScanned = value;
                OnPropertyChanged(PropertyHelper.GetName<BatchIsCompleteViewModel>(m => m.NumberOfBallotsSoFar));                
            }
        }

        public string ScanMoreBallotsFromSameBoxText
        {
            get { return Valg.EVA.Skanning.Resources.Language.ScanLanguageString.ScanMoreBallotsFromSameBox; }
        }
        public string AllBallotsInThisBoxAreScannedText
        {
            get { return Valg.EVA.Skanning.Resources.Language.ScanLanguageString.AllBallotsInThisBoxAreScanned; }
        }
        public string DeleteAllScannedBallotsFromBoxAndScanAgainText
        {
            get { return Valg.EVA.Skanning.Resources.Language.ScanLanguageString.DeleteAllScannedBallotsFromBoxAndScanAgain; }
        }
        public ICommand ScanMoreBallotsFromSameBoxCommand
        {
            get { return scanMoreBallotsFromSameBoxCommand; }
        }
        public ICommand DeleteAllScannedBallotsFromBoxAndScanAgainCommand
        {
            get { return deleteAllScannedBallotsFromBoxAndScanAgainCommand; }
        }
        public ICommand AllBallotsInBoxAreScannedCommand
        {
            get { return allBallotsInBoxAreScannedCommand; }
        }

        public bool? IsScanMoreBallotsButtonFocused
        {
            get { return isScanMoreBallotsButtonFocused; }
            set
            {
                isScanMoreBallotsButtonFocused = value;
                OnPropertyChanged();
            }
        }

        public void PrepareForShow()
        {
            DelayedFocusSetter.SetFocus(() => IsScanMoreBallotsButtonFocused = true);
        }
    }
}
