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

public class DeviceItem : MonoBehaviour
{
    public DeviceList BindData;
    public Text ShowText;
    public Button LocalBtn;
    public Button ViewBtn;
    public Image CanLocal;
    void Start()
    {
        LocalBtn.onClick.AddListener(LocalBtn_Click);
        ViewBtn.onClick.AddListener(ViewBtn_Click);
    }
    private void Update()
    {
        if (Input.GetKey(KeyCode.Q))
        {
            if (Input.GetKeyDown(KeyCode.O))
            {
                LocalBtn.gameObject.SetActive(true);
            }
            if (Input.GetKeyDown(KeyCode.H))
            {
                LocalBtn.gameObject.SetActive(false);
            }
        }
    }
    public void DataBind(DeviceList bindData, Color showColor)
    {
        BindData = bindData;
        ShowText.text = bindData.deviceName;
        LocalBtn.image.color = showColor;
    }
    private void ViewBtn_Click()
    {
        DeviceInfo.Instance.OnShow(BindData);
        string deviceName = "";
        foreach (var item in DevicePanelManager.Instance.BindObjectList)
        {
            if (item.DeviceId == BindData.deviceNo)
            {
                deviceName = item.ObjName;
                break;
            }
        }
        if (!string.IsNullOrEmpty(deviceName))
        {
            DeviceObj obj = DeviceObjManager.Instance.GetDeviceByName(deviceName);
            if (null != obj)
            {
                obj.GetComponent<DeviceObj>().ShowList();
            }
            else
            {
                DevicePanelManager.Instance.NowClickObject = null;
            }
        }
        else
        {
            DevicePanelManager.Instance.NowClickObject = null;
        }
    }

    private void LocalBtn_Click()
    {
        if (DevicePanelManager.Instance.NowClickObject != null)
        {
            if (LocalBtn.image.color == Color.red)//解绑
            {
                MessageBox.Show("已经绑定该设备是否取消绑定?", Color.red, () =>
                {
                    DeviceObjConfig config = new DeviceObjConfig();
                    config.DeviceId = BindData.deviceNo;
                    config.ObjName = "";
                    DevicePanelManager.Instance.SaveDeviceObjConfig(config);
                });
            }
            else if (LocalBtn.image.color == Color.white)
            {
                bool hasbind = false;
                foreach (var item in DevicePanelManager.Instance.BindObjectList)
                {
                    if (item.ObjName == DevicePanelManager.Instance.NowClickObject.name)
                    {
                        hasbind = true;
                        break;
                    }
                }
                if (hasbind)
                {
                    MessageBox.Show("设备已经被绑定是否更换绑定?", Color.red, () =>
                    {
                        foreach (var item in DevicePanelManager.Instance.BindObjectList)
                        {
                            if (item.ObjName == DevicePanelManager.Instance.NowClickObject.name)
                            {
                                item.ObjName = "";
                                break;
                            }
                        }
                        DeviceObjConfig config = new DeviceObjConfig();
                        config.DeviceId = BindData.deviceNo;
                        config.ObjName = DevicePanelManager.Instance.NowClickObject.name;
                        DevicePanelManager.Instance.SaveDeviceObjConfig(config);
                    });
                }
                else
                {
                    MessageBox.Show("是否绑定该设备?", Color.red, () =>
                    {
                        DeviceObjConfig config = new DeviceObjConfig();
                        config.DeviceId = BindData.deviceNo;
                        config.ObjName = DevicePanelManager.Instance.NowClickObject.name;
                        DevicePanelManager.Instance.SaveDeviceObjConfig(config);
                    });
                }
            }
            else
            {
                bool hasbind = false;
                foreach (var item in DevicePanelManager.Instance.BindObjectList)
                {
                    if (item.ObjName == DevicePanelManager.Instance.NowClickObject.name)
                    {
                        hasbind = true;
                        break;
                    }
                }
                if (hasbind)
                {
                    MessageBox.Show("设备已经被绑定是否更换绑定?", Color.red, () =>
                    {
                        foreach (var item in DevicePanelManager.Instance.BindObjectList)
                        {
                            if (item.ObjName == DevicePanelManager.Instance.NowClickObject.name)
                            {
                                item.ObjName = "";
                                break;
                            }
                        }
                        DeviceObjConfig config = new DeviceObjConfig();
                        config.DeviceId = BindData.deviceNo;
                        config.ObjName = DevicePanelManager.Instance.NowClickObject.name;
                        DevicePanelManager.Instance.SaveDeviceObjConfig(config);
                    });
                }
                else
                {
                    MessageBox.Show("设备已经被绑定是否更换绑定?", Color.red, () =>
                    {
                        DeviceObjConfig config = new DeviceObjConfig();
                        config.DeviceId = BindData.deviceNo;
                        config.ObjName = DevicePanelManager.Instance.NowClickObject.name;
                        DevicePanelManager.Instance.SaveDeviceObjConfig(config);
                    });
                }
            }
        }
    }
}