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

public class DisplayChosenIcons : MonoBehaviour
{
    List<string> IconKinds = new List<string>
    {
        "水位监测", "流量监测",  "水质监测",
        
    };

    // 缓存：每个标签 -> 该标签下的所有物体
    private Dictionary<string, List<GameObject>> cachedObjects;
    private List<Button> allButtons = new List<Button>();

    public Camera cam;

    // 飞行锁定时间，固定 1.5 秒
    public float flyDuration = 1.5f;

    private bool flying = false;

    
    void Start()
    {
        if (cam == null)
            cam = Camera.main;

        cachedObjects = new Dictionary<string, List<GameObject>>();

        // 为每个标签创建一个空列表，并缓存场景物体
        foreach (string tag in IconKinds)
        {
            cachedObjects[tag] = new List<GameObject>();
            GameObject[] found = GameObject.FindGameObjectsWithTag(tag);
            cachedObjects[tag].AddRange(found);
        }

        // 绑定按钮点击
        foreach (Transform child in transform)
        {
            Button btn = child.GetComponent<Button>();
            if (btn != null)
            {
                allButtons.Add(btn);

                string btnName = child.gameObject.name;
                btn.onClick.AddListener(() => OnClickEvent(btnName));
            }
        }
    }

    void OnClickEvent(string selectedName)
    {
        if (cam.gameObject.activeInHierarchy == false)
        {
            return;
        }
        // 飞行过程中，所有点击无效
        if (flying)
        {
            return;
        }
        flying = true;
        SetButtonsInteractable(false);

        // 1. 立即切换图标显示
        ApplyDisplay(selectedName);

        // 2. 开始飞行
        if (cam != null)
        {
            DoubleClickToFocus focus = cam.GetComponent<DoubleClickToFocus>();
            if (focus != null)
            {
                focus.FlyToOverlookAndConvertCore(selectedName);
            }
        }

        // 3. 1.5 秒后解锁
        StartCoroutine(UnlockAfterDelay());
    }

    IEnumerator UnlockAfterDelay()
    {
        yield return null;//这里很恶心，没有办法确定这个响应函数和DoubleClickToFocus里的update谁先执行，
                          //只能等下一帧里面刷新了飞行时间再说
        float transitionDuration = cam.transform.GetComponent<DoubleClickToFocus>().transitionDuration;
        yield return new WaitForSeconds(transitionDuration+0.01f);

        flying = false;
        SetButtonsInteractable(true);
    }

    void ApplyDisplay(string selectedName)
    {
        if (selectedName == "全部")
        {
            foreach (var kvp in cachedObjects)
            {
                foreach (GameObject obj in kvp.Value)
                {
                    if (obj != null)
                        obj.SetActive(true);
                }
            }
        }
        else
        {
            // 先全部隐藏
            foreach (var kvp in cachedObjects)
            {
                foreach (GameObject obj in kvp.Value)
                {
                    if (obj != null)
                        obj.SetActive(false);
                }
            }

            // 再显示当前类别
            if (cachedObjects.ContainsKey(selectedName))
            {
                foreach (GameObject obj in cachedObjects[selectedName])
                {
                    if (obj != null)
                        obj.SetActive(true);
                }
            }
        }
    }

    void SetButtonsInteractable(bool enable)
    {
        foreach (Button btn in allButtons)
        {
            if (btn != null)
                btn.interactable = enable;
        }
    }
}