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 PaperJamViewModel : ViewModelBase, INotificationDuringScanningViewModel
    {
        private readonly ICommand restartAfterLastScannedBallotCommand;
        private readonly ICommand deleteAllAndRestartCommand;
        private readonly ICommand deleteAllAndCancelCommand;
        private bool errorIsResolved;
        private bool isRestartAfterLastScannedBallotButtonFocused;

        public PaperJamViewModel()
        {
            restartAfterLastScannedBallotCommand = new DelegateCommand(RestartAfterLast);
            deleteAllAndRestartCommand = new DelegateCommand(DeleteAndRestart);
            deleteAllAndCancelCommand = new DelegateCommand(DeleteAndCancel);
        }

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

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

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

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

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

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

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

        public string DeleteAllBallotsInBoxAndRestart
        {
            get { return Valg.EVA.Skanning.Resources.Language.ScanLanguageString.DeleteAllScannedBallotsFromBoxAndScanAgain; }
        }
        public string DeleteAllBallotsIsBoxAndCancel
        {
            get { return Valg.EVA.Skanning.Resources.Language.ScanLanguageString.DeleteAllScannedBallotsFromBoxAndCancel; }
        }
        public ICommand RestartAfterLastScannedBallotCommand
        {
            get { return restartAfterLastScannedBallotCommand; }
        }
        public ICommand DeleteAllAndRestartCommand
        {
            get { return deleteAllAndRestartCommand; }
        }
        public ICommand DeleteAllAndCancelCommand
        {
            get { return deleteAllAndCancelCommand; }
        }
        public bool ErrorIsResolved
        {
            get { return errorIsResolved; }
            private set
            {
                errorIsResolved = value;
                OnPropertyChanged();
            }
        }
        public bool IsRestartAfterLastScannedBallotButtonFocused
        {
            get { return isRestartAfterLastScannedBallotButtonFocused; }
            set
            {
                isRestartAfterLastScannedBallotButtonFocused = value;
                OnPropertyChanged();
            }
        }

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