using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;

namespace Kryz.CharacterStats.Examples
{
	public class ItemSlot : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
	{
		[SerializeField] protected Image image;

		public UnityEvent_Item OnRightClickEvent = new UnityEvent_Item();

		protected Item _item;
		public Item Item {
			get { return _item; }
			set {
				_item = value;

				if (_item == null) {
					image.enabled = false;
				} else {
					image.sprite = _item.Icon;
					image.enabled = true;
				}
			}
		}

		public void OnPointerClick(PointerEventData eventData)
		{
			if (eventData != null && eventData.button == PointerEventData.InputButton.Right)
			{
				//Debug.LogError("PointerClick");
				//Debug.LogError(Item);
				//Debug.LogError(OnRightClickEvent);
				if (Item != null && OnRightClickEvent != null)
                {
					OnRightClickEvent.Invoke(Item);
					//Debug.LogError("right click: " + Item.ItemName);
                }
			}
		}

		protected virtual void OnValidate()
		{
			if (image == null)
				image = GetComponent<Image>();
		}

		public virtual void OnPointerEnter(PointerEventData eventData)
		{
			ItemTooltip.Instance.ShowTooltip(Item);
		}

		public void OnPointerExit(PointerEventData eventData)
		{
			ItemTooltip.Instance.HideTooltip();
		}
	}
}
