using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
//============================================================
//支持中文,文件使用UTF-8编码
//@author	#AUTHOR#
//@create	#CREATEDATE#
//@company	#COMPANY#
//
//@description:
//============================================================

public class ToolItem : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public string toolName;
    public Button closeButton;
    public Button selfButton;
    public Image showIcon;
    public Text showName;
    public GameObject tips;
    public Vector3 originPos = new Vector3();
    public Vector3 originAngle = new Vector3();
    public GameObject prefab;
    public Attribute attribute = Attribute.defaultModel;

    public RightAndWrong rightAndWeong = RightAndWrong.defaultModel;

    private float y;


    private void Start()
    {
        tips = UIManager.Instance.toolsItemManager.toolPanel.transform.Find("Tips").gameObject;
        showName = tips.transform.GetChild(0).GetComponent<Text>();
    }

    public void SetValue(string name, Transform modelTransform,Vector3 pos,Vector3 Rot)
    {
        toolName = name;
        closeButton.onClick.AddListener(OnClose);

        Texture2D icon = Resources.Load<Texture2D>("New/UI/" + name);
        showIcon.sprite = ToSprite(icon);
        originPos = pos;
        originAngle = Rot;
        prefab = Resources.Load<GameObject>("New/Models/" + toolName);
        attribute = prefab.GetComponent<ToolModelClick>().attribute;
        rightAndWeong = modelTransform.GetComponent<ToolModelClick>().rightAndWrong;
    }

    public void SetState(bool isActive)
    {
        closeButton.gameObject.SetActive(isActive);
        if (toolName == "工作卡")
            selfButton.interactable = isActive;
        else
            selfButton.interactable = !isActive;
    }

    /// <summary>
    /// 判断自己是不是可以点击
    /// </summary>
    public void SwitchSelfIsClick()
    {
        closeButton.gameObject.SetActive(false);
        if (attribute == Attribute.SafetyToolsAndInstruments)
        {
            selfButton.interactable = false;
        }
        else if (attribute == Attribute.ToolsAndInstruments)
        {
            selfButton.interactable = true;
        }
        else
        {

        }
    }



    private Sprite ToSprite(Texture2D t)
    {
        Sprite prite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0.5f, 0.5f));
        return prite;
    }

    private void OnClose()
    {
        tips.SetActive(false);
        GameObject obj = Instantiate(prefab);
        obj.name = toolName;
        obj.transform.localPosition = originPos;
        obj.transform.localEulerAngles = originAngle;
        Destroy(gameObject);
        ToolsItemManager.Instance.RemoveNull();
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        showName.text = toolName;
        tips.transform.position = transform.position + new Vector3(0, 10, 0);
        tips.SetActive(true);
    }


    public void OnPointerExit(PointerEventData eventData)
    {
        tips.SetActive(false);
    }

}