using System;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UpgradePlacementButton : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
{
    private Button _button;

    [SerializeField]
    private TMP_Text _foodLabel;
    [SerializeField]
    private TMP_Text _woodLabel;
    [SerializeField]
    private TMP_Text _rockLabel;
    [SerializeField]
    private Image _backgroundImage;
    [SerializeField]
    private Image _unitArt;
    [SerializeField]
    private GameObject _buttonContainer;
    [SerializeField]
    private GameObject _prefab;
    [SerializeField]
    protected UnitCard _unitCardInformation;
    [SerializeField]
    protected Sprite _rangeSprite;
    private GameObject _unitGameObject;
    private Canvas _canvas;
    private static GameObject _outline;

    public Button Button => _button;

    void Start()
    {
        _button = GetComponent<Button>();
        if (_unitCardInformation == null)
        {
            Destroy(gameObject);
        }
    }

    void Update()
    {
        if (_unitCardInformation != null)
        {
            _button.interactable = CanUse();
            if (CanUse())
            {
                _backgroundImage.color = Color.green;
            }
            else
            {
                _backgroundImage.color = Color.red;
            }
        }
    }

    private bool CanUse()
    {
        return ResourceManager.Instance.EnoughFor(_unitCardInformation.Rock, _unitCardInformation.Wood, _unitCardInformation.Food);
    }

    void SetTextFor(TMP_Text label, int value)
    {
        label.transform.parent.gameObject.SetActive(value > 0);
        label.text = "" + value;
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (CanUse())
        {
            var originalParent = _unitGameObject.transform.parent;
            Vector3 instantiatePosition = _unitGameObject.transform.position;
            Instantiate(_prefab, instantiatePosition, Quaternion.identity, originalParent);
            ResourceManager resourceManager = ResourceManager.Instance;
            resourceManager.Remove(_unitCardInformation.Rock, _unitCardInformation.Wood, _unitCardInformation.Food);
            Destroy(_unitGameObject);
            Destroy(_buttonContainer);
            HideTooltip();
        }
    }

    public void Initialize(UnitUpgrade unitUpgrade, GameObject parentGameObject, Canvas canvas)
    {
        Debug.Log(unitUpgrade.name);
        _unitCardInformation = unitUpgrade.UpgradeUnitCard;
        SetTextFor(_foodLabel, _unitCardInformation.Food);
        SetTextFor(_woodLabel, _unitCardInformation.Wood);
        SetTextFor(_rockLabel, _unitCardInformation.Rock);
        _prefab = unitUpgrade.UpgradePrefab;
        _unitGameObject = parentGameObject;
        _canvas = canvas;
        _unitArt.sprite = unitUpgrade.UpgradeCardArt;

        if (_outline == null && _unitGameObject.TryGetComponent(out House currentBuilding))
        {
            _outline = new GameObject("Outline");

            var rend = _outline.AddComponent<SpriteRenderer>();
            rend.sprite = _rangeSprite;
            rend.sortingLayerName = "Character";
            rend.sortingOrder = 1;
            rend.color = new Color(1, 1, 1, 0.2f);
        }
    }
    public void OnPointerEnter(PointerEventData eventData)
    {
        Ally allyObj = _prefab.GetComponent<Ally>();
        TooltipManager.Instance.ShowTooltip(_unitCardInformation, allyObj.Hp.ToString(), allyObj.AttackDamage.ToString(), allyObj.AttackInterval.ToString());
        if (_prefab.TryGetComponent(out ProductionBuilding hoveredBuilding))
        {
            if (_unitGameObject.TryGetComponent(out House currentBuilding))
            {
                var range = hoveredBuilding.Range * 2 + 1;
                currentBuilding.RangeOutline.size = new Vector2(range, range);

                foreach (var rend in currentBuilding.GetAllComponents<SpriteRenderer>())
                {
                    _outline.SetActive(true);
                    _outline.transform.SetParent(currentBuilding.transform);
                    _outline.transform.localPosition = new Vector3();
                    _outline.transform.localScale = currentBuilding.RangeOutline.size;
                    rend.material.SetColor("_OutlineColor", Color.blue);
                }
            }
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        HideTooltip();

        if (_outline)
        {
            _outline.SetActive(false);
        }
    }

    private void HideTooltip()
    {
        TooltipManager.Instance.HideToolTip();
    }
}