using System.Collections;
using System.Collections.Generic;
using TFramework;
using UnityEngine;
using UnityEngine.UI;

public class DebugPanelData : PanelData
{

}

/*
 UI面板预制体路径
 UI面板AB包路径（热更使用）
 UI面板完整路径（热更编辑器调试使用）
 加载方式（对应前面路径）
 UI作用域（决定该UI面板挂载在那个Canvas下）
 */
[UIResource("UI/DebugPanel",UIManager.DEFAULTDOMAIN)]
public partial class DebugPanel :BasePanel
{
    public override bool StickyTopic => true;

    FPS fPS = new FPS();

    private List<BaseComponent> m_componentsList = new List<BaseComponent>();


    public void Init()
    {
        fPS.onUpdateFPS = (f) => FPS_Text.text =  $"<color={(f<60?"red": "#00FF1A")}>{f.ToString("F2")}</color>";
        CloseBtn_Button.onClick.AddListener(() =>
        {
            Max_RectTransform.SetActive(false);
            Min_DragIcon.SetActive(true);
        });

        Min_DragIcon.onClick = () =>
        {
            Max_RectTransform.SetActive(true);
            Min_DragIcon.SetActive(false);
        };

        for (int i = 0; i < Content_DebugComponent.Components.Length; i++)
        {
            m_componentsList.Add(Content_DebugComponent.Components[i].m_prefab.Clone(Content_DebugComponent.transform));
            m_componentsList[i].Init();
        }

        for (int i = 0; i < Content_DebugComponent.Components.Length; i++)
        {
            if (ItmeBar_Toggle.transform.parent.childCount <= i)
            {
                ItmeBar_Toggle.Clone(ItmeBar_Toggle.transform.parent,true);
            }
            ItmeBar_Toggle.transform.parent.GetChild(i).GetChildComponent<Text>("Label").text = Content_DebugComponent.Components[i].m_name;
            int index = i;
            ItmeBar_Toggle.transform.parent.GetChild(i).GetComponent<Toggle>().onValueChanged.AddListener(isOn =>
            {
                if (isOn)
                {
                   ChangeView(index);
                }
            });
            ItmeBar_Toggle.transform.parent.GetChild(i).SetActive(true);
        } 
    }


    public void ChangeView(int index)
    {
        m_componentsList.ForEach(c => c.SetActive(false));
        m_componentsList[index].SetActive(true);
    }

    public override void OnEnter(IPanelData args = null)
    {
        base.OnEnter(args);
    }

    public override void OnExit(IPanelData args = null, bool isDestroy = false)
    {
        if(isDestroy)
        {
            for (int i = 0; i < m_componentsList.Count;)
            {
                GameObject.Destroy(m_componentsList[i]);
                m_componentsList.Remove(m_componentsList[i]);
            }
        }
        base.OnExit(args);
    }
}
