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

public class PlayerController : MonoBehaviour
{
    [Header("Movement Settings")]
    public float moveSpeed = 5f;
    public float speedMultiplier = 1f;

    [Header("Ground Check")]
    public bool isGrounded = false;
    public Transform groundCheckTransform;
    public float groundCheckRadius = 0.25f;
    public LayerMask groundLayerMask = 1;

    // Components
    public Animator anim;
    public Rigidbody rb;
    private PlayerActionManager actionManager;

    // Input and movement state
    private Vector2 moveInput;
    private bool isMoving;

    // Animation triggers
    [HideInInspector] public bool jumpTrigger = false;
    [HideInInspector] public bool isHanging = false;
    [HideInInspector] public Vector3 externalVelocity = Vector3.zero;

    // AGGIUNTO: Rilevamento piattaforma mobile
    private bool isOnMovingPlatform = false;
    private Vector3 platformVelocity = Vector3.zero;

    private void Awake()
    {
        rb = GetComponent<Rigidbody>();
        anim = GetComponent<Animator>();
        actionManager = GetComponent<PlayerActionManager>();
    }

    private void Update()
    {
        HandleInput();
        CheckGroundStatus();
    }

    private void FixedUpdate()
    {
        HandleMovement();
        UpdateAnimations();
    }

    private void HandleInput()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        moveInput = new Vector2(horizontal, vertical);

        isMoving = moveInput.magnitude > 0.1f;

        // Action Handler
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (actionManager != null) 
            {
                actionManager.ExecuteSelectedAction();
            }
        }

        if (Input.GetKeyDown(KeyCode.Tab))
        {
            if (actionManager != null) 
            {
                actionManager.SelectNextAction();
            }
        }
    }

    private void HandleMovement()
    {
        if (!isMoving)
        {
            Vector3 velocity = rb.velocity;
            velocity.x = 0f;
            velocity.z = 0f;
            rb.velocity = velocity;
            return;
        }

        // Movement relative to the camera
        Vector3 cameraForward = Camera.main.transform.forward;
        Vector3 cameraRight = Camera.main.transform.right;

        cameraForward.y = 0f;
        cameraRight.y = 0f;
        cameraForward.Normalize();
        cameraRight.Normalize();

        Vector3 moveDirection = cameraForward * moveInput.y + cameraRight * moveInput.x;
        moveDirection.Normalize();

        Vector3 targetVelocity = moveDirection * moveSpeed * speedMultiplier;
        
        // targetVelocity += new Vector3(externalVelocity.x, 0, externalVelocity.z);
        targetVelocity.y = rb.velocity.y;
        rb.velocity = targetVelocity;
        
        if (moveDirection.magnitude > 0.1f)
        {
            Vector3 rotation = new Vector3(moveDirection.x, 0, moveDirection.z);
            transform.rotation = Quaternion.LookRotation(rotation);
        }

        externalVelocity = Vector3.zero;
    }

    private void CheckGroundStatus()
    {
        if (groundCheckTransform != null)
        {
            isGrounded = Physics.CheckSphere(groundCheckTransform.position, groundCheckRadius, groundLayerMask);
        }
    }
    
    private void UpdateAnimations()
    {
        anim.SetBool("isRunning", isMoving); 
        anim.SetBool("isHanging", isHanging); 

        if (jumpTrigger)
        {
            anim.SetTrigger("isJumping");
            jumpTrigger = false;
        }
    }
}