using UnityEngine;
using UnityEngine.InputSystem;
using System;
using UnityEngine.Events;

/// <summary>
/// Player controller.
/// </summary>
[RequireComponent(typeof(CharacterController))]
[DefaultExecutionOrder(1000)]
public class Player : MonoBehaviour
{
    public static Player Instance { get; private set; }

    public enum PlayerState
    {
        Idle,
        Moving,
        Jumping,
        Falling,
        Stunned,
        Eliminated,
        Loser,
        Winner,
    }

    [System.Serializable]
    public class Settings
    {
        [Header("Movements")]

        [Tooltip("Movement speed in km/h")]
        public float Speed = 18f;

        [Tooltip("Jump force in m/s")]
        public float JumpForce = 8f;

        [Tooltip("Player rotation speed towards movement direction")]
        public float RotationSpeed = 10f;

        [Tooltip("Ground detection tolerance")]
        public float GroundTolerance = 0.2f;

        [Tooltip("Layers considered as ground")]
        public LayerMask GroundLayer = 1;

        [Tooltip("Layers considered as death zone")]
        public LayerMask DeathLayer = 0;

        [Header("Forces")]

        [Tooltip("Decay rate of extra forces (m/s²)")]
        public float ExtraForcesDrag = 8f;

        [Header("Debug")]

        [Tooltip("GUI logs of current state")]
        public bool StateLogs;
    }

    [System.Serializable]
    public class References
    {
        public CharacterController Controller;
        public InputActionAsset InputActions;
    }

    [System.Serializable]
    public class StateContainer
    {
        [Tooltip("Current player state")]
        public PlayerState CurrentState = PlayerState.Idle;

        [Tooltip("Is player paused?")]
        public bool IsPaused = false;

        [Tooltip("Is player grounded?")]
        public bool IsGrounded;

        [Tooltip("Current velocity in m/s")]
        public Vector3 Velocity;

        [Tooltip("Ground transform evaluated as parent")]
        public Transform Ground;

        public float VerticalVelocity => Velocity.y;
        public Vector3 HorizontalVelocity => new Vector3(Velocity.x, 0, Velocity.z);
    }

    [System.Serializable]
    public class HealthContainer
    {
        [Tooltip("Maximum health points")]
        public int MaxHealth = 3;

        [Tooltip("Current health points")]
        public int CurrentHealth;

        [Tooltip("Event on definitive die")]
        public UnityEvent OnDie;
    }

    [SerializeField] private Settings _settings;
    [SerializeField] private References _references;
    [SerializeField] private StateContainer _state;
    [SerializeField] private HealthContainer _health;

    public StateContainer State => _state;
    public HealthContainer Health => _health;

    #region Private Fields
    private InputAction _moveAction;
    private InputAction _jumpAction;

    private Camera _camera;

    // Ground check geometry
    private Vector3 _groundCheckRayOffset;
    private Vector3 _groundCheckSphereOffset;
    private float _groundCheckRadius;

    // OverlapSphere results buffer (non-alloc)
    private Collider[] _overlapResults = new Collider[1];

    // Platform tracking
    private Vector3 _lastPlatformPosition;
    private Quaternion _lastPlatformRotation;

    // External forces
    private Vector3 _impulseForce;     // AddForce: decays automatically each frame
    private Vector3 _persistentForce;  // SetForce: maintained by caller
    private Vector3 _platformVelocity; // Inherited on leaving platform: decays automatically

    // Events
    public event Action<PlayerState, PlayerState> OnStateChanged;
    #endregion

    #region Constants
    private const float KMH_TO_MS = 1 / 3.6f;
    private const float STICK_FORCE = -5f;
    private const float GRAVITY = -20f;
    private const float MAX_GRAVITY = -50f;
    #endregion

    #region Unity Debug
    void OnGUI()
    {
        if (_settings.StateLogs)
        {
            GUIStyle style = new GUIStyle();
            style.fontSize = 15;
            style.normal.textColor = Color.white;
            style.alignment = TextAnchor.UpperLeft;

            GUILayout.BeginArea(new Rect(10, 10, 400, 200));
            GUILayout.Label($"State: {_state.CurrentState}", style);
            GUILayout.Label($"Grounded: {_state.IsGrounded}", style);
            GUILayout.Label($"Velocity: {_state.Velocity.magnitude:F2} m/s", style);
            GUILayout.EndArea();
        }
    }

