using System;

namespace Valg.EVA.Skanning.ServicesModel.Internal
{
    public static class BarcodeSplitter
    {
        public static readonly int ElectionEventIdStartIndex = 0;
        public static readonly int LengthOfElectionEventId = 6;

        public static readonly int ElectionGroupIdStartIndex = 6;
        public static readonly int LengthOfElectionGroupId = 2;

        public static readonly int ElectionIdStartIndex = 8;
        public static readonly int LengthOfElectionId = 2;

        public static readonly int ContestIdStartIndex = 10;
        public static readonly int LengthOfContestId = 6;

        public static readonly int BoxNumberStartIndex = 16;
        public static readonly int LengthOfBoxNumber = 2;

        public static readonly int TotalBoxCountStartIndex = 18;
        public static readonly int LengthOfTotalBoxCount = 2;

        public static readonly int ContentTypeStartIndex = 20;
        public static readonly int LengthOfContentType = 1;

        public static readonly int VotingCategoryTypeStartIndex = 21;
        public static readonly int LengthOfVotingCategoryType = 2;

        public static readonly int CountryCodeStartIndex = 0;
        public static readonly int LengthOfCountryCode = 2;

        public static readonly int CountyCodeStartIndex = 2;
        public static readonly int LengthOfCountyCode = 2;

        public static readonly int MunicipalityCodeStartIndex = 4;
        public static readonly int LengthOfMunicipalityCode = 4;

        public static readonly int LocationCodeStartIndex = 8;
        public static readonly int LengthOfLocationCode = 6;

        public static string GetElectionEventIdFromBarcodeLong(this string stringToConvert)
        {
            if (String.IsNullOrEmpty(stringToConvert))
            {
                return String.Empty;
            }
            var length = LengthOfElectionEventId <= stringToConvert.Length ? LengthOfElectionEventId : stringToConvert.Length;
            return stringToConvert.Substring(ElectionEventIdStartIndex, length);
        }

        public static string GetElectionGroupIdFromBarcodeLong(this string barcodeLong)
        {
            if (String.IsNullOrEmpty(barcodeLong))
            {
                return String.Empty;
            }

            return barcodeLong.Substring(ElectionGroupIdStartIndex, LengthOfElectionGroupId);
        }

        public static string GetElectionIdFromBarcodeLong(this string stringToConvert)
        {
            if (String.IsNullOrEmpty(stringToConvert))
            {
                return String.Empty;
            }

            return stringToConvert.Substring(ElectionIdStartIndex, LengthOfElectionId);
        }

        public static string GetContestIdFromBarcodeLong(this string stringToConvert)
        {
            if (String.IsNullOrEmpty(stringToConvert))
            {
                return String.Empty;
            }

            return stringToConvert.Substring(ContestIdStartIndex, LengthOfContestId);
        }

        public static string GetVotingCategoryTypeFromBarcodeLong(this string stringToConvert)
        {
            if (String.IsNullOrEmpty(stringToConvert))
            {
                return String.Empty;
            }

            return stringToConvert.Substring(VotingCategoryTypeStartIndex, LengthOfVotingCategoryType);
        }

        public static int GetBoxNumberFromBarcodeLong(this string barcodeLong)
        {
            if (String.IsNullOrEmpty(barcodeLong))
            {
                return 0;
            }

            return Convert.ToInt32(barcodeLong.Substring(BoxNumberStartIndex, LengthOfBoxNumber));
        }

        public static int GetTotalBoxCountFromBarcodeLong(this string barcodeLong)
        {
            if (String.IsNullOrEmpty(barcodeLong))
            {
                return 0;
            }

            return Convert.ToInt32(barcodeLong.Substring(TotalBoxCountStartIndex, LengthOfTotalBoxCount));
        }

        public static string GetContentTypeFromBarcodeLong(this string stringToConvert)
        {
            if (String.IsNullOrEmpty(stringToConvert))
            {
                return String.Empty;
            }

            return stringToConvert.Substring(ContentTypeStartIndex, LengthOfContentType);
        }

        public static string GetCountyCodeFromBarcodeShort(this string barcodeShort)
        {
            if (string.IsNullOrEmpty(barcodeShort))
            {
                return string.Empty;
            }

            return barcodeShort.Substring(CountyCodeStartIndex, LengthOfCountyCode);
        }

        public static string GetMunicipalityCodeFromBarcodeShort(this string stringToConvert)
        {
            if (String.IsNullOrEmpty(stringToConvert))
            {
                return String.Empty;
            }

            return stringToConvert.Substring(MunicipalityCodeStartIndex, LengthOfMunicipalityCode);
        }

        public static string GetLocationFromBarcodeShort(this string barcodeShort)
        {
            if (string.IsNullOrEmpty(barcodeShort))
            {
                return string.Empty;
            }

            return barcodeShort.Substring(LocationCodeStartIndex, LengthOfLocationCode);
        }
    }
}
