using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class gameSceneLogic : MonoBehaviour, IEventHandler
{
    // Start is called before the first frame update
    public GameObject gameShowView = null;
    public GameObject areaGroupList = null;    
    public static int maxIdValue = 100;
    private int nowidx = 4;
    public float progress = 0;
    public bool showAreaId = true;
    private List<TempGroupInfos> groupInfos = null;
    private Vector2 pos1 = new Vector2(-10000, -10000);
    private int rvalue = -1;
    Dictionary<string, EventChildItemBack> childEventHandlers = new Dictionary<string, EventChildItemBack>();
    void onEvent(string name, EventChildItemBack func)
    {
        utilsTools.onEvent(this, name);
        childEventHandlers.Add(name, func);
    }
    void Start()
    {
        Application.SetStackTraceLogType(LogType.Log, StackTraceLogType.None);
        onEvent("switchGameAreaGroupId",this.onEvent_switchGameAreaGroupId_Handler);
        onEvent("gameViewOnInitEnd",this.onEvent_gameViewOnInitEnd_Handler);
        onEvent("gameClickPos",this.onEvent_gameClickPos_Handler);
        onEvent("inGameLoadingBarEnd",this.onEvent_inGameLoadingBarEnd_Handler);
        onEvent("showAreaEventOk", onSelectAreaEvent);
        onEvent("initViewShowData", onInitViewShowDataEvent);
        //gameScenUser
        var gridViewList = areaGroupList.GetComponent<gameSceneAreaGroupHandler>();
        gridViewList.gameObject.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {   
    }
    private void onUpdateGroupGridHandler(GameObject gobject, int idx, ListViewBackTypeEnum lvbType,TempGroupInfos group)
    {
        if (lvbType == ListViewBackTypeEnum.create || ListViewBackTypeEnum.update == lvbType)
        {
            var item = gobject.GetComponent<colorItem>();
            bool isSelect = idx == nowidx;
            item.IsSelect = isSelect;

            Vector3 vector3;
            Quaternion quaternion;
            gobject.transform.GetLocalPositionAndRotation(out vector3, out quaternion);
            vector3.y = -130f;
            if (isSelect)
            {
                vector3.y = vector3.y + 14;
            }
            gobject.transform.SetLocalPositionAndRotation(vector3, quaternion);
            item.IdName = (group.gid).ToString();
            item.index = idx;
            item.UseColor = group.rgb;
            item.NowProgress = group.Jindu;
        }
    }
    
    public void onEventHandler(string eventName, List<UnityEngine.Object> uobjects, List<System.Object> objects)
    {
        if (childEventHandlers.ContainsKey(eventName))
        {
            childEventHandlers[eventName](uobjects, objects);
        }
    }
    private void onEvent_switchGameAreaGroupId_Handler(List<UnityEngine.Object> uobjects, List<System.Object> objects)
    {
        Vector3 vector3;
        Quaternion quaternion;
        var gridViewList = areaGroupList.GetComponent<gameSceneAreaGroupHandler>();
        colorItem item = null;
        if (nowidx >= 0)
        {
            item = gridViewList.getObjectById(nowidx).GetComponent<colorItem>();
            item.IsSelect = false;
            item.transform.GetLocalPositionAndRotation(out vector3, out quaternion);
            vector3.y = vector3.y - 14;
            item.transform.SetLocalPositionAndRotation(vector3, quaternion);
        }
        var maingameView = gameShowView.GetComponent<mainGameView>();
        int idx = (int)objects[0];
        nowidx = idx;
        var nowSelectGroupId = groupInfos[idx].gid;
        item = gridViewList.getObjectById(idx).GetComponent<colorItem>();
        item.IsSelect = true;
        item.transform.GetLocalPositionAndRotation(out vector3, out quaternion);
        vector3.y = vector3.y + 14;
        item.transform.SetLocalPositionAndRotation(vector3, quaternion);
        Debug.Log("切换下标开始" + nowSelectGroupId.ToString());
        maingameView.switchShowAreaGroup((short)nowSelectGroupId, (int id, bool isOk) => {
            Debug.Log("切换下标完成" + id.ToString());
        });
    }
    private void onEvent_gameViewOnInitEnd_Handler(List<UnityEngine.Object> uobjects, List<System.Object> objects)
    {
        var gridViewList = areaGroupList.GetComponent<gameSceneAreaGroupHandler>();
        gridViewList.gameObject.SetActive(true);
        var maingameView = gameShowView.GetComponent<mainGameView>();
        var groupInfos1 = maingameView.getGroups();
        groupInfos = new List<TempGroupInfos>();
        
        for (int i = 0; i < groupInfos1.Count; i++)
        {
            if (groupInfos1[i].Jindu == 1)
            {
                continue;
            }

            groupInfos.Add(groupInfos1[i]);
        }
        gradViewBack gradView = (GameObject gobject, int idx, ListViewBackTypeEnum lvbType) => {
            this.onUpdateGroupGridHandler(gobject, idx, lvbType, groupInfos[idx]);
        };
        gridViewList.setDataNoGrid(groupInfos.Count, gradView);
        if (nowidx >= 0)
        {
            maingameView.switchShowAreaGroup((short)(groupInfos[nowidx].gid), (int id, bool isOk) => {
                Debug.Log("切换下标完成" + id.ToString());
            });
        }
    }
    private void onEvent_gameClickPos_Handler(List<UnityEngine.Object> uobjects, List<System.Object> objects)
    {
        short x = (short)objects[0];
        short y = (short)objects[1];
        var maingameView = gameShowView.GetComponent<mainGameView>();
        maingameView.showAreaByPos(x, y, (int id, bool isOk) => {
            Debug.Log("点击区域" + id.ToSafeString() + " 显示" + isOk.ToSafeString());
            return;
        });
    }
    private void onEvent_inGameLoadingBarEnd_Handler(List<UnityEngine.Object> uobjects, List<System.Object> objects)
    {
        transform.Find("ingameLoad").gameObject.SetActive(false);
    }
    void onInitViewShowDataEvent(List<UnityEngine.Object> uobjects, List<System.Object> objects)
    {
        GameScenUserInfo gameScenUser = (GameScenUserInfo)objects[0];
        Image image = transform.Find("ingameLoad/loadImg").GetComponent<Image>();
        utilsTools.loadLocalImage(this, image, gameScenUser.getLocalThumbnailPath());
    }
    void onSelectAreaEvent(List<UnityEngine.Object> uobjects, List<System.Object> objects)
    {
        gameAreaItem gameArea = (gameAreaItem)objects[0];
        short x = (short)objects[1];
        short y = (short)objects[2];
        if (groupInfos != null)
        {
            var gridViewList = areaGroupList.GetComponent<gameSceneAreaGroupHandler>();
            colorItem item = null;
            groupInfos[nowidx].show++;
            if (groupInfos[nowidx].show.Equals(groupInfos[nowidx].size))//直接移除
            {
                if(nowidx == groupInfos.Count - 1)
                {
                    groupInfos.RemoveAt(nowidx);
                    nowidx--;
                }else
                    groupInfos.RemoveAt(nowidx);
                //切换组
                gradViewBack gradView = (GameObject gobject, int idx, ListViewBackTypeEnum lvbType) => {
                    this.onUpdateGroupGridHandler(gobject, idx, lvbType, groupInfos[idx]);
                };
                gridViewList.setDataNoGrid(groupInfos.Count, gradView);
                if (nowidx >= 0)
                {
                    var maingameView = gameShowView.GetComponent<mainGameView>();
                    maingameView.switchShowAreaGroup((short)(groupInfos[nowidx].gid), (int id, bool isOk) => {
                        Debug.Log("切换下标完成" + id.ToString());
                    });
                }
            }
            else
            {
                item = gridViewList.getObjectById(nowidx).GetComponent<colorItem>();
                item.NowProgress = groupInfos[nowidx].Jindu;
            }
        }
    }
    public void onTestTemp1()
    {
        Debug.Log("---->>>");
    }

    
}
