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

public class Creature : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler {

    Weapon weapon;
    int health;
    private bool isDragged = false;
    private CanvasGroup canvasGroup;
    private RectTransform rectTransform;
    [SerializeField] GameObject creatureSprite;

    private void Start() {
        canvasGroup = GetComponent<CanvasGroup>();
        rectTransform = GetComponent<RectTransform>();
    }

    public void OnEndDrag(PointerEventData eventData) {
        canvasGroup.alpha = 1f;
        canvasGroup.blocksRaycasts = true;
    }

    public void OnBeginDrag(PointerEventData eventData) {
        canvasGroup.alpha = 0.6f;
        canvasGroup.blocksRaycasts = false;
    }

    public void OnDrag(PointerEventData eventData) {
        //rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
        rectTransform.position = Input.mousePosition;

        RaycastHit2D hit = Physics2D.Raycast(rectTransform.position, Vector2.down);
        if (hit.collider != null) {
            Debug.Log("Raycast hit " + hit.collider.name);
            if (hit.collider.tag.Equals("Tile")) {
                rectTransform.sizeDelta = new Vector2(hit.collider.gameObject.GetComponent<RectTransform>().rect.width, hit.collider.gameObject.GetComponent<RectTransform>().rect.height);
            }
        }
    }
}
