using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using DG.Tweening;
using UnityEngine;

public class MartiController : MonoBehaviour
{
    public bool CanMove = true;

    [Header("Movement Parameters")]
    [SerializeField] private float moveSpeed = 5f;
    [SerializeField] private float turnSpeed = 5f;
    [SerializeField] private float jumpForce = 5f;
    [SerializeField] private float gravity = 5f;
    [SerializeField] private float maxSpeed = 20f;


    [Header("Look Parameters")]
    [SerializeField, Range(1, 100)] private float lookSensitivity = 2f;

    // private CharacterController characterController;
    private Rigidbody rb;
    private Animator animator;

    public Vector3 moveDirection;
    private Vector2 currentInput;

    public GameObject poopPrefab;

    private void Awake()
    {
        rb = GetComponent<Rigidbody>();
        animator = GetComponent<Animator>();
        // characterController = GetComponent<CharacterController>();
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    private void Update()
    {
        if (CanMove)
        {
            HandleLook();
            HandleJump();
            HandleRotation();
            HandleMovement();
            HandlePoopMechanic();
            if (moveDirection != Vector3.zero)
            {
                rb.AddForce(moveDirection * Time.deltaTime, ForceMode.VelocityChange);

                animator.SetBool("isMoving", true);
                if (new Vector2(moveDirection.x, moveDirection.z).magnitude < 1f && moveDirection.y < 0.1f)
                {
                    animator.SetBool("isMoving", false);
                }
            }

            // playerCamera.transform.position = transform.position - new Vector3(0f, -5f, 10f);
        }
    }

    private void HandlePoopMechanic()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            var poop = Instantiate(poopPrefab, transform.position, Quaternion.AngleAxis(90f, Vector3.right));
            gameObject.GetComponent<BarController>().CurrentFood -= 10f;
        }
    }

    private void HandleRotation()
    {
        var horizontal = Input.GetAxisRaw("Horizontal");

        var force = horizontal * transform.right * 0.2f;
        force.y = 0f;

        rb.AddForce(force, ForceMode.Impulse);

        var euler = transform.rotation.eulerAngles;
        euler.z = -22.5f * horizontal;
        transform.DORotate(euler, .6f);

    }


    private void HandleMovement()
    {
        currentInput = new Vector2(moveSpeed * Input.GetAxisRaw("Vertical"), 0f);

        if (currentInput.x == 0)
        {
            moveDirection -= 20 * Time.deltaTime * transform.forward;
        }
        else
        {
            moveDirection += currentInput.x * Time.deltaTime * transform.forward;
            moveDirection = Vector3.ClampMagnitude(moveDirection, maxSpeed);
        }
    }

    private void HandleJump()
    {
        bool isJumped = Input.GetButton("Jump");
        if (isJumped)
        {
            moveDirection.y += jumpForce * Time.deltaTime * 2f;
            moveDirection.y = Mathf.Clamp(moveDirection.y, 0f, jumpForce);
        }
        else
        {
            moveDirection.y -= gravity * Time.deltaTime * moveDirection.y > 0f ? 2f : 1f;
            moveDirection.y = Mathf.Clamp(moveDirection.y, -gravity * 2, jumpForce);
        }

        if (transform.position.y < 0.05f)
        {
            // var euler = transform.rotation.eulerAngles;
            // euler.x = 0;
            // transform.rotation = Quaternion.Euler(euler);
            moveDirection.y = Mathf.Clamp(moveDirection.y, 0f, moveDirection.y);
        }

    }

    private void HandleLook()
    {

        var rotY = Input.GetAxis("Mouse Y") * lookSensitivity;
        var rotX = Input.GetAxis("Mouse X") * lookSensitivity;

        var euler = transform.rotation.eulerAngles;

        // euler.x += rotY;
        euler.y += rotX;

        transform.rotation = Quaternion.Euler(euler);
        var oldY = moveDirection.y;
        var mag = new Vector2(moveDirection.x, moveDirection.z).magnitude;
        moveDirection = mag * new Vector3(transform.forward.x, 0f, transform.forward.z);
        moveDirection.y = oldY;
    }

}