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

public class DragObject : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
    public GameObject leftX, rightX;
    private Vector2 lastMousePosition;
    private bool withinPosRange, withinRotationAngle;
    public int rotationspeed = 70;
    public Image Bar;

    public AudioClip pour_drink;
    private AudioSource source;

    private Vector3 initPos;

    void Start()
    {
        initPos = transform.position;
        source = GetComponent<AudioSource>();
    }

    void Update()
    {
        withinPosRange = transform.position.x > leftX.transform.position.x && transform.position.x < rightX.transform.position.x;
        withinRotationAngle = transform.eulerAngles.z > 120 && transform.eulerAngles.z < 180;
      
        rotateOil();
        pourOil();
    }

    void pourOil()
    {
        if (withinPosRange && withinRotationAngle)
        {
            if (!source.isPlaying)
                source.PlayOneShot(pour_drink);

            Bar.fillAmount += Time.deltaTime * 0.15f;
        }
        else
            source.Stop();
    }


    private void rotateOil()
    {
        if (Input.GetKey(KeyCode.Q))
            transform.Rotate((Vector3.forward * Time.deltaTime) * rotationspeed);
        else if (Input.GetKey(KeyCode.W))
            transform.Rotate((Vector3.forward * Time.deltaTime) * -rotationspeed);
    }


    // ==================================================== Drag Funcrtion ==========================================================
    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("Begin Drag");
        lastMousePosition = eventData.position;
    }

    /// <summary>
    /// This method will be called during the mouse drag
    /// </summary>
    /// <param name="eventData">mouse pointer event data</param>
    public void OnDrag(PointerEventData eventData)
    {
        Vector2 currentMousePosition = eventData.position;
        Vector2 diff = currentMousePosition - lastMousePosition;
        RectTransform rect = GetComponent<RectTransform>();

        Vector3 newPosition = rect.position + new Vector3(diff.x, diff.y, transform.position.z);
        Vector3 oldPos = rect.position;
        rect.position = newPosition;
        if (!IsRectTransformInsideSreen(rect))
        {
            rect.position = oldPos;
        }
        lastMousePosition = currentMousePosition;
    }

    /// <summary>
    /// This method will be called at the end of mouse drag
    /// </summary>
    /// <param name="eventData"></param>
    public void OnEndDrag(PointerEventData eventData)
    {
        Debug.Log("End Drag");

        //Implement your funtionlity here
        transform.position = initPos;
        transform.eulerAngles = new Vector3(0, 0, 0);
    }

    /// <summary>
    /// This methods will check is the rect transform is inside the screen or not
    /// </summary>
    /// <param name="rectTransform">Rect Trasform</param>
    /// <returns></returns>
    private bool IsRectTransformInsideSreen(RectTransform rectTransform)
    {
        bool isInside = false;
        Vector3[] corners = new Vector3[4];
        rectTransform.GetWorldCorners(corners);
        int visibleCorners = 0;
        Rect rect = new Rect(0, 0, Screen.width, Screen.height);
        foreach (Vector3 corner in corners)
        {
            if (rect.Contains(corner))
            {
                visibleCorners++;
            }
        }
        if (visibleCorners == 4)
        {
            isInside = true;
        }
        return isInside;
    }
}
