using System;
using Valg.EVA.Skanning.ServicesModel.Internal.SystemSettings;

namespace Valg.EVA.Skanning.ServicesModel.Internal
{
    public class CountingUniqueIdCreator
    {
        private readonly ISystemSettingsProvider systemSettingsProvider;

        public CountingUniqueIdCreator(ISystemSettingsProvider systemSettingsProvider)
        {
            this.systemSettingsProvider = systemSettingsProvider;
        }

        public string Create(string barcodeLong, string barcodeShort)
        {
            if (string.IsNullOrEmpty(barcodeLong) 
                || string.IsNullOrEmpty(barcodeShort) 
                || barcodeLong.Length != systemSettingsProvider.GetBarcodeLongLength() 
                || (barcodeShort.Length != systemSettingsProvider.GetBarcodeShortLength() && barcodeShort.Length != systemSettingsProvider.GetBarcodeShortBoroughLength()))
            {
                throw new ArgumentException();
            }

            var electionAndContestInfo = barcodeLong.Substring(0, BarcodeSplitter.BoxNumberStartIndex);
            var votingCategory = barcodeLong.Substring(BarcodeSplitter.ContentTypeStartIndex + 1);
            return electionAndContestInfo + votingCategory + barcodeShort;
        }

        public static string Create(string electionEventId, string contestId, string areaContextId, string votingCategory, string electionIdentifier, string electionGroup)
        {
            string areaContextWithoutSeperator = areaContextId.Replace(".", "");
            return electionEventId + electionGroup + electionIdentifier + contestId + votingCategory + areaContextWithoutSeperator;
        }
    }
}
