using Furion;
using System.Text;
namespace Admin.NET.Core
{
    /// 
    /// Éú³ÉÖú¼ÇÂë
    /// 
    public class MnemonicCodeUtil
    {
        private struct ItemWord
        {
            public int numFlag;     //ÊÇ·ñ¶àÒô×Ö
            public char strWord;    //µ±Ç°µ¥´Ê
            public string strSpell1;//ÊäÈëÂë1
            public string strSpell2;//ÊäÈëÂë2
            public string strExt;   //¶àÒô×Ö±£Áô´®
            public string strName;  //ÓÃÓÚ¶¨ÒåÐÕÊÏÂë
        }
        private static readonly int INTMAX = 65536;
        private static ItemWord[] stWord = new ItemWord[INTMAX];
        private static Boolean b_LoadData = false;
        //¶ÁÈ¡ËùÓкº×Ö
        public static Boolean fun_LoadWords()
        {
            int i, numIndex;
            int numCount1, numCount2;
            string strFileName;
            char[] chrWord;
            if (b_LoadData)
                return true;
            //ÒÔϳõʼ»¯ËùÓеÄÊý¾Ý
            for (i = 0; i < INTMAX; i++)
            {
                stWord[i].numFlag = 0;
                stWord[i].strSpell1 = "";
                stWord[i].strSpell2 = "";
                stWord[i].strName = "";
                stWord[i].strExt = "|";
            }
            var rootPath = System.IO.Path.Combine(App.WebHostEnvironment.WebRootPath, "ChineseSpellFile");
            //¶ÁÈ¡ºº×ÖÆ´Òô
            strFileName = rootPath + @"\hzpy1.txt";
            StreamReader srFile = new StreamReader(strFileName, Encoding.ASCII);
            string strInput = null;
            numCount1 = 0;
            while ((strInput = srFile.ReadLine()) != null)
            {
                chrWord = strInput.ToCharArray();
                numIndex = (int)chrWord[0];
                if (numIndex <= 0) continue;
                if (numIndex >= INTMAX) continue;
                stWord[numIndex].strWord = chrWord[0];
                stWord[numIndex].strSpell1 = chrWord[2].ToString();
                numCount1++;
            }
            srFile.Close();
            //¶ÁÈ¡¶àÒô×Ö
            strFileName = rootPath + @"\hzpy2.txt";
            srFile = new StreamReader(strFileName, Encoding.ASCII);
            while ((strInput = srFile.ReadLine()) != null)
            {
                chrWord = strInput.ToCharArray();
                numIndex = (int)chrWord[0];
                if (numIndex <= 0) continue;
                if (numIndex >= INTMAX) continue;
                stWord[numIndex].numFlag = 1;
                strInput = strInput.Substring(2);
                strInput = strInput.Replace("\t", " ");
                while (strInput.IndexOf("  ") >= 0)
                {
                    strInput = strInput.Replace("  ", " ");
                }
                strInput = strInput.Replace(" ", "/");
                stWord[numIndex].strExt = stWord[numIndex].strExt + strInput + "|";
            }
            srFile.Close();
            //¶ÁÈ¡ÐÕÊÏ
            strFileName = rootPath + @"\hzpy3.txt";
            srFile = new StreamReader(strFileName, Encoding.ASCII);
            while ((strInput = srFile.ReadLine()) != null)
            {
                chrWord = strInput.ToCharArray();
                numIndex = (int)chrWord[0];
                if (numIndex <= 0) continue;
                if (numIndex >= INTMAX) continue;
                stWord[numIndex].numFlag = 1;
                strInput = strInput.Substring(2);
                strInput = strInput.Replace("\t", " ");
                while (strInput.IndexOf("  ") >= 0)
                {
                    strInput = strInput.Replace("  ", " ");
                }
                stWord[numIndex].strName = strInput;
            }
            srFile.Close();
            //ÒÔϲ¿·Ö¶ÁÈ¡Îå±ÊÂëµÄÊ××Ö·û
            strFileName = rootPath + @"\hzwb.txt";
            numCount2 = 0;
            srFile = new StreamReader(strFileName, Encoding.ASCII);
            while ((strInput = srFile.ReadLine()) != null)
            {
                chrWord = strInput.ToCharArray();
                numIndex = (int)chrWord[0];
                if (numIndex <= 0) continue;
                if (numIndex >= INTMAX) continue;
                stWord[numIndex].strWord = chrWord[0];
                stWord[numIndex].strSpell2 = chrWord[1].ToString();
                numCount2++;
            }
            srFile.Close();
            b_LoadData = true;
            return true;
        }
        private static string funFindMulti(int numIndex, char ch1, char ch2)
        {
            int numPos;
            string strWord;
            strWord = "|" + ch1 + ch2 + "/";
            numPos = stWord[numIndex].strExt.IndexOf(strWord);
            if (numPos >= 0)
                return stWord[numIndex].strExt.Substring(numPos + strWord.Length, 1);
            return "";
        }
        /// 
        /// Öú¼ÇÂë(Æ´Òô)
        /// 
        /// 
        /// 
        /// 
        public static string funChineseSpell(string strChinese, bool IsName)
        {
            if (string.IsNullOrWhiteSpace(strChinese))
            {
                return string.Empty;
            }
            string strSpell, strThis;
            int i, numCount, numIndex;
            char[] chrWord;
            strSpell = "";
            chrWord = strChinese.ToCharArray();
            numCount = strChinese.Length;
            //2010-03-19 ÒÀ´Î´¦Àí¸÷ºº×ֵį´ÒôÂë
            for (i = 0; i < numCount; i++)
            {
                numIndex = (int)chrWord[i];
                if (numIndex <= 0) continue;
                if (numIndex >= INTMAX) continue;
                //2010-03-19 ÊÇ·ñ¶àÒô×Ö
                strThis = "";
                if (stWord[numIndex].numFlag > 0 && !IsName)
                {
                    if (i > 0)
                    {
                        strThis = funFindMulti(numIndex, chrWord[i - 1], chrWord[i]);
                    }
                    if (strThis.Length == 0)
                    {
                        if (i < numCount - 1)
                            strThis = funFindMulti(numIndex, chrWord[i], chrWord[i + 1]);
                    }
                }
                //2010-04-02 °´ÐÕÃû½øÐд¦Àí
                if (IsName && i == 0 && stWord[numIndex].strName.Length > 0)
                    strThis = stWord[numIndex].strName;
                //2010-03-20 δÕÒ×ÅʱÊDZê×¼Âë
                if (strThis.Length == 0)
                    strThis = stWord[numIndex].strSpell1.ToString();
                strSpell = strSpell + strThis;
            }
            return strSpell;
        }
        /// 
        /// Öú¼ÇÂë(Îå±Ê)
        /// 
        /// 
        /// 
        public string funWbzxSpell(string strChinese)
        {
            string strSpell = "";
            int i, numCount, numIndex;
            char[] chrWord;
            strSpell = "";
            chrWord = strChinese.ToCharArray();
            numCount = strChinese.Length;
            //2010-03-19 ÒÀ´Î´¦Àí¸÷ºº×ÖµÄÎå±Ê±àÂë
            for (i = 0; i < numCount; i++)
            {
                numIndex = (int)chrWord[i];
                if (numIndex <= 0) continue;
                if (numIndex >= INTMAX) continue;
                strSpell = strSpell + stWord[numIndex].strSpell2.ToString();
            }
            return strSpell;
        }
    }
}