
using System.Text;

/**
 * 基本区扩展区互换
 *
 * @author alimjan
 */
public static class UyBaseAndEx
{

    public static char[] UChar600 = {'ت', 'پ', 'ب', 'ر', 'د', 'چ', 'ج',
            'ز', 'ف', 'ق', 'ك', 'ش', 'گ', 'س', 'ڭ', 'ن', 'م', 'ۋ', 'ل', 'خ',
            'غ', 'ژ', 'ي', 'ا', 'ە', 'و', 'ۈ', 'ۆ', 'ۇ', 'ې', 'ى', 'ھ', 'ئ'};
    public static char[] UCharExB = {'ﺗ', 'ﭘ', 'ﺑ', 'ﺭ', 'ﺩ', 'ﭼ', 'ﺟ',
            'ﺯ', 'ﻓ', 'ﻗ', 'ﻛ', 'ﺷ', 'ﮔ', 'ﺳ', 'ﯕ', 'ﻧ', 'ﻣ', 'ﯞ', 'ﻟ', 'ﺧ',
            'ﻏ', 'ﮊ', 'ﻳ', 'ﺍ', 'ﻩ', 'ﻭ', 'ﯛ', 'ﯙ', 'ﯗ', 'ﯦ', 'ﯨ', 'ﮬ', 'ﺋ'};
    public static char[] UCharExO = {'ﺘ', 'ﭙ', 'ﺒ', 'ﺮ', 'ﺪ', 'ﭽ', 'ﺠ',
            'ﺰ', 'ﻔ', 'ﻘ', 'ﻜ', 'ﺸ', 'ﮕ', 'ﺴ', 'ﯖ', 'ﻨ', 'ﻤ', 'ﯟ', 'ﻠ', 'ﺨ',
            'ﻐ', 'ﮋ', 'ﻴ', 'ﺎ', 'ﻪ', 'ﻮ', 'ﯜ', 'ﯚ', 'ﯘ', 'ﯧ', 'ﯩ', 'ﮭ', 'ﺌ'};
    public static char[] UCharExA = {'ﺖ', 'ﭗ', 'ﺐ', 'ﺮ', 'ﺪ', 'ﭻ', 'ﺞ',
            'ﺰ', 'ﻒ', 'ﻖ', 'ﻚ', 'ﺶ', 'ﮓ', 'ﺲ', 'ﯔ', 'ﻦ', 'ﻢ', 'ﯟ', 'ﻞ', 'ﺦ',
            'ﻎ', 'ﮋ', 'ﻲ', 'ﺎ', 'ﻪ', 'ﻮ', 'ﯜ', 'ﯚ', 'ﯘ', 'ﯥ', 'ﻰ', 'ﮫ', 'ﺌ'};
    public static char[] UCharExY = {'ﺕ', 'ﭖ', 'ﺏ', 'ﺭ', 'ﺩ', 'ﭺ', 'ﺝ',
            'ﺯ', 'ﻑ', 'ﻕ', 'ﻙ', 'ﺵ', 'ﮒ', 'ﺱ', 'ﯓ', 'ﻥ', 'ﻡ', 'ﯞ', 'ﻝ', 'ﺥ',
            'ﻍ', 'ﮊ', 'ﻱ', 'ﺍ', 'ﻩ', 'ﻭ', 'ﯛ', 'ﯙ', 'ﯗ', 'ﯤ', 'ﻯ', 'ﮪ', 'ﺋ'};
    public static char[] SOL_KOL = {'ت', 'ئ', 'خ', 'چ', 'ج', 'پ', 'ب', 'س',
            'ش', 'غ', 'ف', 'ق', 'ك', 'گ', 'ڭ', 'ل', 'م', 'ن', 'ھ', 'ې', 'ى',
            'ي'};
    private static LA_Class la1 = new LA_Class('ﻟ', 'ﺎ', 'ﻻ');

    private static LA_Class la2 = new LA_Class('ﻠ', 'ﺎ', 'ﻼ');

