using System;
using UnityEngine;

namespace NBF
{
    [Serializable]
    public class MouseLook
    {
        public float XSensitivity = 2f;

        public float YSensitivity = 2f;

        public bool clampVerticalRotation = true;

        public float MinimumX = -90f;

        public float MaximumX = 90f;

        public bool smooth;

        public float smoothTime = 5f;

        public bool lockCursor = true;

        public float RotYvalue;

        public bool invertMouseY;

        public bool invertMouseX;

        public int ControllerHandMode;

        private Quaternion m_CharacterTargetRot;

        private Quaternion m_CameraTargetRot;

        private bool m_cursorIsLocked = true;

        // private Player player;

        public void Init(Transform character, Transform camera)
        {
            m_CharacterTargetRot = character.localRotation;
            m_CameraTargetRot = camera.localRotation;
            // player = ReInput.players.GetPlayer(0);
        }

        public void LookRotation(Transform character, Transform camera)
        {
            // if (!SRDebug.Instance.IsDebugPanelVisible && !SRDebug.Instance.IsDebugPanelVisible)
            {
                // var lookInput = InputManager.GetLookInput();
                var lookInput = Vector2.zero;

                float num = Mathf.Clamp(lookInput.y * YSensitivity * 5f * Time.deltaTime, -10f, 10f);
                float num2 =
                    Mathf.Clamp(lookInput.x * XSensitivity * 5f * Time.deltaTime, -10f, 10f);

                if (invertMouseY)
                {
                    num *= -1f;
                }

                if (invertMouseX)
                {
                    num2 *= -1f;
                }

                RotYvalue = num2;
                m_CharacterTargetRot *= Quaternion.Euler(0f, num2, 0f);
                m_CameraTargetRot *= Quaternion.Euler(0f - num, 0f, 0f);
                if (clampVerticalRotation)
                {
                    m_CameraTargetRot = ClampRotationAroundXAxis(m_CameraTargetRot);
                }

                if (smooth)
                {
                    character.localRotation = Quaternion.Slerp(character.localRotation, m_CharacterTargetRot,
                        smoothTime * Time.deltaTime);
                    camera.localRotation = Quaternion.Slerp(camera.localRotation, m_CameraTargetRot,
                        smoothTime * Time.deltaTime);
                }
                else
                {
                    character.localRotation = m_CharacterTargetRot;
                    camera.localRotation = m_CameraTargetRot;
                }

                UpdateCursorLock();
            }
        }

        public void SetCursorLock(bool value)
        {
            lockCursor = value;
            if (!lockCursor)
            {
                Cursor.lockState = CursorLockMode.None;
                Cursor.visible = true;
            }
            else
            {
                Cursor.lockState = CursorLockMode.Locked;
                Cursor.visible = false;
            }
        }

        public void UpdateCursorLock()
        {
            if (lockCursor)
            {
                InternalLockUpdate();
            }
        }

        private void InternalLockUpdate()
        {
        }

        private Quaternion ClampRotationAroundXAxis(Quaternion q)
        {
            q.x /= q.w;
            q.y /= q.w;
            q.z /= q.w;
            q.w = 1f;
            float value = 114.59156f * Mathf.Atan(q.x);
            value = Mathf.Clamp(value, MinimumX, MaximumX);
            q.x = Mathf.Tan((float)Math.PI / 360f * value);
            return q;
        }
    }
}