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

public class DeviceBind : MonoBehaviour
{
    public DeviceType BindType;
    public List<DeviceList> Binddata;
    public Transform BindParent;
    public GameObject BindePrefab;
    public Text Title;
    public Button CloseBtn;

    public void DataBind(DeviceType bindType, List<DeviceList> binddata, string bindobjname = null)
    {
        Title.text = bindType.ToString() + "列表";
        Binddata = binddata;
        BindType = bindType;
        foreach (Transform item in BindParent)
        {
            if (item.name != "None")
            {
                Destroy(item.gameObject);
            }
        }

        foreach (var item in binddata)
        {
            GameObject go = Instantiate(BindePrefab, BindParent);
            Color showColor = Color.white;
            foreach (var de in DevicePanelManager.Instance.BindObjectList)
            {
                if (!string.IsNullOrEmpty(de.ObjName))
                {
                    if (de.DeviceId == item.deviceNo)
                    {
                        if (de.ObjName == bindobjname)
                        {
                            showColor = Color.red;
                        }
                        else
                        {
                            showColor = Color.yellow;
                        }
                        break;
                    }
                }
            }
            go.GetComponent<DeviceItem>().DataBind(item, showColor);
        }
        if (binddata.Count == 0)
        {
            BindParent.Find("None").gameObject.SetActive(true);
        }
        else
        {
            BindParent.Find("None").gameObject.SetActive(false);
        }
        gameObject.SetActive(true);
    }
    void Start()
    {
        CloseBtn.onClick.AddListener(() =>
        {
            gameObject.SetActive(false);
        });
    }
}