using Costumes;
using Costumes.Config;
using Managers;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace UI
{
    public class CostumePanel : MonoBehaviour
    {
        [SerializeField] private CostumeConfig costumeConfig;

        [SerializeField] private GameObject panel;
        [SerializeField] private Image costumeImage;
        [SerializeField] private TextMeshProUGUI costumeNameText;
        [SerializeField] private TextMeshProUGUI costumeAmountText;
        [SerializeField] private TextMeshProUGUI costumeIndexText;
        [SerializeField] private TextMeshProUGUI buyCostumePriceText;
        [SerializeField] private TextMeshProUGUI sellCostumePriceText;
        [SerializeField] private Button prevCostumeButton;
        [SerializeField] private Button nextCostumeButton;
        [SerializeField] private Button buyCostumeButton;
        [SerializeField] private Button sellCostumeButton;

        private int currentCostumeIndex = 0;
        private int costumesAmount;

        private Costumes.Config.CostumeData costumeData;

        private List<(CostumeType, int)> costumes = new List<(CostumeType, int)>();

        void Start()
        {
            costumesAmount = costumeConfig.data.Count;

            costumeIndexText.text = $"{currentCostumeIndex + 1}/{costumesAmount}";

            prevCostumeButton.onClick.AddListener(PrevCostume);
            nextCostumeButton.onClick.AddListener(NextCostume);

            buyCostumeButton.onClick.AddListener(BuyCostume);
            sellCostumeButton.onClick.AddListener(SellCostume);
        }

        public void Show(bool v = true)
        {
            panel.SetActive(v);

            costumes = GameManager.Instance.GetCostumeByType();

            UpdateUI();
        }

        private void NextCostume()
        {
            currentCostumeIndex++;

            if (currentCostumeIndex == costumesAmount)
                currentCostumeIndex = 0;

            UpdateUI();
            Sound.SoundController.Instance.Play(SoundType.UIClick);
        }

        private void PrevCostume()
        {
            currentCostumeIndex--;

            if (currentCostumeIndex < 0)
                currentCostumeIndex = costumesAmount - 1;

            UpdateUI();
            Sound.SoundController.Instance.Play(SoundType.UIClick);
        }

        private void UpdateUI()
        {
            costumeData = costumeConfig.data[currentCostumeIndex];

            costumeIndexText.text = $"{currentCostumeIndex + 1}/{costumesAmount}";
            costumeNameText.text = costumeData.name;
            costumeImage.sprite = costumeData.icon;

            costumeAmountText.text = costumes.Find(x => x.Item1 == costumeConfig.data[currentCostumeIndex].type).Item2.ToString();

            buyCostumePriceText.text = $"{costumeData.buyPrice}$";
            sellCostumePriceText.text = $"{costumeData.sellPrice}$";

            buyCostumeButton.interactable = DataManager.Instance.Budget >= costumeData.buyPrice;
            sellCostumeButton.interactable = costumes.Find(x => x.Item1 == costumeConfig.data[currentCostumeIndex].type).Item2 > 0;
        }

        private void BuyCostume()
        {
            GameManager.Instance.BuyCostume(costumeData.type);

            costumes = GameManager.Instance.GetCostumeByType();

            UpdateUI();
            Sound.SoundController.Instance.Play(SoundType.UIClick);
        }

        private void SellCostume()
        {
            GameManager.Instance.SellCostume(costumeData.type);

            costumes = GameManager.Instance.GetCostumeByType();

            UpdateUI();
            Sound.SoundController.Instance.Play(SoundType.UIClick);
        }
    }
}