using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using System.Threading;
using TMPro;
using Unity.VisualScripting;
using UnityEditor;

public class mainGameView : MonoBehaviour
{
    private gameIdJsonData gameidJsondata = null;
    private string progresName = "";
    public GameObject lineImg = null;//线图
    public GameObject colorImg = null;//颜色图
    public GameObject lowImg = null;//底图
    public GameObject areaIdBase = null;
    [Header("区域id的预制体")]
    public string perfabPath = "";
    private GameObject areaIdperfabObj = null;
    public DigitMatrixShort digitMatrix = null;
    public float minShowFontSize = 20f;
    public float showFontSizeMax = 30f;
    protected Texture2D lineTexture2d = null;
    private Texture2D lowImgTexture2d = null;
    private Texture2D colorTexure2d = null;
    protected short nowSelectgid = -1;
    private bool fillAnimation = true;
    private bool thumbnailIsChange = false;
    private string[] initDigitMatrixs = { "", "","" };
    private Dictionary<short, short> aid2selectOrderIdx = new Dictionary<short, short>();
    private GameScenUserInfo gameScenUseInfo = null;
    //x,y aid 当前帧向量 起点终点 等宽度计算扩散
    private List<AreaAnimationDiffuse> showAreaAnimationDatas = new List<AreaAnimationDiffuse>();
    private Dictionary<short, GroupByAreaPtInfos> groupPtInfos = new Dictionary<short, GroupByAreaPtInfos>();
    private Dictionary<short, GroupByAreaPtInfos> groupPtInfos_All = new Dictionary<short, GroupByAreaPtInfos>();//不管是否涂色都会记录
    private short bianlun = -1;
    private short fullShow = -2;
    private Texture2D thumbnailTexture2d = null;
    private bool isDebugTestToEnd = false;
    public GameObject clickPosEff = null;
    Dictionary<string, Color[]> allColor = new Dictionary<string, Color[]>();
    [SerializeField]
    private int selectTextureIdx = 1;
    static private string[] allSelectTexturePaths = {
        "scenes/game/cells/buleCell",
        "scenes/game/cells/gray",
        "scenes/game/cells/grayCell"
    };
    private ColorIndexTables nowSelectColors = null;
    public int SelectTextureIdx
    {
        get { return selectTextureIdx; }
        set { 
            if(value >= 0 &&  value < allSelectTexturePaths.Length)
            {
                if (selectTextureIdx == value) return;
                selectTextureIdx = value;
                //修改
                loadSelectImageTextureId(value);
            }
        }
    }
    
