using System.Collections.Generic;
using Valg.EVA.Skanning.Scan.ReadSoftContracts.Model;
using Valg.EVA.Skanning.Services.SystemSettings;

namespace Valg.Eva.Skanning.Scan.Presentation.ReadSoftModel.FieldValidators
{
    public class BallotIdFieldValidator : IFieldValidator
    {
        private readonly ISystemSettingsService systemSettingsService;

        public BallotIdFieldValidator(ISystemSettingsService systemSettingsService)
        {
            this.systemSettingsService = systemSettingsService;
        }

        public ScannableFieldType FieldType { get { return ScannableFieldType.BallotID; } }
        public bool IsFieldValid(ScannedField field, IEnumerable<ScannedField> alreadyAddedFields = null)
        {
            return (
                !field.Value.Contains("*") &&
                HasBallotIdValidLength(field.Value));
        }

        private bool HasBallotIdValidLength(string value)
        {
            return value.Length  == systemSettingsService.GetBallotIdLength() || value.Length == systemSettingsService.GetBallotIdLength() - 1;
        }
    }
}
