using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.Collections;
using System.IO;
using GCGame.Table;

public class RechargeTableToStr : ScriptableObject
{
    private static Vector3 position;
    private static Quaternion rotation;
    //private static Vector3 scale;

    [MenuItem("ProTool/Table/RechargeTabToStr")]
    public static void RechargeTabToStr()
    {
        string writePath = Application.dataPath + "/Editor/ZhanyouTool/RechargeTableStr.txt";
        string readPath = Application.dataPath + "/Project/BundleData/Tables/Recharge.txt";


        StreamReader tableReader = new StreamReader(readPath);
        StreamWriter tableWrite = new StreamWriter(writePath);
        tableReader.ReadLine();
        tableReader.ReadLine();
        tableReader.ReadLine();
        tableReader.ReadLine();
        string writeLine = "";
        while (!tableReader.EndOfStream)
        {
            string line = tableReader.ReadLine();
            string[] lineSplit = line.Split('\t');
            if (lineSplit.Length >= 7)
            {
                writeLine = "+\"{";
                writeLine += "\\\"goods_id\\\"" + " = " + lineSplit[0] + ";";
                writeLine += "\\\"goods_describe\\\"" + " = " + lineSplit[1] + ";";
                writeLine += "\\\"goods_register_id\\\"" + " = " + lineSplit[2] + ";";
                writeLine += "\\\"goods_icon\\\"" + " = " + lineSplit[4] + ";";
                writeLine += "\\\"goods_number\\\"" + " = " + lineSplit[5] + ";";
                writeLine += "\\\"goods_price\\\"" + " = " + "10" + ";";
                writeLine += "}\"";
                tableWrite.WriteLine(writeLine);
            }
        }

        tableReader.Close();
        tableWrite.Close();
    }

    private static string[] MySplit(string str, string[] nTypeList, string regix)
    {
        if (string.IsNullOrEmpty(str))
        {
            return null;
        }
        string[] content = new string[nTypeList.Length];
        int nIndex = 0;
        int nstartPos = 0;
        while (nstartPos <= str.Length)
        {
            int nsPos = str.IndexOf(regix, nstartPos);
            if (nsPos < 0)
            {
                string lastdataString = str.Substring(nstartPos);
                if (string.IsNullOrEmpty(lastdataString) && nTypeList[nIndex].ToLower() != "string")
                {
                    content[nIndex++] = "--";
                }
                else
                {
                    content[nIndex++] = lastdataString;
                }
                break;
            }
            else
            {
                if (nstartPos == nsPos)
                {
                    if (nTypeList[nIndex].ToLower() != "string")
                    {
                        content[nIndex++] = "--";
                    }
                    else
                    {
                        content[nIndex++] = "";
                    }
                }
                else
                {
                    content[nIndex++] = str.Substring(nstartPos, nsPos - nstartPos);
                }
                nstartPos = nsPos + 1;
            }
        }
        return content;
    }

    [MenuItem("ProTool/Table/CheckTable1")]
    public static void CheckTable1()
    {
        string m_Key = "";
        string[] Values = null;

        Object selectObj = Selection.activeObject;
        TextAsset testAsset = selectObj as TextAsset;
        string[] alldataRow;
        alldataRow = testAsset.text.Split('\n');
        //skip fort three
        int skip = 0;
        string[] typeList = null;
        string[] typeNames = null;
        foreach (string line in alldataRow)
        {
            int nKey = -1;
            if (skip == 0)
            {
                string sztemp = line;
                if (sztemp.Length >= 1)
                {
                    if (sztemp[sztemp.Length - 1] == '\r')
                    {
                        sztemp = sztemp.TrimEnd('\r');
                    }
                }
                typeNames = line.Split('\t');
                ++skip;
                continue;
            }

            if (skip == 1)
            {
                string sztemp = line;
                if (sztemp.Length >= 1)
                {
                    if (sztemp[sztemp.Length - 1] == '\r')
                    {
                        sztemp = sztemp.TrimEnd('\r');
                    }
                }
                typeList = line.Split('\t');
                Values = new string[typeList.Length];
                ++skip;
                continue;
            }
            if (++skip < 4) continue;
            if (string.IsNullOrEmpty(line)) continue;
            if (line[0] == '#') continue;
            string szlinetemp = line;
            if (szlinetemp.Length >= 1)
            {
                if (szlinetemp[szlinetemp.Length - 1] == '\r')
                {
                    szlinetemp = szlinetemp.TrimEnd('\r');
                }
            }
            string[] strCol = MySplit(szlinetemp, typeList, "\t");
            if (strCol.Length == 0) continue;
            string skey = strCol[0];
            string[] valuesList = new string[strCol.Length];

            if (string.IsNullOrEmpty(skey) || skey.Equals("--"))
            {
                skey = m_Key;
                nKey = int.Parse(skey);
                valuesList[0] = skey;
                for (int i = 1; i < strCol.Length; ++i)
                {
                    if (string.IsNullOrEmpty(strCol[i]) || strCol[i] == "--")
                    {
                        valuesList[i] = Values[i];
                    }
                    else
                    {
                        valuesList[i] = strCol[i];
                        Values[i] = strCol[i];
                    }
                }

            }
            else
            {
                m_Key = skey;
                nKey = int.Parse(skey);

                for (int i = 0; i < strCol.Length; ++i)

                {
                    if (strCol[i] == "--")
                    {
                        valuesList[i] = "0";
                        Values[i] = "0";
                    }
                    else
                    {
                        valuesList[i] = strCol[i];
                        Values[i] = strCol[i];
                    }
                }

                for (int i = 0; i < typeList.Length; i++)
                {
                    bool isError = false;
                    if (typeList[i].Contains("INT") || typeList[i].Contains("BOOL"))
                    {
                        string value = Values[i];
                        int outValue = 0;
                        if (int.TryParse(value, out outValue) == false)
                        {
                            isError = true;
                        }
                    }
                    if (typeList[i].Contains("FLOAT"))
                    {
                        string value = Values[i];
                        float outValue = 0;
                        if (float.TryParse(value, out outValue) == false)
                        {
                            isError = true;
                        }
                    }
                    if (typeList[i].Contains("SHORT"))
                    {
                        string value = Values[i];
                        short outValue = 0;
                        if (short.TryParse(value, out outValue) == false)
                        {
                            isError = true;
                        }
                    }
                    if (isError)
                    {
                        Debug.Log(string.Format("key = {0}          index = {1}          typeName = {2}", nKey, i, typeNames[i]));
                    }
                }
            }
        }
    }

    [MenuItem("ProTool/Table/CheckTable")]
    public static void CheckTable()
    {
        var strDicts = TableManager.GetStrDictionary();
        var strFilts = TableManager.GetStrFilter();

        int filt1 = 0;
        foreach (var strDict in strDicts)
        {
            foreach (var filter in strFilts.Values)
            {
                if (null != filter)
                {
                    //找到了
                    foreach(var item in strDict.Value)
                    {
                        if (item.StrDictionary.IndexOf(filter.SzString) != -1)
                        {
                            ++filt1;
                            Debug.LogError("字典和谐:" + strDict.Key.ToString() + "," + filter.SzString + "," + item.StrDictionary);
                        }
                    }
                }
            }
        }
        if (filt1 == 0)
            Debug.Log("字典没有和谐字");
    }
}