    private static int containsInArray(char[] arr, char s)
    {
        int rtn = -1;
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i] == s)
            {
                rtn = i;
                break;
            }
        }
        return rtn;
    }



    private static string ProLA_HAMZE(string aWord)
    {

        char[] tmp = aWord.ToCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < tmp.Length - 1; i++)
        {
            if (tmp[i] == la1.base1 && tmp[i + 1] == la1.base2)
            {
                sb.Append(la1.ex);
                i++;
            }
            else if (tmp[i] == la2.base1 && tmp[i + 1] == la2.base2)
            {
                sb.Append(la2.ex);
                i++;
            }
            else
            {
                sb.Append(tmp[i]);
            }
        }
        if (aWord.Length < 2)
        {
            sb.Append(aWord);
        }
        else
        {
            if (tmp[tmp.Length - 2] == la1.base1 && tmp[tmp.Length - 1] == la1.base2)
            {

            }
            else if (tmp[tmp.Length - 2] == la2.base1 && tmp[tmp.Length - 1] == la2.base2)
            {

            }
            else
            {
                sb.Append(tmp[tmp.Length - 1]);
            }
        }
        return sb.ToString();
    }

    private static char getExChar(char achar, UyHarp k)
    {
        // 获取一个基本去字符对应的扩展区字符
        int indx = containsInArray(UChar600, achar);
        if (indx > -1)
        {
            switch (k)
            {
                case UyHarp.BASH:
                    return UCharExB[indx];
                case UyHarp.OTTURA:
                    return UCharExO[indx];
                case UyHarp.AHIR:
                    return UCharExA[indx];
                case UyHarp.YALGHUZ:
                    return UCharExY[indx];
            }
        }
        return achar;
    }


    /**
     * 转扩展去
     *
     * @param s
     * @return
     */
    public static string toEx(string s)
    {

        if (!string.IsNullOrEmpty(s))
        {
            StringBuilder exWord = new StringBuilder();

            int indx, indx2, indx3 = -1;
            char c1, c2, c3, c1Ex, c2Ex;
            char[] tmp = s.ToCharArray();
            if (s.Length == 1)
            {
                indx = containsInArray(UChar600, s.ToCharArray()[0]);
                if (indx != -1)
                    return UCharExY[indx] + "";
                else
                    return s;
            }
            if (s.Length == 2)
            {
                c1 = tmp[0];
                c2 = tmp[1];

                if (containsInArray(SOL_KOL, c1) > -1)
                {
                    c1Ex = getExChar(c1, UyHarp.BASH);
                    c2Ex = getExChar(c2, UyHarp.AHIR);
                    return "" + c1Ex + c2Ex;
                }
            }
            c1 = tmp[0];
            c1Ex = getExChar(c1, UyHarp.BASH);
            exWord.Append(c1Ex);

            for (int i = 1; i < tmp.Length - 1; i++)
            {
                c1 = tmp[i - 1];
                c2 = tmp[i];
                c3 = tmp[i + 1];
                indx3 = containsInArray(UChar600, c3);
                indx2 = containsInArray(UChar600, c2);
                if (indx2 > -1)
                {
                    if (containsInArray(SOL_KOL, c1) > -1)
                    {
                        if (indx3 > -1)
                        {
                            c2Ex = getExChar(c2, UyHarp.OTTURA);
                        }
                        else
                        {
                            c2Ex = getExChar(c2, UyHarp.AHIR);
                        }
                    }
                    else
                    {
                        if (indx3 > -1)
                        {
                            c2Ex = getExChar(c2, UyHarp.BASH);
                        }
                        else
                        {
                            c2Ex = getExChar(c2, UyHarp.YALGHUZ);
                        }
                    }
                }
                else
                    c2Ex = c2;
                exWord.Append(c2Ex);
            }

            if (containsInArray(SOL_KOL, tmp[tmp.Length - 2]) > -1)
            {
                c2Ex = getExChar(tmp[tmp.Length - 1],
                        UyHarp.AHIR);
            }
            else
            {
                c2Ex = getExChar(tmp[tmp.Length - 1],
                        UyHarp.YALGHUZ);
            }
            exWord.Append(c2Ex);
            return ProLA_HAMZE(exWord.ToString());
        }
        return "";
    }

    private enum UyHarp
    {
        BASH, OTTURA, AHIR, YALGHUZ
    }

    private class LA_Class
    {
        public char base1, base2;
        public char ex;

        public LA_Class(char base1, char base2, char ex)
        {
            this.base1 = base1;
            this.base2 = base2;
            this.ex = ex;
        }
    }

}