using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Documents;
using System.Windows.Input;
using Valg.EVA.Skanning.UserInterfaceElements.EventArgs;
using Prism.Commands;
using Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.Candidate;

namespace Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.RenumberStrikeout
{
    public class RenumberStrikeoutContextMenuCommand
    {
        private readonly DelegateCommand<RenumberViewModel> renumberCommand;
        private readonly DelegateCommand strikeoutCommand;
        private readonly DelegateCommand noChangesCommand;
        private readonly RenumberStrikeoutContextMenuViewModel renumberStrikeoutContextMenuViewModel;

        public RenumberStrikeoutContextMenuCommand()
        {
            renumberCommand = new DelegateCommand<RenumberViewModel>(ExecuteRenumber);
            strikeoutCommand = new DelegateCommand(ExecuteStrikeout);
            noChangesCommand = new DelegateCommand(ExecuteNoChanges);

            renumberStrikeoutContextMenuViewModel = new RenumberStrikeoutContextMenuViewModel
            {
                DisplayName = Resources.Language.VerifyLanguageString.RenumberCandidateText,
                Command = RenumberCommand
            };
            CommandList = new ObservableCollection<ContextMenuViewModel>
            {
                renumberStrikeoutContextMenuViewModel,
                new ContextMenuViewModel { DisplayName = Resources.Language.VerifyLanguageString.StrikeoutCandidateText, Command = StrikeoutCommand },
                new ContextMenuViewModel { DisplayName = Resources.Language.VerifyLanguageString.NoCandidateChangesText, Command = NoChangesCommand }
            };
        }

        public void SetPossibleRenumberValues(List<int> renumberValues)
        {
            renumberStrikeoutContextMenuViewModel.SetPossibleRenumberValues(renumberValues);
        }

        public ObservableCollection<ContextMenuViewModel> CommandList { get; }

        public event EventHandler<CustomEventArgs> ContextMenuCommandWasExecuted;

        private ICommand RenumberCommand => renumberCommand;

        private ICommand StrikeoutCommand => strikeoutCommand;

        private ICommand NoChangesCommand => noChangesCommand;

        private void ExecuteRenumber(RenumberViewModel renumberViewModel)
        {
            RaiseRenumberSelectedEvent(renumberViewModel.Value);
        }

        public void RaiseRenumberSelectedEvent(int newCandidateIndex)
        {
            ContextMenuCommandWasExecuted?.Invoke(this, new CustomEventArgs(newCandidateIndex));
        }

        private void ExecuteStrikeout()
        {
            ContextMenuCommandWasExecuted?.Invoke(this, new CustomEventArgs(true));
        }

        private void ExecuteNoChanges()
        {
            ContextMenuCommandWasExecuted?.Invoke(this, new CustomEventArgs());
        }
    }
}