    public void loopAddTaskBack(int count,int step,TaskItemBack back, System.Object[] param = null)
    {
        List<System.Object> newParams = null;
        if(param != null) 
            newParams = param.ToList();
        else
            newParams = new List<System.Object>();
        int idx1 = newParams.Count;
        newParams.Add(0);
        newParams.Add(0);
        System.Object[] newParamArray = newParams.ToArray();
        TaskManages tm = TaskManages.getInstance();
        if(count <= step)
        {
            newParamArray[idx1] = 0;
            newParamArray[idx1 + 1] = count;
            tm.addTaskArray(this, back, newParamArray);
            return;
        }
        for (int x = 0; ;)
        {
            newParamArray[idx1] = x;
            newParamArray[idx1 + 1] = step;
            tm.addTaskArray(this, back, newParamArray);
            x += step;
            if(x >= count - step)
            {
                if(x == (count + step))//最后整数
                {
                    return;
                }
                step = count - x;
                newParamArray[idx1] = x;
                newParamArray[idx1 + 1] = step;
                tm.addTaskArray(this, back, newParamArray);
                return;
            }
        }
    }
    public void onInitDigitMatrix(string name)
    {
        
        Debug.Log("onInitDigitMatrix >> " + name);
        if (lowImg == null && name.Equals("select") == false) 
            onInitDigitMatrix("select");
        for (int i = 0; i < initDigitMatrixs.Count(); i++)
        {
            if (initDigitMatrixs[i].Equals(""))
            {
                initDigitMatrixs[i] = name;
                break;
            }
            else if (initDigitMatrixs[i].Equals(name)) return;
        }
        if (initDigitMatrixs[1].Equals("") == false && initDigitMatrixs[2].Equals("")){
            if (thumbnailTexture2d == null)
            {
                onInitDigitMatrix("thumbnail");
                return;
            }
            utilsTools.setGDataByKey("makeThumbnailBegin", utilsTools.getTotalMillisecond());
            var tm = TaskManages.getInstance();
            if(allColor.ContainsKey("line") == false)
                allColor.Add("line", lineTexture2d.GetPixels(0, 0, gameidJsondata.width, gameidJsondata.height));
            else
                allColor.Add("line", lineTexture2d.GetPixels(0, 0, gameidJsondata.width, gameidJsondata.height));
            TaskItemBack taskItemBack = (System.Object[] param) =>
            {
                int start = (int)param[0];
                int step = (int)param[1];
                float x = 1.0f;
                float y = 1.0f;
                float width = gameidJsondata.width;
                float height = gameidJsondata.height;
                int a1 = utilsTools.getTotalMillisecond();
                utilsTools.sendEventObjectParams("inGameLoadingValue", 6f, false);
                
                Color[] colors = thumbnailTexture2d.GetPixels(0, 0, gameidJsondata.width, gameidJsondata.height);
                for(int y1 = 0;y1 < gameidJsondata.height; y1++)
                {
                    y = y1;
                    for(int x1 = start;x1 < (start + step); x1++)
                    {
                        x = x1;
                        int index = y1 * gameidJsondata.width + x1;
                        short nowValue = digitMatrix.Data[x1,y1];
                        if (nowValue.Equals(bianlun) || nowValue < 0)
                        {
                            var color = colorTexure2d.GetPixelBilinear(x / width, y / height);
                            color.a = 1f;
                            colors[index] = color;

                        }
                        if(nowValue.Equals(fullShow))
                        {
                            Color color1 = allColor["line"][index];
                            color1.a = 0f;
                            allColor["line"][index] = color1;
                        }
                        
                    }
                }
                thumbnailTexture2d.SetPixels(start, 0, step, gameidJsondata.height,colors);
                int diff = utilsTools.getTotalMillisecond() - a1;
                Debug.Log("*****---- makeThumbnail " + start.ToString() + " diff = " + diff.ToString());
            };
            loopAddTaskBack(gameidJsondata.width,340,taskItemBack);
            tm.addTask(this, (System.Object[] param) =>
            {
                onInitDigitMatrix("thumbnail");
                lineTexture2d.SetPixels(allColor["line"]);
                lineTexture2d.Apply();
                //utilsTools.saveByteToPath(utilsTools.ScaleTexture(thumbnailTexture2d, 120, 120).EncodeToPNG(), gameScenUseInfo.getLocalThumbnailPath() + "_bk.png");
            });
            return;
        }
        if(name.Equals("thumbnail"))
            utilsTools.setGDataByKey("makeThumbnailEnd", utilsTools.getTotalMillisecond());
        if (initDigitMatrixs[initDigitMatrixs.Length - 1].Equals("")) return;
        
        nowSelectgid = -1;
        resetSize();
        onInitEndHandler();
        if (thumbnailTexture2d != null) {
            utilsTools.addTimer_Update(gameObject, "shouhuThumbnailIsChange", (String _) =>
            {
                if (thumbnailIsChange == true)
                {
                    thumbnailIsChange = false;
                    thumbnailTexture2d.SetPixels(allColor["thumbnail"]);
                    string path = gameScenUseInfo.getLocalThumbnailPath();
                    var sltTextureSize = utilsTools.getGDataByKey<Vector2>("sltTextureSize", Vector2.one);
                    //utilsTools.saveByteToPath(utilsTools.ScaleTexture(thumbnailTexture2d, Convert.ToInt32(sltTextureSize.x), Convert.ToInt32(sltTextureSize.y)).EncodeToJPG(), path);
                    //utilsTools.saveByteToPath(thumbnailTexture2d.EncodeToJPG(), path);
                }
                return true;
            }, 120, 0);
        }
    }
    protected string getSelectPathByGid(int gid)
    {
        return gameScenUseInfo.getPathByFormath("groupId_{0}.png",gid);
    }
    public void saveGroupTexture(int pgid,Texture2D nowGroupIdTexture,bool isDestroy = true)
    {
        string temppath = getSelectPathByGid(pgid);
        utilsTools.saveByteToPath(nowGroupIdTexture.EncodeToPNG(), temppath);
        utilsTools.saveImageDefaultMetaInfoToPath(temppath);
        if (isDestroy)
        {
            GameObject.Destroy(nowGroupIdTexture);
            nowGroupIdTexture = null;
        }
    }
    public virtual short[] getDiffAids()
    {
        List<short> ret = new List<short>();
        Dictionary<short,bool> keyValuePairs = new Dictionary<short,bool>();
        foreach (var item in gameScenUseInfo.areaInfoUseData)
        {
            if (keyValuePairs.ContainsKey(item.id))
            {
                Debug.Log("重复----" + item.id);
            }
            keyValuePairs.Add(item.id, true);
        }
        foreach (var item in gameidJsondata.roomDatas)
        {
            if(keyValuePairs.ContainsKey(item.id) == false)
            {
                ret.Add(item.id);
            }
        }
        return ret.ToArray();
    }
    //将区域id 离散xy 坐标存储为 连续数组
    public void idHashPosToArrayPos()
    {
        Dictionary<short, short> aidToIndex = new Dictionary<short, short>();
        Dictionary<short, List<int>> AidVersions2 = new Dictionary<short, List<int>>();
        GroupByAreaPtInfos.width = gameidJsondata.width;
        GroupByAreaPtInfos.height = gameidJsondata.height;
        int t1 = utilsTools.getTotalMillisecond();
        Dictionary<short, RectInt> aidByRect = fillAnimation ? new Dictionary<short, RectInt>() : null;
        float maxWidth = (float)(gameidJsondata.width + 1f);
        float maxHeight = (float)(gameidJsondata.height + 1f);
        RectInt model = new RectInt();
        for (int i = 0; i < gameidJsondata.roomGroupsData.Count; i++)
        {
            short gid = gameidJsondata.roomGroupsData[i].id;
            GroupByAreaPtInfos groupByAreaPtInfos = new GroupByAreaPtInfos();
            foreach (var item in gameidJsondata.roomGroupsData[i].roomIds)
            {
                groupByAreaPtInfos.initByAid(item);
                short fid = (short)gameidJsondata.findAid(item);
                aidToIndex.Add(item, fid);
                var temp2 = new List<int>(2400);
                AidVersions2.Add(item, temp2);
                if (aidByRect != null)
                {
                    aidByRect.Add(item, new RectInt(model));
                }
            }
            groupPtInfos_All.Add(gid, groupByAreaPtInfos);
        }
        int cout = 0;
        int cout1 = 0;
        for (short x = 0; x < digitMatrix.RowCount; x++)
        {
            for (short y = 0; y < digitMatrix.ColumnCount; y++)
            {
                cout++;
                short cell = digitMatrix.Data[x, y];
                if (cell < 20 || false == aidToIndex.ContainsKey((short)(cell-20)))
                {
                    continue;
                }
                else
                {
                    short aid = (short)(cell - 20);
                    cout1++;
                    AidVersions2[aid].Add(y * digitMatrix.RowCount + x);
                    if(aidByRect != null)
                    {
                        RectInt rectInt = aidByRect[aid];
                        if (rectInt.left > x) rectInt.left = x;
                        if(rectInt.right < x) rectInt.right = x;
                        if(rectInt.top > y) rectInt.top = y;
                        if(rectInt.dowm < y) rectInt.dowm = y;
                    }
                }
            }
        }
        GroupByAreaPtInfos.mapAllDatas = AidVersions2;
        groupPtInfos = groupPtInfos_All;
        for (int i = 0; i < gameidJsondata.roomGroupsData.Count; i++)
        {
            short gid = gameidJsondata.roomGroupsData[i].id;
            var groupItem = groupPtInfos_All[gid];
            foreach (var item in gameidJsondata.roomGroupsData[i].roomIds)
            {
                if (aid2selectOrderIdx.ContainsKey(item) == true)
                {
                    groupPtInfos[gid].removeAid(item);
                }
            }
        }
    }
    public void onLineColorHandlerEnd()
    {
        if (colorTexure2d == null)
        {
            utilsTools.addTimer_Update(gameObject, "waitColorBack", (String name) =>
            {
                onLineColorHandlerEnd();
                return colorTexure2d != null;
            }, 5, 1000);
        }
        Color[] colors = lineTexture2d.GetPixels(0, 0, lineTexture2d.width, lineTexture2d.height);
        List<Vector2Short> temp = null;
        foreach (var item in aid2selectOrderIdx)
        {
            var aidData = gameidJsondata.getAid(item.Key);
            if (groupPtInfos_All[aidData.gid].getDataByAid(item.Key, ref temp) && temp.Count > 0)
            {
                foreach (var item1 in temp)
                {
                    int idx = item1.y * lineTexture2d.width + item1.x;
                    Color color = colors[idx];
                    color.a = 0f;
                    digitMatrix.Data[item1.x, item1.y] = fullShow;
                    colors[idx] = color;
                }
                groupPtInfos_All[aidData.gid].getLineByAid(item.Key, ref temp);
                foreach (var item1 in temp)
                {
                    int idx = item1.y * lineTexture2d.width + item1.x;
                    Color color = colors[idx];
                    color.a = 0f;
                    colors[idx] = color;
                    digitMatrix.Data[item1.x, item1.y] = fullShow;
                }
            }
        }
        lineTexture2d.SetPixels(0, 0, lineTexture2d.width, lineTexture2d.height, colors);
        lineTexture2d.Apply();
        TaskManages.getInstance().addTask(this, (System.Object[] objects) =>
        {
            utilsTools.setGDataByKey("lineHandlerEnd", utilsTools.getTotalMillisecond());
            lineTexture2d.Apply();
            onInitDigitMatrix("line");
        });
    }
    public void onLoadFileOk(int typeId)
    {
        if (typeId == 1)
        {
            lineTexture2d = lineImg.GetComponent<Image>().sprite.texture;
            TaskManages taskManages = TaskManages.getInstance();
            TaskItemBack taskItemBack = (System.Object[] datas) =>
            {
                int height = (int)datas[0];
                int x = (int)datas[1];
                int setp = (int)datas[2];

                utilsTools.sendEventObjectParams("inGameLoadingValue", 4f, false);
                Color[] colors = lineTexture2d.GetPixels(x, 0, setp, height);
                int idx = 0;
                bool isbackWrite = false;
                for (int y = 0; y < height; y++)
                {
                    for (int x1 = x; x1 < (x + setp); x1++)
                    {
                        Color color = colors[idx];
                        if (color.a == 0)
                        {
                            colors[idx] = Color.white;
                            isbackWrite = true;
                        }
                        idx++;
                    }
                }
                if (isbackWrite)
                {
                    lineTexture2d.SetPixels(x, 0, setp, height, colors);
                }
            };
            System.Object[] objects = { lineTexture2d.height };
            //这个循环是为了修复颜色
            loopAddTaskBack(lineTexture2d.width, 200, taskItemBack, objects);
            taskManages.addTask(this, (System.Object[] a) => {
                utilsTools.setGDataByKey("initFanHong_1", utilsTools.getTotalMillisecond());
            });
            utilsTools.addTimer_Update(gameObject, "waitJsonBack", (String n) =>
            {
                if (gameidJsondata == null) return true;
                    
                taskManages.addTask(this, (System.Object[] a) =>
                {
                    utilsTools.setGDataByKey("beginIdPosToArray", utilsTools.getTotalMillisecond());
                    //连续分组
                    idHashPosToArrayPos();
                    utilsTools.setGDataByKey("endIdPosToArray", utilsTools.getTotalMillisecond());
                    //是否需要阻塞等待彩色图
                    onLineColorHandlerEnd();
                });
                return false;
            }, 5, 100000);
        }
        else if(typeId == 4)
        {
            TaskManages tm = TaskManages.getInstance();
            TaskItemBack checkPtNextHandler = null;
            checkPtNextHandler = (System.Object[] listparam) =>
            {
                if(digitMatrix == null)
                {
                    tm.addTask(this, checkPtNextHandler, listparam);
                    return;
                }
                
                utilsTools.sendEventObjectParams("inGameLoadingValue", 5f, false);
                gameidJsondata.roomGroupsData.Sort(gameAreaGroupItem.sortByIsComplete);
                if (areaIdBase != null && perfabPath.Equals("") == false)
                {
                    //创建一堆节点
                    TaskItemBack onUpdateByIdNode = (System.Object[] listparam) =>
                    {
                        utilsTools.setGDataByKey("makeAreaIdBegin", utilsTools.getTotalMillisecond());
                        utilsTools.sendEventObjectParams("inGameLoadingValue", 5f, false);
                        checkAddAreaId();
                        utilsTools.setGDataByKey("makeAreaIdEnd", utilsTools.getTotalMillisecond());
                        onInitDigitMatrix("select");
                        
                    };
                    tm.addTask(this, onUpdateByIdNode);
                }
                else
                {
                    onInitDigitMatrix("select");
                }
            };
            tm.addTask(this, checkPtNextHandler);
            
        }else if(typeId == 2)
        {
            if(areaIdBase != null)
            {
                colorTexure2d = colorImg.GetComponent<Image>().sprite.texture;
                thumbnailTexture2d = new Texture2D(gameidJsondata.width, gameidJsondata.height, TextureFormat.RGB24,false);
                allColor.Add("color",colorTexure2d.GetPixels(0,0,colorTexure2d.width,colorTexure2d.height));
            }
        }
    }
    protected void checkAddAreaId()
    {
        float width = gameidJsondata.width;
        float height = gameidJsondata.height;
        Vector3 localPosTemp = new Vector3();
        Quaternion quaternion = new Quaternion();
        var areaIdBaseTransform = areaIdBase.transform.GetComponent<RectTransform>();
        Vector3 vector3 = areaIdBase.transform.localScale;
        for (int i = 0; i < gameidJsondata.roomDatas.Count; i++)
        {
            var item = gameidJsondata.roomDatas[i];
            if (digitMatrix.Data[item.px, item.py] >= 20)
            {
                item.tempExKey = "idFlg_" + item.gid.ToString() + "_"+item.id.ToString();
                //转成物体坐标系?
                Transform idValueTran = areaIdBase.transform.Find(item.tempExKey);
                if (idValueTran == null)
                {
                    if (areaIdperfabObj == null)
                    {
                        areaIdperfabObj = Resources.Load<GameObject>(perfabPath);
                    }
                    GameObject idGameObject = Instantiate(areaIdperfabObj);
                    idValueTran = idGameObject.transform;
                    idValueTran.parent = areaIdBase.transform;
                    idGameObject.name = item.tempExKey;
                }
                //怎么转换坐标
                RectTransform rectTransform = areaIdBaseTransform;
                //坐标怎么设置？
                idValueTran.GetLocalPositionAndRotation(out localPosTemp, out quaternion);
                //严格居中 字体大小等问题 需要调整json的生成脚本生成逻辑后才能继续
                localPosTemp.x = (float)item.px / (float)width * rectTransform.rect.width - (rectTransform.rect.width / 2) * vector3.x;
                localPosTemp.y = (float)item.py / (float)height * rectTransform.rect.height - (rectTransform.rect.height / 2) * vector3.y;
                idValueTran.SetLocalPositionAndRotation(localPosTemp, quaternion);
                TextMeshProUGUI textMesh = idValueTran.GetComponent<TextMeshProUGUI>();
                textMesh.text = (item.gid).ToString();
                int len = GroupByAreaPtInfos.mapAllDatas[item.id].Count;
                float temp = len * vector3.x;
                float fontSize = item.pr;
                if (fontSize > showFontSizeMax) fontSize = showFontSizeMax;
                if (temp < minShowFontSize)
                {
                    idValueTran.gameObject.SetActive(false);
                }
                else
                {
                    textMesh.fontSize = fontSize;
                    idValueTran.gameObject.SetActive(true);
                }
            }
        }
    }
    public void updateScaleView()
    {
        Vector3 vector3 = lineImg.transform.localScale;
        float dyMinShowFontSize = utilsTools.getGDataByKey<float>("dyMinShowFontSize", minShowFontSize);
        if (areaIdBase != null && perfabPath.Equals("") == false)
        {
            //倍数怎么弄
            for (int i = 0; i < gameidJsondata.roomDatas.Count; i++)
            {
                var item = gameidJsondata.roomDatas[i];
                if (item.tempExKey.Equals("") == false)
                {
                    Transform idValueTran = areaIdBase.transform.Find(item.tempExKey);
                    if (idValueTran == null) continue;
                    float fontSize = item.pr * vector3.x;
                    if(fontSize < minShowFontSize)
                    {
                        idValueTran.gameObject.SetActive(false);
                    }
                    else
                    {
                        idValueTran.gameObject.SetActive(true);
                    }
                }
            }
        }
    }
    protected void onInitEndHandler()
    {
        Debug.Log("初始化完成 -----------------");
        utilsTools.sendEvent("gameViewOnInitEnd");
        if(lowImg == null)
        {
            utilsTools.sendEventObjectParams("inGameLoadingValue", 100f, true);
        }
        if(clickPosEff != null)
        {
            GameObject go = Instantiate(clickPosEff);
            go.transform.SetParent(areaIdBase.transform);
            GameClickPosEffect gameClickPosEffect = go.GetComponent<GameClickPosEffect>();
            gameClickPosEffect.runR = 90f;
            gameClickPosEffect.maxDotR = 25f;
            go.transform.name = "clickPosEff";
            gameClickPosEffect.initMember();
            go.SetActive(false);
        }
        
    }
    public void loadFileInfo(string url,string localpath,int typeId)
    {
        if (typeId == 1 || typeId == 2)
        {
            
            utilsTools.httpDOWNIMG(this, url, localpath, (string id, string url1, bool isSuccess, System.Object data) =>
            {
                if(isSuccess == false)
                {
                    utilsTools.addTimer_Sec(gameObject, url1, (string name1) =>
                    {
                        loadFileInfo(url1, localpath, typeId);
                        return false;
                    },5);
                    return 0;
                }
                else
                {
                    utilsTools.saveImageDefaultMetaInfoToPath(localpath);
                    utilsTools.sendEventObjectParams("inGameLoadingValue", 8f, false);
                    Image image = null;
                    if(typeId == 1)
                    {
                        image = this.lineImg.GetComponent<Image>();
                    }else if(typeId == 2)
                    {
                        utilsTools.setGDataByKey("loadJpgBack", utilsTools.getTotalMillisecond());
                        image = this.colorImg.GetComponent<Image>();
                    }
                    Texture2D texture2d = data as Texture2D;
                    image.sprite = Sprite.Create(texture2d, new Rect(0, 0, texture2d.width, texture2d.height), new Vector2(0.5f, 0.5f));
                    if(lowImg != null && typeId == 1)
                    {
                        DateTime dt1 =  DateTime.UtcNow;
                        utilsTools.setGDataByKey("loadGaoQingBack", utilsTools.getTotalMillisecond());
                        loadSelectImageTextureId(selectTextureIdx, () =>
                        {
                            Debug.Log("加载选中图并设置Ok4 sec = " + (DateTime.UtcNow - dt1).TotalSeconds.ToString());
                            utilsTools.setGDataByKey("selectMember", utilsTools.getTotalMillisecond());
                            onLoadFileOk(4);
                            
                        });
                    }
                    onLoadFileOk(typeId);
                    return 1;
                }
            });
        }else if(typeId == 3)
        {
            utilsTools.httpDownText(this, url, localpath, (string id, string url1, bool isSuccess, System.Object data) =>
            {
                if(isSuccess == false)
                {
                    utilsTools.addTimer_Sec(gameObject, url1, (string name1) =>
                    {
                        loadFileInfo(url1, localpath, typeId);
                        return false;
                    }, 5);
                    return 0;
                }
                else
                {
                    utilsTools.setGDataByKey("downJsonBack", utilsTools.getTotalMillisecond());
                    utilsTools.sendEventObjectParams("inGameLoadingValue", 8f, false);
                    string jsondata = data as string;
                    gameIdJsonData gameidJsondata1 = JsonUtility.FromJson<gameIdJsonData>(jsondata);
                    
                    int centerSize = gameidJsondata1.roomDatas.Count();
                    foreach (var item in aid2selectOrderIdx)
                    {
                        gameidJsondata1.roomDatas[item.Key].isShow = true;
                    }
                    for(int i = 0;i < gameidJsondata1.roomGroupsData.Count(); i++)
                    {
                        var group = gameidJsondata1.roomGroupsData[i];
                        group.isComplete = true;
                        for (int j = 0; j < group.roomIds.Count(); j++)
                        {
                            if (gameidJsondata1.roomDatas[group.roomIds[j]].isShow == false)
                            {
                                group.isComplete = false;
                                break;
                            }
                        }
                    }
                    utilsTools.sendEventObjectParams("inGameLoadingValue", 2f, false);
                    for (int i = 0; i < gameidJsondata1.roomGroupsData.Count; i++)
                    {
                        var group = gameidJsondata1.roomGroupsData[i];
                        for (int i1 = 0; i1 < group.roomIds.Count(); i1++)
                        {
                            gameidJsondata1.roomDatas[group.roomIds[i1]].gid = group.id;
                        }
                    }
                    this.gameidJsondata = gameidJsondata1;
                    utilsTools.setGDataByKey("allAreaSize", gameidJsondata1.getAllValidAreaItemSize());
                    utilsTools.setGDataByKey("downJsonAsynTask", utilsTools.getTotalMillisecond());

                    onLoadFileOk(typeId);
                    return 0;
                }
            });
        }
    }
    private void bytesInitDigitMatrix(Byte[] bytes)
    {
        int start = 0;
        int step = BitConverter.GetBytes((short)2).Length;
        int stepInt = BitConverter.GetBytes(65539).Length;
        short width = BitConverter.ToInt16(bytes, start);
        start += step;
        short height = BitConverter.ToInt16(bytes, start);
        DigitMatrixShort digitMatrixBK = new DigitMatrixShort(width, height);
        for (short i = 0; i < width; i++)
        {
            for(short j = 0; j < height; j++)
            {
                start += step;
                short temp = BitConverter.ToInt16(bytes, start);
                short aid = (short)(temp - 20);
                if (aid2selectOrderIdx.ContainsKey(aid))
                {
                    temp = fullShow;
                }
                digitMatrixBK.Data[i, j] = temp;
            }
        }
        digitMatrix = digitMatrixBK;
    }
    public bool loadGameImgId(GameScenUserInfo gameUserInfo)
    {
        string linePath = gameUserInfo.getLineImgLocalPath();
        string colorPath = gameUserInfo.getColorImgLocalPath();
        string jsonPath = gameUserInfo.getJsonLocalPath();
        string thumbnail = gameUserInfo.getLocalThumbnailPath();
        if(jsonPath.Equals("") || linePath.Equals("") || colorPath.Equals("") || thumbnail.Equals(""))
        {
            return false;
        }
        if (false == gameUserInfo.createWorkDirectory())return false;
        
        utilsTools.sendEventObjectParams("inGameLoadingValue", 3f, false);
        fillAnimation = Boolean.Parse(utilsTools.getGDataByKey<String>("fillanimation", "false"));
        int selectId = int.Parse(utilsTools.getGDataByKey<String>("highlight_areas", "0"));
        if(selectId < 0 || selectId>= allSelectTexturePaths.Count())
        {
            selectId = 0;
        }
        selectTextureIdx = selectId;
        this.gameScenUseInfo = gameUserInfo;
        for (short i = 0; i < gameUserInfo.areaInfoUseData.Count; i++)
        {
            var userAreaData = gameUserInfo.areaInfoUseData[i];
            aid2selectOrderIdx.Add(userAreaData.id, i);
        }
        utilsTools.setGDataByKey("loadBinBegin", utilsTools.getTotalMillisecond());
        TaskManages.getInstance().insertTask(this, (System.Object[] _) =>
        {
            string binpath = colorPath.Substring(0, linePath.Length - 4) + ".bin";
            if(utilsTools.IsPhone == false)
            {
                binpath = binpath.Replace("/", "\\");
            }
            Byte[] bytes = File.ReadAllBytes(binpath);
            bytesInitDigitMatrix(bytes);
            Debug.Log("bin初始化完成--------------");
        });
        utilsTools.setGDataByKey("loadBinEnd", utilsTools.getTotalMillisecond());
        utilsTools.setGDataByKey("downJson", utilsTools.getTotalMillisecond());
        loadFileInfo(gameUserInfo.json, jsonPath, 3);
        utilsTools.setGDataByKey("loadGaoQing", utilsTools.getTotalMillisecond());
        loadFileInfo(gameUserInfo.img,linePath, 1);
        utilsTools.setGDataByKey("loadJpg", utilsTools.getTotalMillisecond());
        loadFileInfo(gameUserInfo.finishImg, colorPath, 2);
        return true;
    }
    public bool loadGameImgId()
    {
        GameScenUserInfo data = utilsTools.getGDataByKey<GameScenUserInfo>("nowSelectUseData", null);
        if(data == null) return false;
        if (this.gameScenUseInfo != null)
        {
            if (gameScenUseInfo.id.Equals(data.id))
            {
                return true;
            }
        }
        return loadGameImgId(data);
    }
    //重新加载线图
    public void reLoadLineTexture(LoadImageInfo back)
    {
        if (thumbnailTexture2d != null) Destroy(thumbnailTexture2d);
        thumbnailTexture2d = utilsTools.ScaleTexture(colorTexure2d, colorTexure2d.width, colorTexure2d.height, TextureFormat.RGBA32);
        lineImg.SetActive(true);
        Image image = lineImg.GetComponent<Image>();
        utilsTools.loadLocalImage(this, image, gameScenUseInfo.getLineImgLocalPath(), (bool isSuccess) => {
            if(isSuccess)
            {
                if(lineTexture2d !=null)
                {
                    Destroy(lineTexture2d);
                }
                lineTexture2d = image.sprite.texture;
                TaskItemBack taskItemBack = (System.Object[] param) =>
                {
                    int start = (int)param[0];
                    int step = (int)param[1];
                    float x = 1.0f;
                    float y = 1.0f;
                    float width = gameidJsondata.width;
                    float height = gameidJsondata.height;
                    int a1 = utilsTools.getTotalMillisecond();
                    utilsTools.sendEventObjectParams("inGameLoadingValue", 6f, false);
                    int index = 0;
                    Color[] colors = lineTexture2d.GetPixels(start, 0, step, gameidJsondata.height);
                    for (int y1 = 0; y1 < gameidJsondata.height; y1++)
                    {
                        y = y1;
                        for (int x1 = start; x1 < (start + step); x1++)
                        {
                            x = x1;
                            short nowValue = digitMatrix.Data[x1, y1];
                            if (nowValue.Equals(fullShow))
                            {
                                colors[index] = Color.white;
                            }
                            index++;
                        }
                    }
                    lineTexture2d.SetPixels(start, 0, step, gameidJsondata.height, colors);
                };
                loopAddTaskBack(gameidJsondata.width, 340, taskItemBack);
                TaskManages.getInstance().addTask(this, (System.Object[] param) =>
                {
                    lineTexture2d.Apply();
                    back(true);
                });
            }
            else
            {
                back(false);
            }
        });
    }
    public void showArea(int areaId, OnAreaHandler onAreaHandler = null)
    {
        if (areaId == -1)
        {
            lineImg.SetActive(false);
            return;
        }
        //显示指定区域
        if (digitMatrix !=null && lineTexture2d != null && gameidJsondata != null && areaId >= 0 && areaId < gameidJsondata.roomDatas.Count)
        {
            gameAreaItem areaItem1 = gameidJsondata.getAid((short)areaId);
            List<Vector2Short> areaAllPts = new List<Vector2Short>();
            Color color = new Color(1, 1, 1, 0);
            groupPtInfos_All[areaItem1.gid].getDataByAid(areaItem1.id, ref areaAllPts,true);
            foreach (var item in areaAllPts)
            {
                lineTexture2d.SetPixel(item.x,item.y, color);//回放使用
            }
            //groupPtInfos_All[areaItem1.gid].getLineByAid(areaItem1.id, ref areaAllPts);
            //foreach (var item in areaAllPts)
            //{
            //    lineTexture2d.SetPixel(item.x, item.y, color);
            //}
            lineTexture2d.Apply();
        }
        else if (onAreaHandler != null)
        {
            onAreaHandler(areaId, false);
        }
    }

