using Valg.EVA.Skanning.UserInterfaceElements.EventArgs;
using System.Collections.ObjectModel;
using Valg.EVA.Skanning.UserInterfaceElements;
using Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.VerifiableViewModelsCollection;
using Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.Messaging.Commands;
using Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.WriteModel;

namespace Valg.EVA.Skanning.Verify.Startup.VerifyBallotPaper.Candidate
{
    public class CandidateViewModel : ViewModelBase, IViewModelWithVerifiableContent
    {
        private readonly PersonVoteContextMenuCommand personVoteContextMenuCommand;
        private readonly IWriteModelHub writeModelHub;
        private bool isContextMenuOpen;
        private bool needsVerification;
        private bool? personVotedAsGivenByCorrection;
        private bool? personVotedAsInterpretedByScanner;
        private bool isActionButtonFocused;
        private bool neededVerification;

        public CandidateViewModel(PersonVoteContextMenuCommand personVoteContextMenuCommand, IWriteModelHub writeModelHub)
        {
            this.personVoteContextMenuCommand = personVoteContextMenuCommand;
            this.writeModelHub = writeModelHub;
            personVoteContextMenuCommand.ContextMenuCommandWasExecuted += ContextMenuCommandWasExecuted;
        }

        public int? CandidatePersonVoteFieldId { get; set; }

        public string Name { get; set; }

        public int CandidateRank { get; set; }

        public int BallotPaperId { get; set; }

        public bool NeededVerification
        {
            get { return neededVerification; }
            set
            {
                neededVerification = value;
                OnPropertyChanged();
            }
        }
        public int VerificationOrder { get; set; }

        public int PageIndex { get; set; }

        public ObservableCollection<ContextMenuViewModel> VerifyCommand
        {
            get
            {
                return personVoteContextMenuCommand.CommandList;
            }
        }
        public bool IsCreatedFirstTimeByVerifier { get; set; }

        public string CandidateRankFormatted
        {
            get { return string.Format("{0}.", CandidateRank); }
        }

        public bool HasCandidateBeenPersonVoted
        {
            get
            {
                if (PersonVotedAsGivenByCorrection.HasValue)
                {
                    return PersonVotedAsGivenByCorrection.Value;
                }

                if (PersonVotedAsInterpretedByScanner.HasValue)
                {
                    return PersonVotedAsInterpretedByScanner.Value;
                }

                return false;
            }
        }

        public bool? PersonVotedAsInterpretedByScanner
        {
            get { return personVotedAsInterpretedByScanner; }
            set
            {
                personVotedAsInterpretedByScanner = value;

                OnPropertyChanged();
                var propertyName = PropertyHelper.GetName<CandidateViewModel>(c => c.HasCandidateBeenPersonVoted);
                OnPropertyChanged(propertyName);
            }
        }

        public bool? PersonVotedAsGivenByCorrection
        {
            get
            {
                return personVotedAsGivenByCorrection;
            }
            set
            {
                personVotedAsGivenByCorrection = value;

                OnPropertyChanged();
                OnPropertyChanged(PropertyHelper.GetName<CandidateViewModel>(c => c.HasCandidateBeenPersonVoted));
            }
        }

        public bool HasUserCorrectionsBeenMade
        {
            get
            {
                return PersonVotedAsGivenByCorrection.HasValue;
            }
        }

        public bool IsContextMenuOpen
        {
            get { return isContextMenuOpen; }
            set
            {
                if (isContextMenuOpen != value)
                {
                    isContextMenuOpen = value;
                    OnPropertyChanged();
                }
            }
        }

        public bool NeedsVerification
        {
            get
            {
                return needsVerification;
            }
            set
            {
                needsVerification = value;
                OnPropertyChanged();
            }
        }

        public bool IsActionButtonFocused
        {
            get { return isActionButtonFocused; }
            set
            {
                isActionButtonFocused = value;
                OnPropertyChanged();
            }
        }

        public void RemainFocusAtThisViewModel()
        {
            IsActionButtonFocused = true;
        }

        public void BringToFocus()
        {
            IsActionButtonFocused = true;
            IsContextMenuOpen = true;
        }

        public void Reset()
        {
            PersonVotedAsGivenByCorrection = null;

            NeedsVerification = NeededVerification;
            OnPropertyChanged(PropertyHelper.GetName<CandidateViewModel>(c => c.HasUserCorrectionsBeenMade));
        }

        private void ContextMenuCommandWasExecuted(object sender, CustomEventArgs args)
        {
            var newValue = args.BoolValue;
            PersonVotedAsGivenByCorrection = newValue;
            var neededVerificationCopy = NeedsVerification;

            NeedsVerification = false;
            writeModelHub.AddPersonVoteModification(CandidateRank, newValue, CandidatePersonVoteFieldId);

            IsContextMenuOpen = false;
            OnPropertyChanged(PropertyHelper.GetName<CandidateViewModel>(c => c.HasUserCorrectionsBeenMade));

            var actionWasCommmitedArgs = new VerificationWasResolvedEventArgs(this, neededVerificationCopy);
            PublishEvent<VerificationResolvedOnVerifiableViewModelEvent, VerificationWasResolvedEventArgs>(actionWasCommmitedArgs);
        }
    }
}

