﻿namespace Parts.Utilities
{
    using Parts.Models;
    using System.Text.RegularExpressions;
    using System.Text;
    using System;

    public static class NameHelpers
    {
        public static string ComposeModificationName(ModificationInfo modification)
        {
            StringBuilder sb = new StringBuilder();

            // TODO: try to consolidate all these normalizations in a single class with multiple methods or something like this.
            sb.Append(NormalizeSpaces(GetModification(modification.ModificationNameLatin)) + " ");
            sb.Append(NormalizeSpaces(GetEngine(modification.Engine)) + " ");
            sb.Append(modification.EngineVolume.Trim() + "cc ");
            sb.Append(string.Format("({0})", GetPower(modification.EnginePower)));
            sb.Append(string.Format("({0}-{1})", GetFromDate(modification.ProducedFrom), GetToDate(modification.ProducedTo)));

            if (sb.ToString().IndexOf('/') > 0)
            {

            }

            return sb.ToString();
        }

        public static string NormalizeModelName(string modelName)
        {
            // replacing the slash symbol in some model names with + and trimming the spaces arround it
            string pattern = @"\s*\/\s*";
            modelName = Regex.Replace(modelName, pattern, "+");
            modelName = modelName.ToUpper();
            modelName = modelName.Trim();

            return modelName;
        }

        private static string NormalizeSpaces(string stringToNormalize)
        {
            string spacesPattern = @"\s+";
            var normalizedString = Regex.Replace(stringToNormalize, spacesPattern, " ");

            return normalizedString;
        }

        // TODO: Merge that and GetToDate() in a single method.
        // TODO: Sometimes there is a empty string coming into the method. Find the problematic part and handle it elsewhere.
        private static string GetFromDate(string rawFromDate)
        {
            string toPattern = @"\d{1,2}.\d{4}";
            string date = Regex.Match(rawFromDate, toPattern).Value;
            date = date.Replace('/', '.');
            date = date.Trim();

            if (rawFromDate != string.Empty)
            {
                var d = DateTime.Parse(rawFromDate);
                date = d.ToString("M.yyyy");
            }

            return date; 
        }

        private static string GetToDate(string rawToDate)
        {
            string toPattern = @"\d{1,2}.\d{4}";
            string date = Regex.Match(rawToDate, toPattern).Value;
            date = date.Replace('/', '.');
            
            date = date.Trim();
            if (rawToDate != string.Empty)
            {
                var d = DateTime.Parse(rawToDate);
                date = d.ToString("M.yyyy");
            }

            return date;
        }

        private static string GetPower(string rawPower)
        {
            string normalizedPower = rawPower.Replace('/', '-');
            normalizedPower = normalizedPower.Trim();

            return normalizedPower;
        }

        private static string GetEngine(string rawEngine)
        {
            string normalizedEngine = rawEngine.Replace("A/T", "AT");
            normalizedEngine = normalizedEngine.Replace('/', '-');
            normalizedEngine = normalizedEngine.Trim();

            return normalizedEngine;
        }

        private static string GetModification(string rawModification)
        {
            string normalizedModification = rawModification.Replace('/', '-');
            normalizedModification = normalizedModification.Trim();

            return normalizedModification;
        }
    }
}
