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

public class BallController : MonoBehaviour
{

    private GameObject Ball;
    float startTime, endTime, swpiteDistance, swipeTime;
    private Vector2 startPos;
    private Vector2 endPos;

    public float MinSwipDist = 0;
    private float BallVelocity = 0;
    private float BallSpeed = 0;
    public float BallMaxSpeed = 350;
    private Vector3 angle;

    [SerializeField] private bool thrown, holding;
    private Vector3 newPositon;

    //public float smooth = 0.7f;
    Rigidbody rb;
    // Start is called before the first frame update
    void Start()
    {
        setUpBall();
    }

    private void setUpBall()
    {
        GameObject _ball = GameObject.FindGameObjectWithTag("Ball");
        Ball = _ball;
        rb = GetComponent<Rigidbody>();
        ResetBall();
    }

    private void ResetBall()
    {
        angle = Vector3.zero;
        // endPos = new Vector3(0, 0.2, 70);
        // startPos = new Vector3(0, 0.2, 70);
        BallSpeed = 0;
        startTime = 0;
        endTime = 0;
        swipeTime = 0;
        swpiteDistance = 0;

        rb.velocity = Vector3.zero;
        rb.useGravity = false;
        //  Ball.transform.position = new Vector3(0, 0.2, 70);
    }


    void Update()
    {
        // if (holding)
        //     PickUpTheBall();

        if (thrown)
            return;

        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, 100f))
            {
                // có thể dùng tag và layer ở đây
                if (hit.transform == Ball.transform)
                {
                    startTime = Time.time;
                    startPos = Input.mousePosition;
                    //holding = true;
                }
            }
        }
        else if (Input.GetMouseButtonUp(0))
        {
            endTime = Time.time;
            endPos = Input.mousePosition;
            swpiteDistance = (endPos - startPos).magnitude;
            swipeTime = endTime - startTime;

            if (swipeTime < 1f && swpiteDistance > 10f)
            //if (swipeTime < 0.5f && swpiteDistance > 30f)
            {
                //throw ball
                CalSpeed();
                CalAgle();

                // x cao z nhanh
                rb.AddForce(new Vector3((angle.x * BallSpeed), (angle.y * BallSpeed), (angle.z * BallSpeed)));
                rb.useGravity = true;
                holding = false;
                thrown = true;
                Invoke(nameof(ResetBall), 4f);
            }
            else
                ResetBall();
        }
    }

    private void CalAgle()
    {
        angle = Camera.main.ScreenToWorldPoint(new Vector3(endPos.x, endPos.y + 50f, (Camera.main.nearClipPlane + 30)));
    }

    void CalSpeed()
    {
        if (swipeTime > 0)
            BallVelocity = swpiteDistance / (swpiteDistance - swipeTime);

        BallSpeed = BallVelocity * 40;
        if (BallSpeed <= BallMaxSpeed)
        {
            BallSpeed = BallMaxSpeed;
        }
        //if (BallSpeed <= BallMaxSpeed)
        //{
        //    BallSpeed = BallMaxSpeed += 40;
        //}
        swipeTime = 0;
    }
}
