using System.Collections.Generic;
using System.Linq;
using Valg.EVA.Skanning.Contracts;
using Valg.EVA.Skanning.Scan.ReadSoftContracts.Model;

namespace Valg.Eva.Skanning.Scan.Presentation.ReadSoftModel.FieldValidators
{
    public static class CandidateRenumberValidationHelper
    {
        public static bool IsRenumberUsed(string renumberCandidate, IEnumerable<ScannedField> fields)
        {
            return fields.Any(x => x.IsRenumberField() && (x.Value == renumberCandidate || AreValuesIntegerEqual(x.Value,renumberCandidate)));
        }

        private static bool AreValuesIntegerEqual(string alreadyUsedValue, string renumberCandidate)
        {
            int result1;
            if (int.TryParse(alreadyUsedValue, out result1))
            {
                int result2;
                if (int.TryParse(renumberCandidate, out result2))
                {
                    return result1 == result2;
                }
                
            }
            return false;

        }
    }
}
