using System;
using System.Collections.Generic;
using DefaultNamespace;
using MotionFramework;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace ToolsPack
{
    /// <summary>
    /// 背包按钮组件
    /// </summary>
    public class ToolsPackWindowItemBtComponent : MonoBehaviour
    {
        [SerializeField] private RawImage ico;
        [SerializeField] private GameObject closeBt;
        [SerializeField] private string btName;
        [SerializeField] private int index;
        [SerializeField] private int gameNumber;
        [SerializeField] private string btNameText;
        [SerializeField] private ModelTypeEnum _modelTypeEnum;
        [SerializeField] private TMP_Text numberText;


        // [SerializeField] private Sprite defaultSprite;
        // [SerializeField] private Sprite clickSprite;
        [SerializeField] private Image btImage;
        [SerializeField] private Button clickButton;

        [SerializeField] private string clickName;
        [SerializeField] private GameObject modelNameGame;
        [SerializeField] private Dictionary<int, string> _toolsPackWindowItemBtModels;

        private void Awake()
        {
            btImage = this.GetComponent<Image>();
            gameNumber = 1;
            _toolsPackWindowItemBtModels = new Dictionary<int, string>();
        }

        private void Start()
        {
            closeBt.GetComponent<Button>().onClick.AddListener(delegate
            {
                index = _toolsPackWindowItemBtModels.Count - 1;
                btName = _toolsPackWindowItemBtModels[_toolsPackWindowItemBtModels.Count - 1];


                GameObject toolsGame = MotionEngine.GetModule<ToolsPackManager>().GetToolsPack(btName, index);
                toolsGame.SetActive(true);
                MotionEngine.GetModule<ToolsPackManager>().DeleteToolsPack(btName, index);
                MotionEngine.GetModule<ToolsPackManager>().DeleteToolsPackWindowItemBts(btName, index);
            });

            clickButton.onClick.AddListener(delegate
               {
                   MotionEngine.GetModule<ToolsPackManager>().OnTriggerStringEvent(clickName);
                   index = _toolsPackWindowItemBtModels.Count - 1;
                   int tPIBM_Index = _toolsPackWindowItemBtModels.Count - 1;
                   if (tPIBM_Index > 0) tPIBM_Index = 0;//HQB多次拿同一个工具会出现问题
                   btName = _toolsPackWindowItemBtModels[tPIBM_Index];//[_toolsPackWindowItemBtModels.Count - 1];
                   switch (_modelTypeEnum)
                   {
                       case ModelTypeEnum.工具:
                           break;
                       case ModelTypeEnum.设备:
                           MotionEngine.GetModule<ToolsPackManager>().DeleteStringToolsPack(btName, index);
                           MotionEngine.GetModule<ToolsPackManager>().DeleteToolsPackWindowItemBts(btName, index);
                           break;
                   }

                   MotionEngine.GetModule<ToolsPackManager>().GetToolsPackWindowTemp().SetActive(false);
               });





            if (this.name == "单相表三角钥匙")
            {
                closeBt.SetActive(false);

            }
            else if (this.name == "工作证")
            {
                closeBt.SetActive(false);
            }





        }

        public void Init(string gName, string btName, int index, ModelTypeEnum modelTypeEnum)
        {
            btNameText = gName;
            clickName = btName;
            // this.index = index;
            _modelTypeEnum = modelTypeEnum;
            ico.texture = MotionEngine.GetModule<ToolsPackManager>().GetToolsPackWindowBtImage(gName);
            numberText.text = $"x{gameNumber.ToString()}";

            _toolsPackWindowItemBtModels.Add(index, btName);

            // if (MotionEngine.GetModule<ToolsPackManager>().GetToolsPackScene() == ToolsPackScene.工具间)
            // {
            //     closeBt.SetActive(true);
            // }
            // else
            // {
            //     closeBt.SetActive(false);
            // }
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            modelNameGame.SetActive(true);
            modelNameGame.transform.Find("Text").GetComponent<TMP_Text>().text = this.name;
            // btImage.sprite = clickSprite;

            if (MotionEngine.GetModule<ToolsPackManager>().GetToolsPackScene() == ToolsPackScene.工具间)
            {
                if (MotionEngine.GetModule<DataConfigManager>().GetProcessMode() != DefaultNamespace.ProcessMode.ProcessMode.Teaching)
                {

                    if (this.name == "单相表三角钥匙")
                    {
                        closeBt.SetActive(false);

                    }
                    else if (this.name == "工作证")
                    {
                        closeBt.SetActive(false);
                    }
                    else
                        closeBt.SetActive(true);
                }

            }
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            modelNameGame.SetActive(false);
            // btImage.sprite = defaultSprite;
            if (MotionEngine.GetModule<ToolsPackManager>().GetToolsPackScene() == ToolsPackScene.工具间)
            {
                closeBt.SetActive(false);
            }
        }

        public int GetIndex()
        {
            return _toolsPackWindowItemBtModels.Count - 1;
        }


        public int GetGameNumber()
        {
            return gameNumber;
        }

        /// <summary>
        /// 添加物体堆叠数
        /// </summary>
        /// <param name="btName"></param>
        /// <param name="index"></param>
        public void AddGameNumber(string btName, int index)
        {
            gameNumber++;
            numberText.text = $"x{gameNumber.ToString()}";
            _toolsPackWindowItemBtModels[index] = btName;
            //_toolsPackWindowItemBtModels.Add(index, btName);
        }

        /// <summary>
        ///  减去堆叠数
        /// </summary>
        public void ReduceGameNumber()
        {
            string na = this.gameObject.name;
            gameNumber--;
            numberText.text = $"x{gameNumber.ToString()}";
            _toolsPackWindowItemBtModels.Remove(_toolsPackWindowItemBtModels.Count - 1);
        }
    }
}