    public bool switchShowAreaGroup(short gid, OnAreaHandler onAreaHandler = null)
    {
        //utilsTools.setGDataByKey("makeThumbnailBegin", utilsTools.getTotalMillisecond());
        if(utilsTools.ContainsKeyGDataByKey("firstSwitchShowAreaBegin") == false)
        {
            utilsTools.setGDataByKey("firstSwitchShowAreaBegin", utilsTools.getTotalMillisecond());
        }
        if (gid == nowSelectgid)
        {
            if(onAreaHandler != null)
                onAreaHandler(gid, true);            
            return true;
        }
        if (lowImg == null)
        {
            if (onAreaHandler != null)
                onAreaHandler(gid, false);
            return false;
        }
        if (!(digitMatrix != null && lineTexture2d != null && gameidJsondata != null && gameidJsondata.findGid(gid)))
        {
            if (onAreaHandler != null)
                onAreaHandler(gid, false);
            return false;
        }
        Color color = new Color(1, 1, 1, 0);
        gameAreaGroupItem jsonGroupInfo = null;
        GroupByAreaPtInfos groupPt = null;
        List<Vector2Short> tempData = new List<Vector2Short>();
        
        if(allColor.ContainsKey("low") == false)
        {
            allColor.Add("low", lowImgTexture2d.GetPixels(0, 0, lowImgTexture2d.width, lowImgTexture2d.height));
        }
        else
        {
            allColor["low"] = lowImgTexture2d.GetPixels(0, 0, lowImgTexture2d.width, lowImgTexture2d.height);
        }
        if (thumbnailTexture2d != null && allColor.ContainsKey("thumbnail") == false)
            allColor.Add("thumbnail", thumbnailTexture2d.GetPixels(0, 0, thumbnailTexture2d.width, thumbnailTexture2d.height));
        short bianlun = -1;
        if (nowSelectgid > 0)
        {
            jsonGroupInfo = gameidJsondata.getRoomGroup(nowSelectgid);
            groupPt = groupPtInfos[nowSelectgid];
            
            for (int i = 0; i < jsonGroupInfo.roomIds.Count; i++)
            {
                var aid = jsonGroupInfo.roomIds[i];
                var areamItem = gameidJsondata.getAid(aid);
                if (areamItem.tempExKey.Equals(""))
                {
                    continue;
                }
                groupPt.getDataByAid(aid, ref tempData);
                foreach (var pt in tempData)
                {
                    if (digitMatrix.Data[pt.x, pt.y] == bianlun) continue;
                    color.a = 0f;
                    //lowImgTexture2d.SetPixel(pt.y, pt.x, color);
                    allColor["low"][pt.y * gameidJsondata.width + pt.x] = color;
                }
            }
        }
        else
        {
            utilsTools.sendEventObjectParams("inGameLoadingValue", 100f, true);
            utilsTools.setGDataByKey("enableZoomMove", true);
        }
        nowSelectgid = gid;
        jsonGroupInfo = gameidJsondata.getRoomGroup(nowSelectgid);
        groupPt = groupPtInfos[gid];
        
        
        for (int i = 0; i < jsonGroupInfo.roomIds.Count; i++)
        {
            var aid = jsonGroupInfo.roomIds[i];
            var areamItem = gameidJsondata.getAid(aid);
            if (areamItem.tempExKey.Equals(""))
            {
                continue;
            }
            groupPt.getDataByAid(aid, ref tempData);
            foreach (var pt in tempData)
            {
                if (digitMatrix.Data[pt.x, pt.y] == bianlun) continue;
                this.nowSelectColors.getTiledColor(pt.x, pt.y, ref color);
                color.a = 1f;
                allColor["low"][pt.y * gameidJsondata.width + pt.x] = color;
                //lowImgTexture2d.SetPixel(pt.y, pt.x, color);
            }
        }
        lowImgTexture2d.SetPixels(0, 0, lowImgTexture2d.width, lowImgTexture2d.height, allColor["low"]);
        lowImgTexture2d.Apply();
        lowImg.SetActive(true);
        if (onAreaHandler != null)
            onAreaHandler(gid, true);
        if (utilsTools.ContainsKeyGDataByKey("firstSwitchShowAreaEnd") == false)
        {
            utilsTools.setGDataByKey("firstSwitchShowAreaEnd", utilsTools.getTotalMillisecond());
        }
        return true;
    }
    
