using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
class NetListDataItems
{
    public int nowPageId = -1;
    public Vector2 offsetPos = Vector2.zero;
    public List<netLableImageItem> nowdatas = null;
    public Dictionary<string,int> id2idx = new Dictionary<string,int>();
    //最后一页是否整页面
    public bool lastIsIntegrity(int onePageSize)
    {
        if (nowdatas == null) return false;
        if (nowPageId < 0) return false;
        return nowdatas.Count.Equals(onePageSize * nowPageId);
    }
    public bool appendData(List<netLableImageItem> data,int onePageSize)
    {
        int cout = 0;
        for (int i = 0; i < data.Count; i++)
        {
            if (id2idx.ContainsKey(data[i].id) == false)
            {
                id2idx[data[i].id] = nowdatas.Count;
                nowdatas.Add(data[i]);
                cout++;
            }
        }
        if(onePageSize.Equals(cout))
        {
            nowPageId++;
        }
        return cout > 0;
    }
    public int getCount()
    {
        if (nowdatas == null) return 0;
        return nowdatas.Count;
    }
}
public class homeImageGridView : gridView
{
    public int loadType = 0;
    public bool isTestByNet = false;
    const string urlRoot = "http://8.218.187.222:19897/app/v2/game/ts/tagList";
    public int TagId = -1;
    const int onePageSize = 20;
    private bool nowIsShow = false;
    private static Dictionary<int, NetListDataItems> alldatas = new Dictionary<int, NetListDataItems>();
    
    public void onVectorChangeEvent(Vector2 vector2)
    {
        if(vector2.y == 0 && loadType == 0 && TagId >= 0)
        {
            //给view 添加 底下一排
            loadType = 1;
            urlDataGetByTagPageId(TagId, alldatas[TagId].nowPageId + 1);
        }
    }
    public void setData(int size, gradViewBack back, GetGradViewInfo getGradViewInfo)
    {
        setData(size, back, true, getGradViewInfo);
        GameObject gameObjectContext = GetComponent<ScrollRect>().content.gameObject;
        gameObjectContext.GetComponent<GridLayoutGroup>().SetLayoutVertical();
    }
    public void swapLoadData(int tagId,bool isNowShow)
    {
        if (isNowShow && TagId >= 0)
        {
            //记录高度
            alldatas[TagId].offsetPos = getOffset();
        }
        TagId = tagId;
        nowIsShow = isNowShow;
        if (alldatas.ContainsKey(tagId) == false)
        {
            var netListDataItems = new NetListDataItems();
            netListDataItems.nowPageId = 1;
            alldatas.Add(tagId, netListDataItems);
            urlDataGetByTagPageId(TagId, 1);
        }
        else
        {
            TaskManages.getInstance().addTask(this, updateData);
            setOffset(alldatas[TagId].offsetPos);
            //重新拉去最后一页
            if (alldatas[TagId].lastIsIntegrity(onePageSize) == false)
            {
                urlDataGetByTagPageId(TagId, alldatas[tagId].nowPageId);
            }
        }
    }
    void urlDataGetByTagPageId(int tag, int page)
    {
        string urlFromat = urlRoot + "?tagId={0}&page={1}&zygt=hzwz&tgtk=1";
        onUrlBackHandler backHandler = (string id, string url, bool isSuccess, System.Object data) =>
        {
            Debug.Log(url);
            //Debug.Log(data.ToString());
            //String path =  utilsTools.getWritableByPath("temp" + TagId.ToString() + ".json");
            //File.WriteAllText(path, data as string);
            if (isSuccess == false)
            {
                if (isTestByNet == false)
                {
                    return 1;
                }
                onUrlBackHandler(getDebugUrls());
                return -1;
            }
            else
            {
                onUrlBackHandler(data.ToString());
                return 0;
            }
        };
        utilsTools.httpGET(this, urlFromat, true, backHandler, tag, page);
    }
    string getDebugUrls()
    {
        return "";
    }
    void onUrlBackHandler(string data)
    {
        netLabelData labeldata = JsonUtility.FromJson<netLabelData>(data);
        if (labeldata == null)
        {
            return;
        }
        if (labeldata.status.Equals(200))
        {
            loadType = 0;
            int count = labeldata.result.data.Count;
            if (count > 0)
            {
                var temp = alldatas[TagId];
                if(count.Equals(onePageSize))
                {
                    temp.nowPageId++;
                }
                if(temp.nowdatas == null)
                {
                    temp.nowdatas = labeldata.result.data;
                }
                else
                {
                    if(temp.appendData(labeldata.result.data,onePageSize) == false) {
                        //没有新加不需要更新
                        return; 
                    }
                }
                TaskManages.getInstance().addTask(this, updateData);
            }

        }
    }
    public void updateData(System.Object[] objects)
    {
        int count = alldatas[TagId].nowdatas.Count;
        if ( nowIsShow == false && count > 10 ){
            count = 10;
        }
        this.setData(count, (GameObject gameObject, int idx, ListViewBackTypeEnum backType) => {
            if (backType == ListViewBackTypeEnum.create || ListViewBackTypeEnum.update == backType)
            {
                mainImageItem item = gameObject.GetComponent<mainImageItem>();
                item.setData(alldatas[TagId].nowdatas[idx]);
            }
        }, null);
    }
    // Update is called once per frame
    void Update()
    {
        TaskManages.getInstance().updateTask(this);
    }
}
