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

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

    float rotationspeed = 70;
    float screwTime;
    int screws;
    bool onScrew;
    public bool isFixed = false;
    public GameObject screw1;
    public GameObject screw2;
    public GameObject screw3;
    public GameObject screw4;
    private Vector2 lastMousePosition;

    public AudioClip drill_sound;
    private AudioSource source;


    public void resetScrewGame()
    {
        screwTime = 0;
        isFixed = false;
        screws = 0;
        screw1.SetActive(true);
        screw2.SetActive(true);
        screw3.SetActive(true);
        screw4.SetActive(true);
    }

    void Start()
    {
        source = GetComponent<AudioSource>();
        screwTime = 0;
        screws = 0;
    }

    void Update()
    {
        if (screws == 4)
        {
            isFixed = true;
        }
    }

    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.gameObject.name == "Screw1")
        {
            onScrew = true;

            if (Input.GetKey(KeyCode.Q) && onScrew == true)
            {
                if (!source.isPlaying)
                    source.PlayOneShot(drill_sound);
                transform.Rotate((Vector3.forward * Time.deltaTime) * rotationspeed);
                screwTime += Time.deltaTime;
                print(screwTime);
            }
            if (screwTime >= 1f && collision.gameObject.name == "Screw1")
            {
                screws += 1;
                screw1.SetActive(false);
                screwTime = 0;
            }
        }
        if (collision.gameObject.name == "Screw2")
        {
            onScrew = true;

            if (Input.GetKey(KeyCode.Q) && onScrew == true)
            {
                if (!source.isPlaying)
                    source.PlayOneShot(drill_sound);
                transform.Rotate((Vector3.forward * Time.deltaTime) * rotationspeed);
                screwTime += Time.deltaTime;
                print(screwTime);
            }
            if (screwTime >= 1f && collision.gameObject.name == "Screw2")
            {
                screws += 1;
                screw2.SetActive(false);
                screwTime = 0;
            }
        }
        if (collision.gameObject.name == "Screw3")
        {
            onScrew = true;

            if (Input.GetKey(KeyCode.Q) && onScrew == true)
            {
                if (!source.isPlaying)
                    source.PlayOneShot(drill_sound);
                transform.Rotate((Vector3.forward * Time.deltaTime) * rotationspeed);
                screwTime += Time.deltaTime;
                print(screwTime);
            }
            if (screwTime >= 1f && collision.gameObject.name == "Screw3")
            {
                screws += 1;
                screw3.SetActive(false);
                screwTime = 0;
            }
        }
        if (collision.gameObject.name == "Screw4")
        {
            onScrew = true;

            if (Input.GetKey(KeyCode.Q) && onScrew == true)
            {
                if (!source.isPlaying)
                    source.PlayOneShot(drill_sound);
                transform.Rotate((Vector3.forward * Time.deltaTime) * rotationspeed);
                screwTime += Time.deltaTime;
                print(screwTime);
            }
            if (screwTime >= 1f && collision.gameObject.name == "Screw4")
            {
                screws += 1;
                screw4.SetActive(false);
                screwTime = 0;
            }
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.name == "Screw1")
        {
            onScrew = false;
        }
        if (collision.gameObject.name == "Screw2")
        {
            onScrew = false;
        }
        if (collision.gameObject.name == "Screw3")
        {
            onScrew = false;
        }
        if (collision.gameObject.name == "Screw4")
        {
            onScrew = false;
        }
    }


    /// <summary>
    /// This method will be called on the start of the mouse drag
    /// </summary>
    /// <param name="eventData">mouse pointer event data</param>
    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
    }

    /// <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;
    }
}
