using Valg.EVA.Skanning.ContainerExtensions;
using Valg.EVA.Skanning.ServicesModel.ServiceContracts.Verification.Write;
using Valg.EVA.Skanning.UserInterfaceElements.LightBox;
using Valg.EVA.Skanning.UserInterfaceElements.MessageBox;
using System;
using System.Windows;
using Prism.Commands;
using Valg.EVA.Skanning.UserInterfaceElements;
using Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.Messages;

namespace Valg.EVA.Skanning.Verify.Startup.BoxIsLocked
{
    public class VerificationProcessErrorsAndWarnings : ViewModelBase, IVerificationProcessErrorsAndWarnings
    {
        private readonly ITypedFactory typedFactory;
        private readonly IUnknownBallotInformationViewModel unknownBallotInformationViewModel;

        public VerificationProcessErrorsAndWarnings(ITypedFactory typedFactory, IUnknownBallotInformationViewModel unknownBallotInformationViewModel)
        {
            this.typedFactory = typedFactory;
            this.unknownBallotInformationViewModel = unknownBallotInformationViewModel;
        }

        public void ShowCannotChangeBallotWhenBoxIsLockedInformation()
        {
            var resultDialog = typedFactory.Create<IMessageBox>();

            resultDialog.Initialize(
                Valg.EVA.Skanning.Resources.Language.VerifyLanguageString.Warning_BallotCannotChangeWhenCountingIsLockedOrDeleted,
                GetCannotChangeBallotWhenBoxIsLockedTextDetails(),
                MessageBoxType.Warning);

            var eventArgs = new LightBoxEventArgs(resultDialog as Window);
            PublishEvent<ShowLightBoxEvent, LightBoxEventArgs>(eventArgs);
        }

        public void ShowCannotChangeBallotWhenUnlockedInformation()
        {
            var resultDialog = typedFactory.Create<IMessageBox>();

            resultDialog.Initialize(
                Valg.EVA.Skanning.Resources.Language.VerifyLanguageString.Warning_BallotCannotChangeWhenUnlocked_Title,
                GetCannotChangeBallotWhenUnlocked(),
                MessageBoxType.Warning);

            var eventArgs = new LightBoxEventArgs(resultDialog as Window);
            PublishEvent<ShowLightBoxEvent, LightBoxEventArgs>(eventArgs);
        }

        public void ShowBoxIsLockedOrDeletedInformation()
        {
            var resultDialog = typedFactory.Create<IMessageBox>();

            resultDialog.Initialize(
                Valg.EVA.Skanning.Resources.Language.VerifyLanguageString.Warning_CountingIsLockedOrDeleted,
                Valg.EVA.Skanning.Resources.Language.VerifyLanguageString.Warning_CountingIsLockedOrDeleted_Details,
                MessageBoxType.Warning);

            var eventArgs = new LightBoxEventArgs(resultDialog as Window);
            PublishEvent<ShowLightBoxEvent, LightBoxEventArgs>(eventArgs);
        }

        public void ShowErrorWhenAttemptingToLoadABallotPaper(Exception exception)
        {
            var dialog = typedFactory.Create<IMessageBox>();

            dialog.Initialize(Valg.EVA.Skanning.Resources.Language.VerifyLanguageString.Warning_CouldNotLoadBallotPaper,
                null,
                Valg.EVA.Skanning.Resources.Language.VerifyLanguageString.PossibleSolutionsForWarningCouldNotLoadBallotPaper,
                MessageBoxType.Warning,
                exception != null ? new[] { exception } : null);

            var eventArgs = new LightBoxEventArgs(dialog as Window);
            PublishEvent<ShowLightBoxEvent, LightBoxEventArgs>(eventArgs);
        }

        public void ShowBallotIsUnknownInformation()
        {
            unknownBallotInformationViewModel.ShowUnknownBallotInformationIfNotHiddenByUser();
        }

        public void ShowCannotChangeInformation(SaveBallotPaperVerificationsOperationResult saveBallotPaperVerificationsOperationResult)
        {
            switch (saveBallotPaperVerificationsOperationResult)
            {
                case SaveBallotPaperVerificationsOperationResult.ConcurrencyError:
                    ShowCannotChangeBallotWhenUnlockedInformation();
                    break;
                default:
                    ShowCannotChangeBallotWhenBoxIsLockedInformation();
                    break;
            }
        }

        public void ShowCannotSaveBallotPaperVerificationsWhenIncomplete(DelegateCommand confirmCommand)
        {
            var resultDialog = typedFactory.Create<IMessageBox>();

            resultDialog.Initialize(
                Valg.EVA.Skanning.Resources.Language.VerifyLanguageString.Warning_CannotSaveBallotVerificationsWhenIncomplete,
                Valg.EVA.Skanning.Resources.Language.VerifyLanguageString.Warning_CannotSaveBallotVerificationsWhenIncomplete_Details,
                MessageBoxType.Warning,
                confirmCommand,
                null);

            var eventArgs = new LightBoxEventArgs(resultDialog as Window);
            PublishEvent<ShowLightBoxEvent, LightBoxEventArgs>(eventArgs);
        }

        private static string GetCannotChangeBallotWhenBoxIsLockedTextDetails()
        {
            return string.Format(
                "{0}{1}{1}{2}",
                Valg.EVA.Skanning.Resources.Language.VerifyLanguageString.Warning_BallotCannotChangeWhenCountingIsLocked_ReasonOne,
                Environment.NewLine,
                Valg.EVA.Skanning.Resources.Language.VerifyLanguageString.Warning_BallotCannotChange_ReasonTwo);
        }

        private static string GetCannotChangeBallotWhenUnlocked()
        {
            return string.Format(
                "{0}{1}{1}{2}",
                Valg.EVA.Skanning.Resources.Language.VerifyLanguageString.Warning_BallotCannotChangeWhenUnlocked_ReasonOne,
                Environment.NewLine,
                Valg.EVA.Skanning.Resources.Language.VerifyLanguageString.Warning_BallotCannotChange_ReasonTwo);
        }
    }
}
