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

public class DeviceFollowTarget : MonoBehaviour
{
    public List<Sprite> spList = new List<Sprite>();
    public Transform target;
    public Camera uiCamera;
    private float offset_Y = 2.5f;

    private void Start()
    {
        GetComponent<Button>().onClick.AddListener(ViewClick);
        uiCamera = GameObject.Find("Canvas").GetComponent<Canvas>().worldCamera;
    }
    public void ViewClick()
    {
        if (GetComponent<DeviceLumos>() && DeviceInfo.Instance.gameObject.activeSelf)
            return;
        if (FindObjectOfType<FloorButtonsPanel>())
            FindObjectOfType<FloorButtonsPanel>().ChangeFloor(target.GetComponent<DeviceObj>().floorId);
        Camera.main.GetComponent<CameraOrbit>().SetCameraView(target.position, 15);
        if (target && target.GetComponent<DeviceObj>())
        {
            DevicePanelManager.Instance.NowClickObject = target.GetComponent<DeviceObj>();
            if (!target.GetComponent<DeviceLumos>())
            {
                target.gameObject.AddComponent<DeviceLumos>();
            }
            if (!GetComponent<DeviceLumos>())
                gameObject.AddComponent<DeviceLumos>();

            if (target.GetComponent<DeviceObj>().BindData.deviceNo != null)
            {
                DeviceInfo.Instance.OnShow(target.GetComponent<DeviceObj>().BindData);
            }
            else
            {
                target.GetComponent<DeviceObj>().SetBindData();
                DeviceInfo.Instance.OnShow(target.GetComponent<DeviceObj>().BindData);
            }
        }
    }

    private Vector2 WorldPosToUIPos(Vector3 worldPos)
    {
        Vector2 world2ScreenPos = Camera.main.WorldToScreenPoint(worldPos);
        Vector2 uiPos = new Vector2();
        RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.parent.GetComponent<RectTransform>(), world2ScreenPos, uiCamera, out uiPos);
        return uiPos;
    }
    private void OnDisable()
    {
        if (GetComponent<DeviceLumos>())
        {
            Destroy(GetComponent<DeviceLumos>());
        }
    }
    private void LateUpdate()
    {
        if (gameObject.activeSelf && target)
        {
            GetComponent<RectTransform>().anchoredPosition =
                WorldPosToUIPos(new Vector3(target.transform.position.x, target.transform.position.y + offset_Y, target.transform.position.z));
        }
        if (GetComponent<DeviceLumos>())
        {
            if (target && DevicePanelManager.Instance.NowClickObject != target.GetComponent<DeviceObj>())
            {
                Destroy(GetComponent<DeviceLumos>());
            }
        }
    }

    public void SetTarget(DeviceObj deviceObj)
    {
        target = deviceObj.transform;
        if (deviceObj.BindType == DeviceType.排烟风机 ||
            deviceObj.BindType == DeviceType.补风机 ||
            deviceObj.BindType == DeviceType.送风机)
        {
            GetComponent<Image>().sprite = spList[spList.Count - 1];
            return;
        }
        foreach (var item in spList)
        {
            if (deviceObj.BindType.ToString() == item.name)
            {
                GetComponent<Image>().sprite = item;
                break;
            }
        }
    }

}