using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class ClickHandler : MonoBehaviour
{
    [SerializeField] Texture2D cursorNormalSprite;
    [SerializeField] Texture2D cursorActionSprite;

    [SerializeField] SpriteRenderer spriteRenderer;
    [SerializeField] Sprite itemNormalSprite;
    [SerializeField] Sprite itemHighlightSprite;

    public Vector3 position;

    private bool exec;
    
    public UnityEvent clickEvent;

    private void Start()
    {
        if (position == Vector3.zero)
        {
            exec = true;
            position = transform.position;
        }
    }

    private void Update()
    {
        if (!exec)
            return;
        
        position = transform.position;
    }

    void OnMouseDown()
    {
        if (clickEvent != null)
        {
            clickEvent.Invoke();
        }
    }

    private void OnMouseEnter()
    {
        CursorHandler.ChangeCursor(cursorActionSprite);
        spriteRenderer.sprite = itemHighlightSprite;
    }

    private void OnMouseExit()
    {
        CursorHandler.ChangeCursor(cursorNormalSprite);
        spriteRenderer.sprite = itemNormalSprite;
    }
}
