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

        public UnspecificReadsoftErrorViewModel()
        {
            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 UnspecifiedErrorOccuredWhileScanning
        {
            get { return Valg.EVA.Skanning.Resources.Language.ScanLanguageString.AnUnspecifiedErrorOccuredWhileScanning; }
        }
        public string DeleteAllBallotsInBoxAndRestart
        {
            get { return Valg.EVA.Skanning.Resources.Language.ScanLanguageString.DeleteAllScannedBallotsFromBoxAndScanAgain; }
        }
        public string RestartAfterLastScannedBallot
        {
            get
            {
                if (HasScannedBallots)
                {
                    return Valg.EVA.Skanning.Resources.Language.ScanLanguageString.RestartAfterLastScannedBallot;
                }
                
                return Valg.EVA.Skanning.Resources.Language.ScanLanguageString.RestartScanning;
            }
        }
        public string DeleteAllBallotsInBoxAndCancel
        {
            get
            {
                if (HasScannedBallots)
                {
                    return Valg.EVA.Skanning.Resources.Language.ScanLanguageString.DeleteAllScannedBallotsFromBoxAndCancel;
                }

                return Valg.EVA.Skanning.Resources.Language.LanguageString.Button_Cancel;
            }
        }
        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 void SetDefaultFocus()
        {
            DelayedFocusSetter.SetFocus(() => IsRestartAfterLastScannedBallotButtonFocused = true);
        }

        public bool HasScannedBallots
        {
            get { return hasScannedBallots; }
        }

        public bool? IsRestartAfterLastScannedBallotButtonFocused
        {
            get { return isRestartAfterLastScannedBallotButtonFocused; }
            set
            {
                isRestartAfterLastScannedBallotButtonFocused = value;
                OnPropertyChanged();
            }
        }

        public void Initialize(bool scannedBallots)
        {
            hasScannedBallots = scannedBallots;
        }
    }
}
