using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using static WindowConfig;

public class WindowsManager : MonoSingleton<WindowsManager>
{
    public WindowItemConfig wConfig;
    [HideInInspector]
    public Canvas canvasRoot;
    public List<windowItemGameObject> windowItemGameObjectList;
    public WindowConfig windowsConfig;


    private void Awake()
    {
        StartCoroutine(CheckIsStart());
    }

    void initShowWindow()
    {
        for (int i = 0; i < windowItemGameObjectList.Count; i++)
        {
            if(windowItemGameObjectList[i].type == windowsConfig.initShowWindow)
            {
                windowItemGameObjectList[i].window.SetActive(true);
            }
            else
            {
                windowItemGameObjectList[i].window.SetActive(false);
            }
        }
    }

    public bool isStart = false;
    IEnumerator CheckIsStart()
    {
        while (HttpSDKAction.Instance.jsonData == "")
        {
            yield return null;
        }
        isStart = true;
        canvasRoot = GameObject.Instantiate(windowsConfig.canvas);
        for (int i = 0; i < windowsConfig.initComponent.Count; i++)
        {
           canvasRoot.gameObject.AddComponent(GetTypeByName(windowsConfig.initComponent[i].name));
        }
        windowItemGameObjectList = new List<windowItemGameObject>();
        initWindowList(windowsConfig.windowItemGameObjectList, windowItemGameObjectList, canvasRoot.transform);
        initShowWindow();
    }
    public static System.Type GetTypeByName(string name)
    {
        foreach (Assembly assembly in System.AppDomain.CurrentDomain.GetAssemblies())
        {
            foreach (System.Type type in assembly.GetTypes())
            {
                if (type.Name == name)
                    return type;
            }
        }

        return null;
    }
    void initWindowList(List<windowItemGameObject> list, List<windowItemGameObject> nowlist,Transform parent)
    {
        if(list!=null)
        {
            for (int i = 0; i < list.Count; i++)
            {
                windowItemGameObject wg = new windowItemGameObject();
                wg.type = list[i].type;
                wg.window = GameObject.Instantiate(list[i].window, parent);
                wg.window.name = list[i].window.name;
                wg.windowItemGameObjectList = new List<windowItemGameObject>();
                initGameObject(wg.window);
                initWindowList(list[i].windowItemGameObjectList, wg.windowItemGameObjectList, wg.window.transform);
                nowlist.Add(wg);
            }
        }
    }

    void initGameObject(GameObject go)
    {
        go.transform.localPosition = Vector3.zero;
        go.transform.localEulerAngles = Vector3.zero;
        go.transform.localScale = Vector3.one;
    }


    public GameObject GetPrefab(windowType wt,string name)
    {
        for (int i = 0; i < wConfig.list.Count; i++)
        {
            if(wConfig.list[i].type==wt)
            {
                for (int j = 0; j < wConfig.list[i].PrefabList.Count; j++)
                {
                    if(wConfig.list[i].PrefabList[j].name==name)
                    {
                        return wConfig.list[i].PrefabList[j].obj;
                    }
                }
            }
        }
        return null;
    }

    void setWindowData(windowType wt,GameObject go, string data)
    {
        switch(wt)
        {
            case windowType.Tip2:
                Tip2Window t2w = go.GetComponent<Tip2Window>();
                t2w.showTxt(data);
                break;
        }
        
    }

    bool isChangzhu(windowType wt)
    {
        switch (wt)
        {
            case windowType.Top:
                return true;
        }
        return false;

    }


    bool isHuLue(windowType wt)
    {
        switch (wt)
        {
            case windowType.Tip:
                return true;
            case windowType.Tip2:
                return true;
            case windowType.Error:
                return true;
        }
        return false;

    }

    Stack<windowType> wlist = new Stack<windowType>();
    public void show(windowType wt,bool needHandle = true,string data="")
    {

        for (int i = 0; i < windowItemGameObjectList.Count; i++)
        {
            if (windowItemGameObjectList[i].type == wt)
            {
                windowItemGameObjectList[i].window.SetActive(true);
                setWindowData(wt, windowItemGameObjectList[i].window, data);
            }
            else
            {
                if(needHandle&&!isHuLue(windowItemGameObjectList[i].type))
                {

                    windowItemGameObjectList[i].window.SetActive(isChangzhu(windowItemGameObjectList[i].type));

                }
            }
            if (showitem(windowItemGameObjectList[i].windowItemGameObjectList, wt, needHandle))
            {
                windowItemGameObjectList[i].window.SetActive(true);
            }
        }
        if(wlist.Count>0)
        {
            if(wlist.Peek()!=wt&& !wlist.Contains(wt))
            {
                wlist.Push(wt);
            }
        }else
        {
            wlist.Push(wt);
        }
    }

    bool showitem(List<windowItemGameObject> list,windowType wt, bool needHandle = true)
    {
        bool isShow=false;
        for (int i = 0; i < list.Count; i++)
        {
            showitem(list[i].windowItemGameObjectList, wt);
            if (list[i].type == wt)
            {
                list[i].window.SetActive(true);
                isShow = true;
            }
            else
            {
                if (needHandle && !isHuLue(list[i].type))
                {
                    list[i].window.SetActive(isChangzhu(list[i].type));
                }
            }
        }
        return isShow;
    }
}
