using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using SVGImporter;
using I2.Loc;

public class ActionButtonRadial : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler {

    public SVGImage icon;
    public ActionAsset action;
    public ActionsRadial actionsRadial;
    public Tile targetTile;

    public void Init(ActionAsset action, Tile targetTile)
    {
        this.action = action;
        this.targetTile = targetTile;
        icon.vectorGraphics = action.icon;
        gameObject.SetActive(true);
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if (actionsRadial.rectTransform.localScale.x < 0.9f)
            return;
        
        actionsRadial.descriptionLabel.Term = action.descriptionKey;
        actionsRadial.descriptionPanel.gameObject.SetActive(true);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        actionsRadial.descriptionPanel.gameObject.SetActive(false);
    }
        
    public void OnPointerDown(PointerEventData eventData)
    {
        if (action.energyCost > 0
           && GameController.Instance.energy < action.energyCost)
            return;
        
        action.Action(targetTile);
        actionsRadial.Close();
    }

    void Update()
    {
        if (action.energyCost > 0)
        {
            if (GameController.Instance.energy < action.energyCost && icon.color != Color.red)
                icon.color = Color.red;
            else if (GameController.Instance.energy >= action.energyCost && icon.color != Color.white)
                icon.color = Color.white;
        }

        if (action.moneyCost > 0)
        {
            if (GameController.Instance.money < action.moneyCost && icon.color != Color.red)
                icon.color = Color.red;
            else if (GameController.Instance.money >= action.moneyCost && icon.color != Color.white)
                icon.color = Color.white;
        }
    }

}
