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

public class Poop : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerDownHandler
{
    private Rigidbody2D rbody;
    private SpriteRenderer spriteRenderer;

    private Trash currentTrash;

    public Trash CurrentTrash { get => currentTrash; set => currentTrash = value; }

    private void Start()
    {
    }

    private void Init()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        rbody = GetComponent<Rigidbody2D>();
    }

    private void OnEnable()
    {
        if (rbody == null)
            Init();

        rbody.bodyType = RigidbodyType2D.Dynamic;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log("collision");
        if (collision.gameObject.tag == "trash")
        {
            gameObject.SetActive(false);
            collision.gameObject.GetComponent<TrashBin>().TrashThrown(CurrentTrash);
        }
        if(collision.gameObject.tag == "Outside Bounds")
        {
            EventManager.Instance.Stress(spriteRenderer.sprite);
            gameObject.SetActive(false);
        }
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        rbody.bodyType = RigidbodyType2D.Static;
        Debug.Log("poooop");
       
    }

    public void OnDrag(PointerEventData eventData)
    {
        //Vector2 pos = Camera.main.ScreenToWorldPoint( eventData.pointerCurrentRaycast.screenPosition);
        Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        gameObject.transform.position = pos;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        Debug.Log("Vector2 pos = Camera.main.ScreenToWorldPoint(eventData.pointerCurrentRaycast.screenPosition);"+ Camera.main.ScreenToWorldPoint(eventData.pointerCurrentRaycast.screenPosition));
        Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        gameObject.transform.position = pos;
        rbody.bodyType = RigidbodyType2D.Dynamic;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        rbody.bodyType = RigidbodyType2D.Kinematic;
    }

}
