using System;
using System.Text;
using Earthblend.Tts.Common.Core.Statics;

namespace Earthblend.Tts.Common.Core.Services
{
    public class SubscriptService
    {
        public String ReverseTranslate(String displayText)
        {
            if (String.IsNullOrEmpty(displayText)) return displayText;
            int index = displayText.IndexOf("Sub");
            if (index == -1) return displayText;
            String start = displayText.Substring(0, index);
            return start + VariableUtil.ReverseTranslateSubscript(displayText.Substring(index, displayText.Length - index).Replace("Sub", ""));
        }

        public String TranslateComplexSubscript(String displayText)
        {
            if (displayText == "I.Q₁.X³") { }

            Boolean foundSuperscript = false;
            StringBuilder super = new StringBuilder();
            foreach (var c in displayText)
            {
                if (VariableUtil.KnownSuperscripts.ContainsKey(c))
                {
                    foundSuperscript = true;
                    super.Append(VariableUtil.KnownSuperscripts[c]);
                }
            }

            StringBuilder sb = new StringBuilder();

            if (foundSuperscript)
            {
                sb.Append("Math.Pow(");
            }

            int index = -1;
            Boolean foundSub = false;
            foreach (var c in displayText)
            {
                index++;
                if (VariableUtil.KnownSubscripts.ContainsKey(c.ToString()))
                {
                    if (!foundSub) sb.Append("Sub");
                    sb.Append(c);
                    foundSub = true;
                }
                else
                {
                    foundSub = false;
                    sb.Append(c);
                }
            }

            if (foundSuperscript)
            {
                sb.Append("," + super.ToString() + ")");
            }

            StringBuilder s = new StringBuilder();
            foreach (var c in sb.ToString())
            {
                if (VariableUtil.KnownSubscripts.ContainsKey(c.ToString()))
                {
                    s.Append(VariableUtil.ConvertSubscript(c));
                }
                else
                {
                    s.Append(c);
                }
            }

            return s.ToString();
        }

        public String TranslateSubscript(String displayText)
        {
            if (displayText == "SX₁") { }

            StringBuilder sb = new StringBuilder();

            Boolean foundSuperscript = false;
            String strSuperscript = String.Empty;
            foreach (var c in displayText)
            {
                if (VariableUtil.KnownSuperscripts.ContainsKey(c))
                {
                    strSuperscript = c.ToString();
                    foundSuperscript = true;
                    break;
                }
            }

            #region Subscripts

            int subscriptIndex = 0;
            Boolean foundSubscript = false;

            int subscriptCount = 0;
            foreach (var c in displayText)
            {
                if (VariableUtil.KnownSubscripts.ContainsKey(c.ToString()))
                    subscriptCount++;
            }

            foreach (var c in displayText)
            {
                if (VariableUtil.KnownSubscripts.ContainsKey(c.ToString()))
                {
                    if (foundSuperscript) sb.Append("Math.Pow(");

                    var left = displayText.Substring(0, subscriptIndex);
                    sb.Append(left);
                    String subscript = displayText.Substring(subscriptIndex, subscriptCount); //subscriptIndex +      - 1
                    sb.Append(VariableUtil.KnownSubscripts[subscript]);
                    foundSubscript = true;
                    break;
                }
                subscriptIndex++;
            }

            #endregion

            if (foundSuperscript && foundSuperscript)
                sb.Append("," + VariableUtil.KnownSuperscriptsTable[strSuperscript] + ")");

            if (foundSubscript)
                return sb.ToString();
            else
            {
                return displayText;
            }
        }

    }
}
