using Valg.EVA.Skanning.Contracts.CommonEnums;
using Valg.EVA.Skanning.ServicesModel.ServiceContracts.Verification.Write;
using System.Linq;

namespace Valg.EVA.Skanning.Commands.SaveBallotPaperVerifications
{
    public class SaveBallotPaperVerificationsCommand : ICommand
    {
        public SaveBallotPaperVerificationsCommand(
            BallotNumberVerification ballotNumberVerification,
            PersonVoteVerification[] personVoteVerifications,
            int ballotPaperId,
            int ballotPaperConcurrencyToken,
            RejectionReasonVerification rejectionReasonVerification,
            StampVerification stampVerification,
            string changedBy,
            IWriteInVerification[] writeInVerifications,
            bool isInEmergencyMode, 
            IRenumberStrikeoutVerification[] renumberStrikeoutVerifications,
            bool? isAffiliationHeadersValid)
        {
            BallotNumberVerification = ballotNumberVerification;
            PersonVoteVerifications = personVoteVerifications;
            BallotPaperId = ballotPaperId;
            BallotPaperConcurrencyToken = ballotPaperConcurrencyToken;
            RejectionReasonVerification = rejectionReasonVerification;
            StampVerification = stampVerification;
            ChangedBy = changedBy;
            WriteInVerifications = writeInVerifications;
            RenumberStrikeoutVerifications = renumberStrikeoutVerifications;
            IsInEmergencyMode = isInEmergencyMode;
            IsAffiliationHeadersValid = isAffiliationHeadersValid ?? false;
        }
        public bool IsAffiliationHeadersValid { get; set; }
        public BallotNumberVerification BallotNumberVerification { get; private set; }
        public PersonVoteVerification[] PersonVoteVerifications { get; private set; }
        public RejectionReasonVerification RejectionReasonVerification { get; private set; }
        public IRenumberStrikeoutVerification[] RenumberStrikeoutVerifications { get; private set; }
        public StampVerification StampVerification { get; private set; }
        public IWriteInVerification[] WriteInVerifications { get; private set; }

        public int BallotPaperId { get; private set; }

        public int BallotPaperConcurrencyToken { get; private set; }

        public string ChangedBy { get; private set; }

        public bool IsInEmergencyMode { get; private set; }
        
        public bool IsEmpty
        {
            get
            {
                return StampVerification == null
                    && RejectionReasonVerification == null
                    && BallotNumberVerification == null
                    && (WriteInVerifications == null || WriteInVerifications.Length == 0)
                    && (PersonVoteVerifications == null || PersonVoteVerifications.Length == 0)
                    && (RenumberStrikeoutVerifications == null || RenumberStrikeoutVerifications.Length == 0);
            }
        }

        public bool IsRejectedBecauseOfInvalidStamp
        {
            get
            {
                return RejectionReasonVerification != null
                && RejectionReasonVerification.NewValue == RejectionReasonType.NoOfficialStamp;
            }
        }

        public bool IsRejectedForAnyReason
        {
            get { return RejectionReasonVerification != null && RejectionReasonsCollections.AllActualRejectionReasons.Contains(RejectionReasonVerification.NewValue); }
        }

        public bool IsExplicitlyNotRejected
        {
            get { return RejectionReasonVerification != null && RejectionReasonVerification.NewValue == RejectionReasonType.NotRejected; }
        }
    }
}
