using UnityEngine;

namespace RebootKit.Engine.Simulation.Characters {
    public class CharacterLocomotion : MonoBehaviour {
        [SerializeField] CharacterController m_CharacterController;

        public float maxMovementSpeed = 4.0f;
        public float maxSprintSpeed = 15.0f;
        public float jumpHeight = 1.0f;
        public float gravity = 10f;
        public float maxFallSpeed = 20f;
        public float damping = 20.0f;
        Vector3 m_CurrentVelocity;
        bool m_IsFalling;

        bool m_IsSprinting;
        bool m_JumpRequested;

        Vector3 m_PendingInputValue;

        public bool IsGrounded => m_CharacterController.isGrounded;

        public Vector3 Velocity => m_CurrentVelocity;

        void Update() {
            ConsumePendingInput();
            UpdateVerticalVelocity();

            m_CharacterController.Move(m_CurrentVelocity * Time.deltaTime);

            ApplyFriction();
            DetectFall();
        }

        void DetectFall() {
            if (m_IsFalling && m_CharacterController.isGrounded)
                m_IsFalling = false;
            else if (!m_CharacterController.isGrounded) m_IsFalling = true;
        }

        void ConsumePendingInput() {
            if (!IsGrounded) {
                m_PendingInputValue = Vector3.zero;
                return;
            }

            m_PendingInputValue.y = 0.0f;

            float pendingInputMagnitude = m_PendingInputValue.magnitude;
            Vector3 direction = Vector3.zero;

            if (pendingInputMagnitude > 0.0f)
                // normalize vector, reusing magnitude to avoid multiple sqrt calls
                direction = m_PendingInputValue / pendingInputMagnitude;

            float movementSpeed = m_IsSprinting ? maxSprintSpeed : maxMovementSpeed;
            Vector3 movementVelocity = m_CurrentVelocity;
            movementVelocity.y = 0.0f;
            movementVelocity += direction * (movementSpeed * pendingInputMagnitude);
            movementVelocity.y = 0.0f;

            // Clamp speed
            float movementVelocityMagnitude = movementVelocity.magnitude;
            Vector3 movementVelocityDirection = movementVelocityMagnitude > 0.0f ? movementVelocity / movementVelocityMagnitude : Vector3.zero;

            if (movementVelocityMagnitude > movementSpeed) movementVelocityMagnitude = movementSpeed;

            movementVelocity = movementVelocityDirection * movementVelocityMagnitude;

            m_CurrentVelocity.x = movementVelocity.x;
            m_CurrentVelocity.z = movementVelocity.z;

            m_PendingInputValue = Vector3.zero;
        }

        void UpdateVerticalVelocity() {
            if (m_CharacterController.isGrounded) {
                if (m_JumpRequested) {
                    m_CurrentVelocity.y = Mathf.Sqrt(2.0f * gravity * jumpHeight);
                    m_JumpRequested = false;
                } else {
                    m_CurrentVelocity.y = -1f;
                }
            } else {
                m_CurrentVelocity.y -= gravity * Time.deltaTime;
                m_CurrentVelocity.y = Mathf.Max(m_CurrentVelocity.y, -maxFallSpeed);
            }
        }

        void ApplyFriction() {
            if (!IsGrounded) return;

            Vector3 movementVelocity = m_CurrentVelocity;
            movementVelocity.y = 0.0f;

            movementVelocity = Vector3.MoveTowards(movementVelocity, Vector3.zero, damping * Time.deltaTime);

            m_CurrentVelocity.x = movementVelocity.x;
            m_CurrentVelocity.z = movementVelocity.z;
        }

        public void AddVelocity(Vector3 velocity) {
            m_CurrentVelocity += velocity;
        }

        public void AddMovementInput(Vector3 input, float scale) {
            m_PendingInputValue += input * scale;
        }

        public void Jump() {
            if (!m_CharacterController.isGrounded) return;

            m_JumpRequested = true;
        }

        public void StartSprint() {
            m_IsSprinting = true;
        }

        public void StopSprint() {
            m_IsSprinting = false;
        }
    }
}