using System;
using Valg.EVA.Skanning.Contracts;
using Valg.EVA.Skanning.Entity.ReadModel;
using Valg.EVA.Skanning.ServicesModel.ElectionConfig.Model;
using Valg.EVA.Skanning.ServicesModel.Internal.Validation;

namespace Valg.EVA.Skanning.ServicesModel.Internal.CountResult
{
    public class FilterForStrikeoutInclusion
    {
        private readonly FillPercentageRangeChecker rangeChecker;
        private readonly Affiliation affiliation;

        public FilterForStrikeoutInclusion(FillPercentageRangeChecker rangeChecker, Affiliation affiliation)
        {
            this.rangeChecker = rangeChecker;
            this.affiliation = affiliation;
        }

        public bool IsIncluded(BallotPaperFieldValue field)
        {
            if (field.IsStrikeoutField() 
                && field.Value != ScannerFieldValueTemplateValues.FieldIsIgnored
                && affiliation.CandidateExistsWithinTheAffiliation(field.FieldIndex.GetValueOrDefault()))
            {
                var value = Convert.ToInt32(field.Value);
                if ((value == ScannerFieldValueTemplateValues.StrikeoutIsGiven)
                    || rangeChecker.IsInsideRange(value))
                {
                    return true;
                }
            }

            return false;
        }
    }
}