    public List<TempGroupInfos> getGroups()
    {
        List<TempGroupInfos> ret = new List<TempGroupInfos>();
        if (digitMatrix != null && lineTexture2d != null && gameidJsondata != null)
        {
            foreach (var item in gameidJsondata.roomGroupsData)
            {
                TempGroupInfos tempGroupInfos = new TempGroupInfos();
                item.getRGB(out tempGroupInfos.rgb);
                tempGroupInfos.gid = item.id;
                tempGroupInfos.size = (short)(item.roomIds.Count());
                if (item.isComplete) tempGroupInfos.show = tempGroupInfos.size ;
                else
                {
                    short cout = 0;
                    for (int i = 0; i < item.roomIds.Count(); i++)
                    {
                        if (gameidJsondata.roomDatas[item.roomIds[i]].isShow)
                        {
                            cout++;
                        }
                    }
                    tempGroupInfos.show = cout;
                }
                ret.Add(tempGroupInfos);
            }
        }
        return ret;
    }
    void onShowAreaByNoAnimation(short aid)
    {
        int cout = GroupByAreaPtInfos.mapAllDatas[aid].Count;
        var pts = GroupByAreaPtInfos.mapAllDatas[aid];
        Color[] low = allColor["low"];
        Color[] line = allColor["line"];
        Color[] colors1 = colorTexure2d.GetPixels();
        for (int i = 0; i < cout; i++)
        {
            int value = pts[i];
            short x = (short)(value % gameidJsondata.width);
            short y = (short)(value / gameidJsondata.width);
            digitMatrix.Data[x, y] = fullShow;
            line[value].a = 0;
            low[value].a = 0;
            int x1 = (int)((float)x / (float)gameidJsondata.width * colorTexure2d.width);
            int y1 = (int)((float)y / (float)gameidJsondata.height * colorTexure2d.height);
        }
        lineTexture2d.SetPixels(0, 0, gameidJsondata.width, gameidJsondata.height, allColor["line"]);
        lineTexture2d.Apply();
        lowImgTexture2d.SetPixels(0, 0, gameidJsondata.width, gameidJsondata.height, allColor["low"]);
        lowImgTexture2d.Apply();
        TaskManages.getInstance().addTask(this, (System.Object[] _) =>
        {
            if (utilsTools.isEqualByValue(gameScenUseInfo.progress, 1.0, 0.0000002f))
            {
                TaskManages.getInstance().addTask(this, (System.Object[] parma) =>
                {
                    utilsTools.setGDataByKey("enableZoomMove", false);
                    if (lowImg != null) lowImg.SetActive(false);
                    if (areaIdBase != null) areaIdBase.SetActive(false);
                    utilsTools.sendEventObjectParams("gamePlayerEndHandler");
                });
            }
        });
        drawThumbnailByAid(new List<short>() { aid });
    }
    public virtual bool showAreaByPos(short x,short y,OnAreaHandler showBack)
    {
        if (x >= digitMatrix.RowCount || y >= digitMatrix.ColumnCount || this.nowSelectgid < 0 || x < 0 || y < 0)
        {
            if(showBack!= null)
                showBack(-1, false);
            return false;
        }
        short oldValue = digitMatrix.Data[x, y];
        if (oldValue < 20)
        {
            var aids =  getDiffAids();
            if (showBack != null)
                showBack(-1, false);
            return false;
        }
        short aid = (short)(oldValue - 20);
        foreach (var item in showAreaAnimationDatas)//边界判定 卡顿并且尚未重置中心点时候
        {
            if(item.m_aid.Equals(aid))
            {
                if (showBack != null) showBack(-1, false);
                return false;
            }
        }
        var areaItem = gameidJsondata.roomDatas[aid];
        if(gameidJsondata.getRoomGroup(nowSelectgid).roomIds.IndexOf(aid) < 0)
        {
            if (showBack != null) showBack(aid, false);
            return false;
        }
        if (areaItem.isShow)
        {
            if (showBack != null)
                showBack(-1, false);
            return false;
        }
        areaItem.isShow = true;

        if (areaItem.tempExKey.Equals("") == false)
        {
            var node = areaIdBase.transform.Find(areaItem.tempExKey);
            if (node != null)
            {
                node.SetParent(null);
                GameObject.Destroy(node.gameObject);
            }
            areaItem.tempExKey = "";
        }
        if (fillAnimation == true)
        {
            AreaAnimationDiffuse areaAnimationDiffuse = new AreaAnimationDiffuse(x, y, aid);
            areaAnimationDiffuse.m_fpsStart = 0;
            //short m_fpsEnd = utilsTools.getGDataByKey<short>("areaOutValue", areaAnimationDiffuse.m_fpsEnd);
            areaAnimationDiffuse.m_fpsEnd = 50;
            showAreaAnimationDatas.Add(areaAnimationDiffuse);
        }
        else
        {
            groupPtInfos_All[areaItem.gid].removeAid(areaItem.id);
            onShowAreaByNoAnimation(areaItem.id);
        }
        if(areaIdBase != null)
        {
            var eff = areaIdBase.transform.Find("clickPosEff");
            if(eff != null)
            {
                RectTransform rectTransform = eff.GetComponent<RectTransform>();
                float x1 = (((float)x) / (float)gameidJsondata.width);
                float y1 = (((float)y) / (float)gameidJsondata.height);
                RectTransform areaBase = areaIdBase.GetComponent<RectTransform>();
                float w1 = areaBase.rect.width;// * areaBase.localScale.x;
                float w2 = areaBase.rect.height;// * areaBase.localScale.y;
                x1 = x1 * w1 - w1 / 2;
                y1 = y1 * w2 - w2 / 2;
                rectTransform.localPosition = new Vector3(x1,y1);
                GameClickPosEffect gameClickPosEffect = eff.GetComponent<GameClickPosEffect>();
                Color color;
                gameidJsondata.getRoomGroup(nowSelectgid).getRGB(out color);
                gameClickPosEffect.baseColor = color;
                gameClickPosEffect.player1(8, 0.3f);
            }
        }
        utilsTools.sendEventObjectParams("showAreaEventOk", areaItem, x, y);
        if (showBack != null)
            showBack(aid, true);
        return true;
    }

