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

namespace Valg.Eva.Skanning.Scan.Presentation.ScanInProgress
{
    public class SaveDataFailedViewModel : ViewModelBase, INotificationDuringScanningViewModel
    {
        private bool errorIsResolved;
        private BoxScanningSession boxScanningSession;
        private readonly DatabaseQuerier databaseQuerier;
        private bool isRestartAfterLastScannedBallotButtonFocused;

        public SaveDataFailedViewModel(DatabaseQuerier databaseQuerier)
        {
            this.databaseQuerier = databaseQuerier;

            RestartAfterLastScannedBallotCommand = new DelegateCommand(RestartAfterLast);
            DeleteAllAndRestartCommand = new DelegateCommand(DeleteAndRestart);
            DeleteAllAndCancelCommand = new DelegateCommand(DeleteAndCancel);
        }

        public void Initialize(BoxScanningSession boxSession)
        {
            boxScanningSession = boxSession;
        }

        public ICommand RestartAfterLastScannedBallotCommand { get; private set; }

        public ICommand DeleteAllAndRestartCommand { get; private set; }

        public ICommand DeleteAllAndCancelCommand { get; private set; }

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

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

        private async void DeleteAndCancel()
        {
            var queryResult = await databaseQuerier.QueryIfDatabaseIsAlive();
            boxScanningSession.LastDatabaseCallFailed = !queryResult.IsAlive;

            if (boxScanningSession.LastDatabaseCallFailed)
            {
                ErrorIsResolved = false;
            }
            else
            {
                ErrorIsResolved = true;
                PublishEvent<DeleteAllInBoxAndScanAgainEvent, DeleteAllInBoxAndScanAgainEventArgs>(new DeleteAllInBoxAndScanAgainEventArgs(false));
            }
        }

        private async void DeleteAndRestart()
        {
            var queryResult = await databaseQuerier.QueryIfDatabaseIsAlive();
            boxScanningSession.LastDatabaseCallFailed = !queryResult.IsAlive;

            if (boxScanningSession.LastDatabaseCallFailed)
            {
                ErrorIsResolved = false;
            }
            else
            {
                ErrorIsResolved = true;
                PublishEvent<DeleteAllInBoxAndScanAgainEvent, DeleteAllInBoxAndScanAgainEventArgs>(new DeleteAllInBoxAndScanAgainEventArgs(true));
            }
        }

        private async void RestartAfterLast()
        {
            var queryResult = await databaseQuerier.QueryIfDatabaseIsAlive();
            boxScanningSession.LastDatabaseCallFailed = !queryResult.IsAlive;

            if (boxScanningSession.LastDatabaseCallFailed)
            {
                ErrorIsResolved = false;
            }
            else
            {
                ErrorIsResolved = true;
                PublishEvent<ScanJobRequestedStartedEvent, ScanJobRequestedStartedEventArgs>(new ScanJobRequestedStartedEventArgs());
            }
        }

        public bool ErrorIsResolved
        {
            get { return errorIsResolved; }
            private set
            {
                errorIsResolved = value;
                OnPropertyChanged();
            }
        }

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

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

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

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

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

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