using System;
using JetBrains.Annotations;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;

public class Movement : MonoBehaviour
{
    Rigidbody2D rb; //ignore the unnecessary amount of stupid variables. I'll fix it.
    public float baseTargetVelocityVert;
    public float baseTargetVelocityHoriz;
    public float velocityVert;
    public float velocityHoriz;
    float targetVelocityVert;
    float targetVelocityHoriz;
    float forceToApplyVert;
    float forceToApplyHoriz;
    float forceToApplyVertMult;
    float forceToApplyHorizMult;
    float percentTargetVelReachedVert;
    float percentTargetVelReachedHoriz;
    public float brakeStrengthVert;
    public float brakeStrengthHoriz;
    float brakeForceVert;
    float brakeForceHoriz;
    float percentBrakeReachedVert;
    float percentBrakeReachedHoriz;
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        baseTargetVelocityHoriz = 10f;
        baseTargetVelocityVert = 15f;
        brakeForceHoriz = 10f;
        brakeForceVert = 10f;
    }

    // Update is called once per frame
    void Update()
    {
        //nothing here yet.

    }
    private void FixedUpdate()
    {
        velocityHoriz = (Vector3.Dot(rb.linearVelocity, transform.right));
        velocityVert = (Vector3.Dot(rb.linearVelocity, transform.up));
        if (rb != null)
        {
            targetVelocityVert = 0f;
            if (Keyboard.current.wKey.isPressed)
            {
                targetVelocityVert += 1f;
                Debug.Log("w is pressed");
            }
            
            if (Keyboard.current.sKey.isPressed)
            {
                targetVelocityVert -= 1f;
            }
            targetVelocityVert *= baseTargetVelocityVert; //this is the "speed i want to reach that is also the max speed" calculation for vertical movement.

            targetVelocityHoriz = 0f;
            if(Keyboard.current.aKey.isPressed)
            {
                targetVelocityHoriz -= 1f; 
            }
            if(Keyboard.current.dKey.isPressed)
            {
                targetVelocityHoriz += 1f;
            }
            targetVelocityHoriz *= baseTargetVelocityHoriz; //this is the "speed i want to reach that is also the max speed" calculation for horizontal movement.

            if(targetVelocityVert != 0f)
            {
               
                if (velocityVert < targetVelocityVert) //if not at max vertical speed, get there.
                {
                    
                    percentTargetVelReachedVert = (Mathf.Abs(velocityVert * 100)) / targetVelocityVert;

                    forceToApplyVertMult = (1f - percentTargetVelReachedVert);

                    
                }
                else
                {
                    forceToApplyVertMult = 0f;
                }
                rb.AddForce(transform.up * (targetVelocityVert * forceToApplyVert));
            }
            else //if the speed we want to get to is 0 (because nothing is pressed) brake.
            {
                if(Mathf.Abs(velocityVert) < 0.5f)
                {
                    rb.linearVelocity -= (Vector2)(transform.up * velocityVert);
                }
                else
                {
                    brakeForceVert = brakeStrengthVert * Mathf.Sign(velocityVert) * -1f;
                    rb.AddForce(transform.up * (brakeForceVert / 10f));
                }
                    

            }

            if(targetVelocityHoriz != 0f)
            {
                if (velocityHoriz < targetVelocityHoriz) //if not at max horizontal speed, get there.
                {
                   
                    percentTargetVelReachedHoriz = (Mathf.Abs(velocityHoriz * 100f)) / targetVelocityHoriz; //1

                    forceToApplyHorizMult = (1f - percentTargetVelReachedHoriz); 
                }
                else
                {
                    forceToApplyHorizMult = 0f;
                }
                rb.AddForce(transform.right * (targetVelocityHoriz * forceToApplyHoriz));
            }
            else //if the speed we want to get to is 0 (because nothing is pressed) brake.
            {
                if (Mathf.Abs(velocityHoriz) < 0.5f)
                {
                    rb.linearVelocity -= (Vector2)(transform.right * velocityHoriz);
                }
                else
                {
                    brakeForceHoriz = brakeStrengthHoriz * Mathf.Sign(velocityHoriz) * -1f;
                    rb.AddForce(transform.right * (brakeForceHoriz / 10f));
                }
            }
            
        }
    }
}