using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem.LowLevel;

public class PlayerController : MonoBehaviour
{
    public Animator playerAnimator;
    public Rigidbody playerRigidbody;

    public float walkSpeed, walkBackwardSpeed, oldWalkSpeed, runSpeed, rotateSpeed;
    public float jumpHeight = 500f;

    public bool isRunning = false;
    public bool canRun = true;
    public bool isWalking = false;
    public bool hasJumped = false;
    public bool isGrounded = true;

    public Transform playerTransform;

    private void FixedUpdate()
    {
        if (Input.GetKey(KeyCode.W))
        {
            playerRigidbody.linearVelocity = transform.forward * walkSpeed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.S))
        {
            playerRigidbody.linearVelocity = -transform.forward * walkBackwardSpeed * Time.deltaTime;
        }
    }

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.W))
        {
            playerAnimator.SetBool("IsWalking", true);
            isWalking = true;
        }
        if (Input.GetKeyUp(KeyCode.W))
        {
            playerAnimator.SetBool("IsWalking", false);
            isWalking = false;
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            playerAnimator.SetBool("IsWalkingBackward", true);
        }
        if (Input.GetKeyUp(KeyCode.S)) 
        {
            playerAnimator.SetBool("IsWalkingBackward", false);
        }
        if (Input.GetKey(KeyCode.A))
        {
            playerTransform.Rotate(0, -rotateSpeed * Time.deltaTime, 0);
        }
        if(Input.GetKey(KeyCode.D))
        {
            playerTransform.Rotate(0, rotateSpeed * Time.deltaTime, 0);
        }

        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            playerRigidbody.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
            playerAnimator.SetTrigger("Jump");

            isGrounded = false;
            playerAnimator.SetBool("IsGrounded", false);
        }

        if (isWalking && canRun)
        {
            if (Input.GetKeyDown(KeyCode.LeftShift))
            {
                walkSpeed = walkSpeed + runSpeed; 
                playerAnimator.SetBool("IsRunning", true);
                isRunning = true;
            }
            if (Input.GetKeyUp(KeyCode.LeftShift))
            {
                walkSpeed = oldWalkSpeed;
                playerAnimator.SetBool("IsRunning", false);
                isRunning = false;
            }
        }
        else
        {
            playerAnimator.SetBool("IsRunning", false);
            isRunning = false;
        }
    }

    private void OnCollisionStay(Collision collision)
    {
        foreach(ContactPoint contact in collision.contacts)
        {
            if (contact.normal.y > 0.9f)
            {
                isGrounded = true;
                playerAnimator.SetBool("IsGrounded", true);
                return;
            }
        }
    }

    private void OnCollisionExit(Collision collision)
    {
        isGrounded = false;
        playerAnimator.SetBool("IsGrounded", false);
    }
}
