using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class UI_ToolOrMaterialsOrDeviceItem : BaseItem
{
    public List<ItemInfo> itemInfoList;
    public Button itemCloseBtn;
    private ItemInfo currentItem;
    public void Init(List<ItemInfo> _itemInfo)
    {
        itemInfoList = _itemInfo;
        currentItem = itemInfoList[0];
        if (GameManager.RunModelMgr.ModeType == E_ModeType.Study)
        {
            itemCloseBtn.gameObject.SetActive(false);
        }
        if (GameManager.RunModelMgr.SceneType != E_SceneType.Site)
        {
            GetControl<Image>("iconBtn").GetComponent<Button>().interactable = false;
        }
        GetControl<TextMeshProUGUI>("countText").text = _itemInfo.Count.ToString();
        GetControl<Image>("iconBtn").sprite = Resources.Load<Sprite>(Const.LoadToolAndMaterialPath + currentItem.toolName);
        GameManager.EventMgr.AddEventListener<string>(Enum_EventType.SwitchSubProcessStepTriggerID, SwitchSubProcessStepTriggerID);
    }
    private void SwitchSubProcessStepTriggerID(string arg0)
    {
        if (currentItem.toolName == arg0)
        {
            GameManager.UIMgr.imageTips.ShowTips(GetComponent<RectTransform>());
        }
    }
    protected override void OnClick(string btnName)
    {
        base.OnClick(btnName);
        switch (btnName)
        {
            case "itemCloseBtn":
                int counts = 0; ;
                if (itemInfoList.Count > 1)
                {
                    counts = itemInfoList.Count - 1;
                }
                GameObject obj = Instantiate(itemInfoList[counts].objPrefab);
                obj.transform.position = itemInfoList[counts].selfPosInToolRoom;
                obj.name = itemInfoList[counts].toolName;
                obj.GetComponent<Tool_SelectComponent>().itemInfo = itemInfoList[counts];
                GameManager.PacksackBagMgr.RemoveOneToolOrMater(itemInfoList[counts]);
                itemInfoList.Remove(itemInfoList[counts]);
                GetControl<TextMeshProUGUI>("CountText").text = itemInfoList.Count.ToString();
                if (itemInfoList.Count == 0)
                    Destroy(gameObject);
                break;
            case "iconBtn":
                bool isWorkCard = currentItem.toolName.Equals("����֤") ? false : true; 
                if (GameManager.ProcessMgr.IsRightSubProcessStepsTriggerID(currentItem.toolName, isWorkCard) == 0)
                {
                    GameManager.UIMgr.HidePanel<UI_ToolAndMaterialPanel>();
                    GameObject currentTool = Instantiate(currentItem.objPrefab);
                    currentTool.GetComponent<BaseToolOrDevice>().enabled = false;
                    currentTool.GetComponent<Collider>().enabled = false;
                    currentTool.name = currentItem.toolName;
                    LiveSceneManager.Instance.SetSpawnToolInfo(currentTool);
                    GameManager.UIMgr.imageTips.HideTips();
                }
                break;
        }
    }
    private void OnDestroy()
    {
        GameManager.EventMgr.RemoveEventListener<string>(Enum_EventType.SwitchSubProcessStepTriggerID, SwitchSubProcessStepTriggerID);
    }

    private void OnDisable()
    {
        GameManager.EventMgr.RemoveEventListener<string>(Enum_EventType.SwitchSubProcessStepTriggerID, SwitchSubProcessStepTriggerID);
    }
}