using UnityEngine;

namespace NBF
{
    public partial class FPlayer
    {
        #region Move

        private Quaternion lastRotation;

        private void UpdateMove()
        {
            UpdateGrounded();
            UpdateWater();
            ProcessMoveStates();
            UpdateLookInput();
        }

        private void UpdateGrounded()
        {
            Data.IsGrounded = FirstPerson.IsGrounded();
            Data.Speed = FirstPerson.velocity.magnitude;

            Quaternion rotation = FirstPerson.transform.rotation;

            // 计算当前帧与上一帧的旋转差异
            Quaternion rotationDelta = rotation * Quaternion.Inverse(lastRotation);

            // 将四元数转换为角度轴表示
            rotationDelta.ToAngleAxis(out float angle, out Vector3 axis);

            // 确保角度在0-360范围内
            if (angle > 180f) angle -= 360f;

            // 获取Y轴旋转分量（归一化处理）
            float yRotation = 0f;
            if (Mathf.Abs(angle) > 0.001f && Mathf.Abs(axis.y) > 0.1f)
            {
                // 计算Y轴方向的旋转角度（考虑旋转轴方向）
                yRotation = angle * Mathf.Sign(axis.y);
            }

            float maxTurnSpeed = 180f; // 度/秒
            // 转换为角速度并归一化到[-1, 1]
            float angularSpeed = yRotation / Time.deltaTime;
            float turnValue = Mathf.Clamp(angularSpeed / maxTurnSpeed, -1f, 1f);


            Data.RotationSpeed = turnValue;

            lastRotation = rotation;
        }

        private void UpdateWater()
        {
            // SceneSettings.Instance.Water.w
        }

        private void ProcessMoveStates()
        {
            // if (CameraView.Value == CameraViewType.TPP)
            // {
            //     float num = (IsRunPressed.Value ? MovementSpeed.Value : (MovementSpeed.Value * 0.5f));
            //     num = (IsFlyModeEnabled ? (num * (float)FlySpeed) : num);
            //     Vector3 zero = Vector3.zero;
            //     zero += Vector3.right * MovementDirection.Value.x;
            //     zero += Vector3.forward * MovementDirection.Value.y;
            //     zero = zero.relativeTo(_CameraTPPTarget, _Character.GetUpVector());
            //     _Character.RotateTowards(zero, Time.deltaTime * _RotateTPPSpeed);
            //     float value = Vector3.Dot(_Character.GetForwardVector(), zero);
            //     Vector3 vector = _Character.GetForwardVector() * Mathf.Clamp01(value) * num;
            //     if (checkWaterBound)
            //     {
            //         SetMovementDirectionWithRaycastCheck(vector);
            //     }
            //     else
            //     {
            //         _Character.SetMovementDirection(vector);
            //     }
            // }
            // else
            {
                var num2 = Data.Run ? 7 : 5;
                //(IsRunPressed.Value ? MovementSpeed.Value : (MovementSpeed.Value * 0.5f));
                // num2 = (IsFlyModeEnabled ? (num2 * (float)FlySpeed) : num2);
                Vector3 vector2 = FirstPerson.GetRightVector() * Data.MoveInput.x * num2;
                vector2 += FirstPerson.GetForwardVector() * Data.MoveInput.y * num2;
                // if (checkWaterBound) 
                // {
                //     SetMovementDirectionWithRaycastCheck(vector2);
                // }
                // else
                {
                    FirstPerson.SetMovementDirection(vector2);
                }
            }
        }

        #endregion

        #region Look

        public float MouseSensitivity = 0.1f;
        [Space(15f)] public bool invertLook = true;

        public float minPitch = -60f;

        public float maxPitch = 60f;

        private void UpdateLookInput()
        {
            // TPPLookTarget.position = base.transform.position;
            // if (CameraView.Value == CameraViewType.TPP)
            // {
            //     lookXRot -= MouseInput.Value.y;
            //     lookXRot = Mathf.Clamp(lookXRot, -25f, 55f);
            //     lookYRot += MouseInput.Value.x;
            //     lookYRot = Mathf.Repeat(lookYRot, 360f);
            //     TPPLookTarget.localEulerAngles = new Vector3(lookXRot, lookYRot, 0f);
            // }
            // else if (CameraView.Value == CameraViewType.FPP)
            {
                // if (_IsInVehicle && PlayerState.Value == State.vehicle)
                // {
                //     lookXRot -= MouseInput.Value.y;
                //     lookXRot = Mathf.Clamp(lookXRot, VehicleLookXMinMax.x, VehicleLookXMinMax.y);
                //     lookYRot += MouseInput.Value.x;
                //     lookYRot = Mathf.Clamp(lookYRot, VehicleLookYMinMax.x, VehicleLookYMinMax.y);
                //     VehicleLookTargetParent.localEulerAngles = new Vector3(lookXRot, lookYRot, 0f);
                //     _character.CameraPitch = 0f;
                // }
                // else
                {
                    Vector2 value = InputManager.GetLookInput();
                    FirstPerson.AddControlYawInput(value.x * (float)MouseSensitivity);
                    FirstPerson.AddControlPitchInput((invertLook ? (0f - value.y) : value.y) * (float)MouseSensitivity,
                        minPitch, maxPitch);
                    // lookXRot = base.transform.eulerAngles.x;
                    // lookYRot = base.transform.eulerAngles.y;
                }
            }
        }

        #endregion
    }
}