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

public class SwipeInput : MonoBehaviour
{
    public float AllowedTime = 2f;
    float LastTapTime = 0f;

    public float DeadZone = 380f;
    Vector3 CurrentPos;
    [SerializeField] Vector3 EndPos;

    public bool SwipeUp = false;
    public bool SwipeDown = false;
    public bool SwipeLeft = false;
    public bool SwipeRight = false;

    void SwipeDirection()
    {
        float xAbs = Mathf.Abs(EndPos.x);
        float yAbs = Mathf.Abs(EndPos.y);

        if(xAbs > yAbs)
        {
            if (EndPos.x> CurrentPos.x)
            {
                SwipeRight = true;
            }
            else
            {
                SwipeLeft = true;
            }
            //SwipeRight = SwipeLeft = false;
        }
        else
        {
            if (EndPos.y> CurrentPos.y)
            {
                SwipeUp = true;
            }
            else
            {
                SwipeDown = true;
            }
            //SwipeUp = SwipeDown = false;
        }

    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //To Store the first touch position
        if (Input.GetMouseButtonDown(0))
        {
            CurrentPos = Input.mousePosition;
        }

        
        if (Input.GetMouseButtonUp(0))
        {
            //Debug.Log("im here");
            EndPos = Input.mousePosition - CurrentPos;//to store finger lifted position
            float CurrDragDistSqr = Vector3.SqrMagnitude(EndPos);//calculate Distance

            float DeadZoneSqr = DeadZone * DeadZone;//min distance
            if(EndPos != Vector3.zero && CurrDragDistSqr > DeadZoneSqr)
            {
                if(Time.time - LastTapTime < AllowedTime)
                {
                    SwipeDirection();
                }
                LastTapTime = Time.time;
            }
        }
    }
}