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

public class FaceDetectUI : UIPanelBase
{
    public GameObject ItemPrefab;

    private Dictionary<string, string> itemDetail = new Dictionary<string, string>();
    private Transform _content;

    private void Awake()
    {
        _content = transform.Find("Content/ScrollView/Viewport/Content");
    }

    public void RefreshData(Dictionary<string, string> items)
    {
        int index = 0;
        int count = _content.transform.childCount;

        for (int i = 0; i < count; i++)
        {
            if (_content.transform.GetChild(i).gameObject.activeSelf)
            {
                _content.transform.GetChild(i).gameObject.SetActive(false);
            }
        }

        if (count < items.Count)
        {
            for (int i = count; i < items.Count; i++)
            {
                Instantiate(ItemPrefab, _content);
            }
        }

        foreach (var item in items)
        {
            GameObject go;
            if (index < count)
            {
                go = _content.transform.GetChild(index).gameObject;
                if (!go.TryGetComponent<PanelItemManager>(out _))
                {
                    Destroy(go);
                    go = Instantiate(ItemPrefab, _content);
                }
            }
            else
            {
                go = Instantiate(ItemPrefab, _content);
            }
            var mgr = go.GetComponent<PanelItemManager>();
            mgr.Title.text = item.Key;
            mgr.Description.text = item.Value;
            go.transform.gameObject.SetActive(true);
            go.transform.SetSiblingIndex(index);
            index++;
        }
    }

    public void OnBackBtnClicked()
    {
        UIManager.Instance.CloseCurrent();
        LoadingManager.Instance.Load("Boot");
    }
}