    // Update is called once per frame
    void Update()
    {
        onShowAnimationHandler();
    }
    private void LateUpdate()
    {
        TaskManages.getInstance().updateTask(this);
    }
    void onShowAnimationHandler()
    {
        
        if(showAreaAnimationDatas.Count > 0)
        {
            List<short> removeAids = new List<short>();
            int cout1 = 0;
            int dt001 = utilsTools.getTotalMillisecond();
            foreach (var item in showAreaAnimationDatas)
            {
                short gid = gameidJsondata.getAid(item.m_aid).gid;
                short nowCheckId = (short)(item.m_aid + 20);
                var groupPtInfo = groupPtInfos[gid];
                int cout = 0;
                utilsTools.Ring(item.m_x, item.m_y, item.m_fpsEnd, item.m_fpsStart,  gameidJsondata.width, gameidJsondata.height,(int x, int y, int idx) =>
                {
                    short nowValue = digitMatrix.Data[x, y];
                    if(!nowValue.Equals(nowCheckId))
                    {
                        return false;
                    }
                    cout++;
                    cout1++;
                    digitMatrix.Data[x, y] = fullShow;
                    Color color = allColor["line"][idx];
                    color.a = 0;
                    allColor["line"][idx] = color;
                    allColor["low"][idx] = color;
                    return true;
                });
                if (cout == 0)
                {
                    //没有可渲染区域
                    removeAids.Add(item.m_aid);
                    List<Vector2Short> tempLineColors = new List<Vector2Short>();
                    groupPtInfo.getLineByAid(item.m_aid,ref tempLineColors);
                    foreach (var item1 in tempLineColors)
                    {
                        if (digitMatrix.Data[item1.x, item1.y].Equals(bianlun))
                        {
                            cout++;
                            int idx = (gameidJsondata.width - item1.x + 1) * gameidJsondata.width + (gameidJsondata.height - item1.y + 1);
                            digitMatrix.Data[item1.x, item1.y] = fullShow;
                            Color color = allColor["line"][idx];
                            color.a = 0;
                            allColor["line"][idx] = color;
                            allColor["low"][idx] = color;
                        }
                    }
                    groupPtInfo.removeAid(item.m_aid);

                }
                else
                {
                    short diff = (short)(item.m_fpsEnd - item.m_fpsStart);
                    item.m_fpsStart = item.m_fpsEnd;
                    item.m_fpsEnd += diff;
                }
            }

            int dt002 = utilsTools.getTotalMillisecond();
            if (cout1 > 0)
            {
                lineTexture2d.SetPixels(0, 0, gameidJsondata.width, gameidJsondata.height, allColor["line"]);
                int dt003 = utilsTools.getTotalMillisecond();
                lineTexture2d.Apply();
                int dt004 = utilsTools.getTotalMillisecond();
                lowImgTexture2d.SetPixels(0, 0, gameidJsondata.width, gameidJsondata.height, allColor["low"]);
                lowImgTexture2d.Apply();
                Debug.Log("4-3 = " + (dt004 - dt003).ToString() + " 3 - 2 = " + (dt003 - dt002).ToString() + " 2 - 1 = " + (dt002 - dt001).ToString());
                
            }
            if (removeAids.Count > 0)
            {
                
                //移除已经播放完成
                foreach (var aid in removeAids)
                {
                    for (int i = 0; i < showAreaAnimationDatas.Count; i++)
                    {
                        if (showAreaAnimationDatas[i].m_aid.Equals(aid) == true)
                        {
                            showAreaAnimationDatas.RemoveAt(i);
                            break;
                        }
                    }
                }
                drawThumbnailByAid(removeAids);
                if (showAreaAnimationDatas.Count == 0)
                {
                    //保存一次缩略遮罩图
                    thumbnailIsChange = true;
                    if (utilsTools.isEqualByValue(gameScenUseInfo.progress, 1.0, 0.0000002f))
                    {
                        TaskManages.getInstance().addTask(this, (System.Object[] parma) =>
                        {
                            utilsTools.setGDataByKey("enableZoomMove", false);
                            if (lowImg != null) lowImg.SetActive(false);
                            if (areaIdBase != null) areaIdBase.SetActive(false);
                            utilsTools.sendEventObjectParams("gamePlayerEndHandler");
                        });
                    }
                }
            }
        }
    }
    void drawThumbnailByAid(List<short> aids)
    {
        if (false)
        {
            TaskItemBack taskItemBack = (System.Object[] parmass) =>
            {
                short aid = (short)parmass[0];
                int startIdx = (int)parmass[1];
                int step = (int)parmass[2];
                var pts = GroupByAreaPtInfos.mapAllDatas[aid];
                Color[] thumbnail = allColor["thumbnail"];
                Color[] jpgColors = allColor["color"];
                for (int i = 0; i < step; i++)
                {
                    int value = pts[i + startIdx];
                    short x = (short)(value % gameidJsondata.width);
                    short y = (short)(value / gameidJsondata.width);
                    int x1 = (int)((float)x / (float)gameidJsondata.width * colorTexure2d.width);
                    int y1 = (int)((float)y / (float)gameidJsondata.height * colorTexure2d.height);
                    thumbnail[value] = jpgColors[y1 * colorTexure2d.width + x1];
                }
                thumbnailIsChange = true;
            };
            for (int i = 0; i < aids.Count; i++)
            {
                short aid = aids[i];
                int cout = GroupByAreaPtInfos.mapAllDatas[aid].Count;
                object[] parmass1 = { aid };
                loopAddTaskBack(cout, 10240, taskItemBack, parmass1);
            }
            
        }
    }
    public void resetSize()
    {
        //怎么保持宽高比
        float width = lineTexture2d.width;
        float height = lineTexture2d.height;
        float bet = width / height;
        RectTransform rectTransform = lineImg.GetComponent<RectTransform>();
        RectTransform baseTransform = rectTransform.transform.parent.GetComponent<RectTransform>();
        Vector2 maxSize = new Vector2(baseTransform.rect.width * 0.95f,baseTransform.rect.height * 0.95f);
        float bet1 = 1.0f;
        while (false)
        {
            float tempWidth = rectTransform.rect.width * bet1 * rectTransform.localScale.x;
            float tempHeight = rectTransform.rect.width / bet * bet1 * rectTransform.localScale.y;
            if(tempWidth < maxSize.x && tempHeight < maxSize.y)
            {
                width = tempWidth;
                height = tempHeight;
                break;
            }
            bet1 = bet1 * 0.95f;
        }
        //确定好宽高后赋值
        GameObject[] gameObjects = { lineImg, colorImg, lowImg, areaIdBase };
        Vector3 tempPos = new Vector3();
        Quaternion quaternion;
        foreach (var item in gameObjects)
        {
            if (item == null) continue;
            var temp = item.GetComponent<RectTransform>();
            temp.sizeDelta = new Vector2(width,height);
        }
        checkAddAreaId();
        this.updateScaleView();
        if(lowImg != null)
        {
            utilsTools.setGDataByKey("sltTextureSize",new Vector2(lowImgTexture2d.width/4,lowImgTexture2d.height/4));
        }
    }
    public bool getSize(out int width,out int height)
    {
        width = -1;
        height = -1;
        if (lineTexture2d == null)
            return false;
        width = lineTexture2d.width;
        height = lineTexture2d.height;
        return true;
    }
    public void onClear()
    {
        if(thumbnailTexture2d != null)
        {
            Destroy(thumbnailTexture2d);
            thumbnailTexture2d = null;
        }
        if (gameidJsondata != null) gameidJsondata = null;
        nowSelectgid = -1;
        aid2selectOrderIdx.Clear();
        gameScenUseInfo = null;
        showAreaAnimationDatas.Clear();
        groupPtInfos.Clear();
    }
    public void debugPos(int x,int y)
    {
    }

