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 NoPaperInFeederViewModel : ViewModelBase, INotificationDuringScanningViewModel
    {
        private readonly ICommand startScanningCommand;
        private readonly ICommand deleteAndCancelCommand;
        private bool errorIsResolved;
        private bool? isStartScanningButtonFocused;
        private bool scanMoreBallotsWasRequested;

        public NoPaperInFeederViewModel()
        {
            startScanningCommand = new DelegateCommand(RequestStartScanning);
            deleteAndCancelCommand = new DelegateCommand(DeleteAndRestart);
        }

        private void DeleteAndRestart()
        {
            ErrorIsResolved = true;
            PublishEvent<DeleteAllInBoxAndScanAgainEvent, DeleteAllInBoxAndScanAgainEventArgs>(new DeleteAllInBoxAndScanAgainEventArgs(false));
        }

        private void RequestStartScanning()
        {
            ErrorIsResolved = true;
            PublishEvent<ScanJobRequestedStartedEvent, ScanJobRequestedStartedEventArgs>(new ScanJobRequestedStartedEventArgs());
        }

        public string NoPaperInScanner
        {
            get { return Valg.EVA.Skanning.Resources.Language.ScanLanguageString.NoPaper; }
        }
        public string PleasePutBallotsInTheInFeeder
        {
            get
            {
                return scanMoreBallotsWasRequested ? Valg.EVA.Skanning.Resources.Language.ScanLanguageString.Label_NoPaperWhenScanningMoreBallots : Valg.EVA.Skanning.Resources.Language.ScanLanguageString.Label_NoPaperWhenScanning;
            }
        }
        public string StartScanningAgain
        {
            get
            {
                return scanMoreBallotsWasRequested ? Valg.EVA.Skanning.Resources.Language.ScanLanguageString.Button_ContinueScanning : Valg.EVA.Skanning.Resources.Language.ScanLanguageString.Button_StartScanning;
            }
        }
        public string DeleteAllBallotsInBoxAndCancel
        {
            get
            {
                return scanMoreBallotsWasRequested ? Valg.EVA.Skanning.Resources.Language.ScanLanguageString.Button_DeleteScan : Valg.EVA.Skanning.Resources.Language.LanguageString.Button_Cancel;
            }
        }
        public ICommand StartScanningCommand
        {
            get { return startScanningCommand; }
        }
        public ICommand DeleteAllBallotsAndCancelCommand
        {
            get { return deleteAndCancelCommand; }
        }

        public bool ErrorIsResolved
        {
            get { return errorIsResolved; }
            private set
            {
                errorIsResolved = value;
                OnPropertyChanged();
            }
        }
        public bool? IsStartScanningButtonFocused
        {
            get { return isStartScanningButtonFocused; }
            set
            {
                isStartScanningButtonFocused = value;
                OnPropertyChanged();
            }
        }
        public bool ScanMoreBallotsWasRequested
        {
            get { return scanMoreBallotsWasRequested; }
            private set
            {
                scanMoreBallotsWasRequested = value;
                OnPropertyChanged();
            }
        }

        public void SetDefaultFocus()
        {
            DelayedFocusSetter.SetFocus(() => IsStartScanningButtonFocused = true);            
        }

        public void Initialize(bool moreBallotsWasRequested)
        {
            scanMoreBallotsWasRequested = moreBallotsWasRequested;
        }
    }
}
