using UnityEngine;
using UnityEngine.Rendering;

public class PlayerController : MonoBehaviour
{
    [Header("Movement")]
    public float walkSpeed = 6f;
    public float sprintSpeed = 10f;
    private float currentSpeed;

    [Header("Verticality")]
    public float gravity = -9.81f;
    public float jumpHeight = 2f;

    [Header("Camera")]
    public float mouseSensitivity = 200f;
    public Transform cameraHolder;
    private float xRotation = 0f;

    [Header("Stamina")]
    public int Stamina = 5;
    public int maxStamina = 5;
    private float staminaTimer = 0f;
    private float regenTimer = 0f;

    private CharacterController controller;
    private Vector3 velocity;
    private bool isGrounded;

    void Start()
    {
        controller = GetComponent<CharacterController>();

        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        float x = Input.GetAxis("Horizontal");
        float y = Input.GetAxis("Vertical");
        bool isMoving = (x != 0 || y != 0);
        bool isSprinting = Input.GetKey(KeyCode.LeftShift);

        if (!isMoving && !isSprinting && Stamina < maxStamina)
        {
            regenTimer += Time.deltaTime;

            if (regenTimer >= 1f) {
                Stamina++;
                regenTimer = 0f;
            }
        }
        else
        {
            regenTimer = 0f;
        }

        Camera();
        Movement();
    }

    void Camera()
    {
        // Camera control of player
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        transform.Rotate(Vector3.up * mouseX);

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -80f, 80f);

        cameraHolder.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    }

    void Movement()
    {
        // Movement of the player & sprint
        bool isSprinting = Input.GetKey(KeyCode.LeftShift);

        isGrounded = controller.isGrounded;
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        Vector3 move = transform.right * x + transform.forward * z;

        if (isSprinting && Stamina > 0)
        {
            currentSpeed = sprintSpeed;
            staminaTimer += Time.deltaTime;

            if (staminaTimer >= 0.5f)
            {
                Stamina--;
                staminaTimer = 0f;
            }
        }
        else
        {
            currentSpeed = walkSpeed;
            staminaTimer = 0f;
        }

        controller.Move(move * currentSpeed * Time.deltaTime);

        // Jumping
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }
}