    public void onTipHandler()
    {
        if (isDebugTestToEnd == false)
        {
            if (nowSelectgid >= 0)
            {
                var roomGroup = gameidJsondata.getRoomGroup(nowSelectgid);
                if (groupPtInfos.ContainsKey(nowSelectgid) == false)
                {
                    return;
                }
                var aidpts = groupPtInfos[nowSelectgid];
                short[] noshowAids = aidpts.getDataAllKey(true);
                if (noshowAids.Length > 0)
                {
                    short aid = -1;
                    for (int i = 0; i < noshowAids.Length; i++)
                    {
                        aid = noshowAids[i];
                        foreach (var item in showAreaAnimationDatas)
                        {
                            if (aid.Equals(item.m_aid))
                            {
                                aid = -1;
                                break;
                            }
                        }
                        if (aid > -1)
                        {
                            break;
                        }
                    }
                    if (aid > -1)
                    {
                        var tipAidInfo = gameidJsondata.getAid(aid);
                        utilsTools.sendEventObjectParams("tipShowPosMaxScale", tipAidInfo, gameidJsondata.width, gameidJsondata.height);
                    }
                }
            }
        }
        else if (nowSelectgid >= 0)
        {
            var roomGroup = gameidJsondata.getRoomGroup(nowSelectgid);
            if (groupPtInfos.ContainsKey(nowSelectgid) == false)
            {
                return;
            }
            var aidpts = groupPtInfos[nowSelectgid];
            short[] noshowAids = aidpts.getDataAllKey();
            List<short> aids = new List<short>();
            if (noshowAids.Length > 0)
            {
                short aid = -1;
                for (int i = 0; i < noshowAids.Length; i++)
                {
                    aid = noshowAids[i];
                    foreach (var item in showAreaAnimationDatas)
                    {
                        if (aid.Equals(item.m_aid))
                        {
                            aid = -1;
                            break;
                        }
                    }
                    if (aid > -1)
                    {
                        aids.Add(aid);
                    }
                }
            }
            if (aids.Count > 0)
            {
                TaskManages.getInstance().addTask(this, (System.Object[] p) =>
                {
                    List<short> showAids = (List<short>)p[0];
                    foreach (var item in showAids)
                    {
                        var tipAidInfo = gameidJsondata.getAid(item);
                        showAreaByPos(tipAidInfo.px, tipAidInfo.py, null);
                        break;
                    }
                    utilsTools.addTimer_Update(gameObject, "sdcdvc", (String name) =>
                    {
                        utilsTools.setGDataByKey("enableZoomMove", false);
                        if (lowImg != null) lowImg.SetActive(false);
                        if (areaIdBase != null) areaIdBase.SetActive(false);
                        utilsTools.sendEventObjectParams("gamePlayerEndHandler");
                        return false;
                    },15);
                }, aids);
            }
        }
        else{
            utilsTools.setGDataByKey("enableZoomMove", false);
            if (lowImg != null) lowImg.SetActive(false);
            if (areaIdBase != null) areaIdBase.SetActive(false);
            utilsTools.sendEventObjectParams("gamePlayerEndHandler");
        }
    }
    private void loadSelectImageTexture(Texture2D nowSelectTexture,NotifyEmptParam notify, int width = 0,int height = 0)
    {
        if(width == 0 || height == 0)
        {
            width = gameidJsondata.width;
            height = gameidJsondata.height;
        }
        Texture2D texture2D = new Texture2D(width,height);
        var tm = TaskManages.getInstance();
        nowSelectColors = new ColorIndexTables(nowSelectTexture.width,nowSelectTexture.height);
        for (int i = 0; i < nowSelectTexture.width; i++)
        {
            for (int y = 0; y < nowSelectTexture.height; y++)
            {
                nowSelectColors.setColor(i, y, nowSelectTexture.GetPixel(i, y));
            }
        }
        System.Object[] objects = { texture2D, height };
        TaskItemBack taskItemBack = (System.Object[] paramlist) =>
        {
            Texture2D texture2D1 = (Texture2D)paramlist[0];
            int height1 = (int)paramlist[1];
            int start = (int)paramlist[2];
            int step = (int)paramlist[3];
            bool isShow = false;
            Color color = new Color(1,1,1,0);
            for (int i = 0; i < step; i++)
            {
                int x = start + i;
                for (int y = 0; y < height1; y++)
                {
                    //x,y 怎么转换
                    texture2D1.SetPixel(x, y, color);
                }
            }
        };
        loopAddTaskBack(width, 220, taskItemBack, objects);
        System.Object[] objects2 = { texture2D, notify };
        tm.addTask(this, (System.Object[] param) =>
        {
            Texture2D texture2D1 = (Texture2D)param[0];
            NotifyEmptParam notify1 = (NotifyEmptParam)param[1];

            Sprite sprite = Sprite.Create(texture2D1, new Rect(0, 0, texture2D1.width, texture2D1.height), new Vector2(0.5f, 0.5f));
            lowImg.GetComponent<Image>().sprite = sprite;
            texture2D1.Apply();
            if (lowImgTexture2d != null)
            {
                Destroy(lowImgTexture2d);
                lowImgTexture2d = texture2D1;
            }
            else
            {
                lowImgTexture2d = texture2D1;
            }
            if (notify1 != null) { notify1(); }
        }, objects2);

    }
    private void loadSelectImageTextureId(int idx = 0, NotifyEmptParam notify = null)
    {
        string path = allSelectTexturePaths[idx];
        var req = Resources.LoadAsync<Texture2D>(path);
        utilsTools.delTimeHandler(gameObject, "loadGrouSmallText");
        utilsTools.addTimer_Update(gameObject, "loadGrouSmallText", (String name) =>
        {
            if(req.isDone)
            {
                TaskManages.getInstance().insertTask(this, (System.Object[] param) =>
                {
                    Texture2D texture2D = req.asset as Texture2D;
                    loadSelectImageTexture(texture2D, notify);
                });
                
                return false;
            }
            return true;
        }, 8, 100000);
    }
    public void showTimes()
    {
        //结束key 开始value
        Dictionary<string,string> timeKeys = new Dictionary<string,string>();
        timeKeys.Add("loadingEnd", "firstT");
        timeKeys.Add("endUnZip", "beginUnZip");
        timeKeys.Add("downGaoQingBack", "downGaoQing");
        timeKeys.Add("initFanHong_1", "initFanHong");
        timeKeys.Add("initFanHong_2", "initFanHong_1");
        timeKeys.Add("initFanHong_3", "initFanHong_2");
        timeKeys.Add("lineHandlerEnd", "loadGaoQingBack");
        timeKeys.Add("downJsonBack", "downJson");
        timeKeys.Add("downJsonAsynTask", "downJsonBack");
        timeKeys.Add("loadGaoQingBack", "loadGaoQing");
        timeKeys.Add("selectMember", "loadGaoQingBack");
        timeKeys.Add("makeAreaIdEnd", "makeAreaIdBegin");
        timeKeys.Add("makeThumbnailEnd", "makeThumbnailBegin");
        timeKeys.Add("loadJpgBack", "loadJpg");
        timeKeys.Add("loadBinEnd", "loadBinBegin");
        timeKeys.Add("downZipEnd", "downZipBegin");
        timeKeys.Add("endIdPosToArray", "beginIdPosToArray");
        
        timeKeys.Add("firstSwitchShowAreaEnd", "firstSwitchShowAreaBegin");
        String str1 = "";
        foreach (var item in timeKeys)
        {
            int defvalue = 0;
            int end = utilsTools.getGDataByKey<int>(item.Key, defvalue);
            int begin = utilsTools.getGDataByKey<int>(item.Value, defvalue);
            if(end.Equals(defvalue) || begin.Equals(defvalue)) { continue; }
            str1 = str1 + item.Key + "-----" + item.Value + ":" + (end - begin).ToString()+"\n";
        }
        Debug.Log(str1);
        Transform debugShowTimes = transform.parent.Find("debugShowTimes");
        if (debugShowTimes != null)
        {
            debugShowTimes.GetComponent<TextMeshProUGUI>().text = str1;
            utilsTools.addTimer_Sec(gameObject, "sdf", (String _) =>
            {
                transform.parent.Find("debugShowTimes").gameObject.SetActive(false);
                return false;
            }, 5);
        }
    }
}