    void OnDrawGizmos()
    {
        Color c = _state.IsGrounded ? Color.green : new Color(1, .5f, 0);

        Gizmos.color = c;
        Gizmos.DrawRay(transform.position + _groundCheckRayOffset, Vector3.down * (_settings.GroundTolerance * 2f));
        Gizmos.DrawWireSphere(transform.position + _groundCheckSphereOffset, _groundCheckRadius);
    }
    #endregion

    #region Unity Lifecycle
    void OnControllerColliderHit(ControllerColliderHit hit)
    {
        if ((_settings.DeathLayer & (1 << hit.gameObject.layer)) != 0)
        {
            Eliminate();
        }
    }

    void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
            return;
        }
        Instance = this;

        if (_references.Controller == null)
            _references.Controller = GetComponent<CharacterController>();

        _camera = Camera.main;

        _moveAction = _references.InputActions.FindActionMap("Player").FindAction("Move");
        _jumpAction = _references.InputActions.FindActionMap("Player").FindAction("Jump");

        _references.Controller.skinWidth = 0.08f;
        _references.Controller.minMoveDistance = 0f;
        _references.Controller.stepOffset = 0.4f;

        CharacterController cc = _references.Controller;
        _groundCheckRayOffset = cc.center + Vector3.up * (-cc.height * .5f - cc.skinWidth + _settings.GroundTolerance);
        _groundCheckSphereOffset = cc.center + Vector3.up * (-cc.height * .5f + cc.radius - cc.skinWidth - _settings.GroundTolerance);
        _groundCheckRadius = cc.radius;

        _health.CurrentHealth = _health.MaxHealth;
    }

    void OnEnable()
    {
        _moveAction?.Enable();
        _jumpAction?.Enable();
    }

    void OnDisable()
    {
        _moveAction?.Disable();
        _jumpAction?.Disable();
    }

    void Update()
    {
        if (_state.IsPaused) return;
        if (!_references.Controller.enabled) return;

        float deltaTime = Time.deltaTime;

        SetPlatformOffset();
        SetGravity(deltaTime);
        SetVelocity(deltaTime);
        SetJump();
        SetMovement(deltaTime);
        SetState();
    }
    #endregion

    #region Physics
    /// <summary>
    /// Applies a one-shot impulse in any direction.
    /// Decays automatically at ExtraForcesDrag rate.
    /// Suitable for trampolines, explosions, knockbacks.
    /// </summary>
    public void AddForce(Vector3 force)
    {
        _impulseForce += force;
    }

    /// <summary>
    /// Sets a persistent force applied every frame until cleared.
    /// Caller is responsible for resetting to Vector3.zero.
    /// Suitable for conveyor belts, jetpacks, wind zones.
    /// </summary>
    public void SetForce(Vector3 force)
    {
        _persistentForce = force;

        if (force.y > 0)
            _state.Velocity.y = 0;
    }
    #endregion

    #region Public Methods
    /// <summary>
    /// Teleport player to position.
    /// </summary>
    public void Teleport(Vector3 position)
    {
        _references.Controller.enabled = false;

        _state.Velocity = _impulseForce = _persistentForce = _platformVelocity = Vector3.zero;
        _state.Ground = null;

        transform.position = position;
        
        _references.Controller.enabled = true;
    }

    /// <summary>
    /// Sets the pause state of the player.
    /// </summary>
    public void Pause(bool paused)
    {
        _state.IsPaused = paused;
    }

    /// <summary>
    /// Stuns for a specified duration (0 = infinite).
    /// </summary>
    public void Stun(float duration = 0)
    {
        if (_state.CurrentState == PlayerState.Eliminated)
            return;

        _references.Controller.enabled = false;

        _state.Velocity = _impulseForce = _persistentForce = _platformVelocity = Vector3.zero;
        _state.Ground = null;
        _state.CurrentState = PlayerState.Stunned;

        if (duration > 0)
            Invoke(nameof(Recover), duration);
    }

    /// <summary>
    /// Eliminates the player from the game.
    /// </summary>
    public void Eliminate()
    {
        _references.Controller.enabled = false;
        
        _state.Velocity = _impulseForce = _persistentForce = _platformVelocity = Vector3.zero;
        _state.Ground = null;
        _state.CurrentState = PlayerState.Eliminated;

        Invoke(nameof(GoToCheckpoint), 2);
    }
    
    /// <summary>
    /// Recover from stun.
    /// </summary>
    public void GoToCheckpoint()
    {
        if (Checkpoint.Active && _health.CurrentHealth > 0)
        {
            _health.CurrentHealth--;

            transform.position = Checkpoint.Active.transform.position;

            Debug.Log("Go to last checkpoint");

            Recover();
        }
        else
        {
            Debug.Log("Player is dead");

            _health.OnDie.Invoke();
        }
    }
    
    /// <summary>
    /// Recover from stun.
    /// </summary>
    public void Recover()
    {
        if (_state.CurrentState == PlayerState.Stunned ||
            _state.CurrentState == PlayerState.Eliminated)
            _state.CurrentState = PlayerState.Idle;

        _references.Controller.enabled = true;
    }

    /// <summary>
    /// Set the player loser.
    /// </summary>
    public void Loose()
    {
        _references.Controller.enabled = false;

        _state.Velocity = _impulseForce = _persistentForce = _platformVelocity = Vector3.zero;
        _state.Ground = null;
        _state.CurrentState = PlayerState.Loser;
    }

    /// <summary>
    /// Set the player winner.
    /// </summary>
    public void Win()
    {
        _references.Controller.enabled = false;

        _state.Velocity = _impulseForce = _persistentForce = _platformVelocity = Vector3.zero;
        _state.Ground = null;
        _state.CurrentState = PlayerState.Winner;
    }
    #endregion

    #region Player Logic
    private void SetPlatformOffset()
    {
        // Raycast for center contact
        Vector3 rayOrigin = transform.position + _groundCheckRayOffset;
        bool rayHit = Physics.Raycast(rayOrigin, Vector3.down, out RaycastHit rayInfo,
                                      _settings.GroundTolerance * 2f, _settings.GroundLayer);

        // OverlapSphere for edge contact — works even when already intersecting
        Vector3 sphereOrigin = transform.position + _groundCheckSphereOffset;
        int overlapCount = Physics.OverlapSphereNonAlloc(sphereOrigin, _groundCheckRadius,
                                                         _overlapResults, _settings.GroundLayer);
        bool sphereHit = overlapCount > 0;

        bool wasGrounded = _state.IsGrounded;
        _state.IsGrounded = rayHit || sphereHit;

        if (_state.IsGrounded)
        {
            Transform currentGround = rayHit ? rayInfo.collider.transform : _overlapResults[0].transform;

            // Initialize references when landing on a new surface to prevent teleporting
            if (currentGround != _state.Ground)
            {
                _state.Ground = currentGround;
                _lastPlatformPosition = _state.Ground.position;
                _lastPlatformRotation = _state.Ground.rotation;

                _impulseForce.y = _platformVelocity.y = 0;

                return;
            }

            // Yaw delta: rotate player around platform pivot
            Quaternion rotationDelta = _state.Ground.rotation * Quaternion.Inverse(_lastPlatformRotation);
            float platformYaw = rotationDelta.eulerAngles.y;

            if (Mathf.Abs(platformYaw) > .001f)
            {
                Vector3 dir = transform.position - _state.Ground.position;
                dir = Quaternion.Euler(0, platformYaw, 0) * dir;
                transform.position = _state.Ground.position + dir;
                transform.Rotate(0, platformYaw, 0);
            }

            // Translation delta
            Vector3 platformDelta = _state.Ground.position - _lastPlatformPosition;
            transform.position += platformDelta;

            // Store current state for next frame
            _lastPlatformPosition = _state.Ground.position;
            _lastPlatformRotation = _state.Ground.rotation;

            // Sync physics broadphase — prevents CC from seeing stale overlap
            Physics.SyncTransforms();
        }
        else
        {
            // Just left the ground: inherit platform velocity so the player
            // doesn't stop dead in the air when jumping off a moving platform
            if (wasGrounded && _state.Ground != null)
                _platformVelocity = (_state.Ground.position - _lastPlatformPosition) / Time.deltaTime;

            _state.Ground = null;
        }
    }

    private void SetGravity(float deltaTime)
    {
        if (_state.IsGrounded && _state.Velocity.y < 0)
        {
            _state.Velocity.y = STICK_FORCE;
        }
        else if (_persistentForce.y <= 0)
        {
            if (_platformVelocity.y > 0)
            {
                _platformVelocity.y += GRAVITY * deltaTime;

                if (_platformVelocity.y < 0)
                    _state.Velocity.y += _platformVelocity.y;
            }
            else if (_impulseForce.y > 0)
            {
                _impulseForce.y += GRAVITY * deltaTime;

                if (_impulseForce.y < 0)
                    _state.Velocity.y += _impulseForce.y;
            }
            else
            {
                _state.Velocity.y += GRAVITY * deltaTime;
            }
            
            _state.Velocity.y = Mathf.Max(_state.Velocity.y, MAX_GRAVITY);
        }
    }

    private void SetVelocity(float deltaTime)
    {
        // Movement attenuation based on state
        float moveAtten;
        switch (_state.CurrentState)
        {
            case PlayerState.Idle:
            case PlayerState.Moving:
                moveAtten = 1f;
                break;
            case PlayerState.Jumping:
                moveAtten = .8f;
                break;
            case PlayerState.Falling:
                moveAtten = .6f;
                break;
            default:
                moveAtten = 0f;
                break;
        }

        Vector2 input = _moveAction.ReadValue<Vector2>();
        Vector3 forward = Vector3.ProjectOnPlane(_camera.transform.forward, Vector3.up).normalized;
        Vector3 right = Vector3.ProjectOnPlane(_camera.transform.right, Vector3.up).normalized;

        float speed = _settings.Speed * KMH_TO_MS * moveAtten;
        Vector3 moveInput = (forward * input.y + right * input.x) * speed;

        _state.Velocity.x = moveInput.x;
        _state.Velocity.z = moveInput.z;

        if (moveInput.sqrMagnitude > 0.001f)
        {
            Quaternion targetRot = Quaternion.LookRotation(moveInput);
            float step = _settings.RotationSpeed * deltaTime;
            Vector3 euler = Quaternion.Slerp(transform.rotation, targetRot, step).eulerAngles;
            transform.rotation = Quaternion.Euler(0, euler.y, 0);
        }
    }

    private void SetJump()
    {
        if (_jumpAction.triggered && _state.IsGrounded)
        {
            _state.Velocity.y = _settings.JumpForce;
            _state.Ground = null;
        }
    }

    private void SetMovement(float deltaTime)
    {
        Vector3 velocity = _state.Velocity + _impulseForce + _persistentForce + _platformVelocity;
        _references.Controller.Move(velocity * deltaTime);

        // Decay impulse force
        Vector3 impulseForce = Vector3.MoveTowards(_impulseForce, Vector3.zero, _settings.ExtraForcesDrag * deltaTime);
        impulseForce.y = _impulseForce.y;
        _impulseForce = impulseForce;

        // Decay inherited platform velocity: reset when back on ground
        if (_state.IsGrounded)
        {
            _platformVelocity = Vector3.zero;
        }
        else
        {
            Vector3 platformVelocity = Vector3.MoveTowards(_platformVelocity, Vector3.zero, _settings.ExtraForcesDrag * deltaTime);
            platformVelocity.y = _platformVelocity.y;
            _platformVelocity = platformVelocity;
        }
    }

    private void SetState()
    {
        if (_state.CurrentState == PlayerState.Stunned ||
            _state.CurrentState == PlayerState.Eliminated ||
            _state.CurrentState == PlayerState.Winner ||
            _state.CurrentState == PlayerState.Loser)
        {
            return;
        }

        PlayerState previousState = _state.CurrentState;

        if (!_state.IsGrounded)
        {
            _state.CurrentState = _state.VerticalVelocity > 0
                ? PlayerState.Jumping
                : PlayerState.Falling;
        }
        else
        {
            _state.CurrentState = _state.HorizontalVelocity.sqrMagnitude > 0.1f
                ? PlayerState.Moving
                : PlayerState.Idle;
        }

        if (previousState != _state.CurrentState)
            OnStateChanged?.Invoke(previousState, _state.CurrentState);
    }
    #endregion
}