using CustomInspector;
using System;
using UnityEngine;
using Mirror;
using UnityEngine.InputSystem;

namespace PlayerBehaviour
{
    public class PlayerCameraController : CameraController
    {
        /// <summary>
        /// (Vertical, Horizontal)
        /// </summary>
        public readonly static Vector2 defaultLookDirection = new(25, 0);
        [SerializeField, ReadOnly, Tooltip("(Vertical, Horizontal)")]
        Vector2 lookDir = Vector2.zero;

        float MaxLookHeight => carData.PrefabSettings.maxLookHeight;
        float MinLookHeight => carData.PrefabSettings.minLookHeight;
        private float Internal_sensitivity => carData.PrefabSettings.internal_sensitivity;

        bool rotationBlocked = false;

        float currentIdle = 0;
        const float maxIdleTime = 6; //max idle time until looks forward
        const float idleMoveSpeed = 30; //when idle how fast turned to front

        private PlayerInputActions playerControls;
        private Vector2 lastLookInput = Vector2.zero;

        protected override void Awake()
        {
            base.Awake();

            lookDir = defaultLookDirection;

            playerControls = new PlayerInputActions();

            // Subscribe to the actions in Awake, if the GameObject is not going to be disabled/enabled often
            playerControls.Player.Aiming.performed += OnAimPerformed;
            playerControls.Player.Aiming.canceled += OnAimCanceled;
            playerControls.Player.Look.canceled += ctx => lastLookInput = ctx.ReadValue<Vector2>();
            playerControls.Player.Look.canceled += ctx => lastLookInput = Vector2.zero;

        }

        public bool isAiming = false;
        private void OnAimPerformed(InputAction.CallbackContext context)
        {
            isAiming = true;

            if (!SettingsHandler.IsAimingMethodHold())
            {
                Aiming ^= true;
            }

        }
        private void OnAimCanceled(InputAction.CallbackContext context)
        {
            isAiming = false;
        }
        private void OnLookPerformed(InputAction.CallbackContext context)
        {
            Vector2 lookInput = context.ReadValue<Vector2>();
            HandleLook(lookInput);
        }

        private void HandleLook(Vector2 lookInput)
        {
            // UpdateMouseInput
            float sensitivity = Internal_sensitivity * SettingsHandler.GetSensitivity();
            if (lookInput != Vector2.zero)
            {
                lookDir += (sensitivity / currentZoom) * lookInput;
                currentIdle = 0;
            }
            else
            {
                //slowly rotate camera/view to car direction if no mouse input
                if (carData.CameraController.Aiming)
                {
                    currentIdle = 0;
                }
                else
                {
                    var car = carData.carInstance.CarController;
                    if (car.CurrentAcceleration != Prometeo.CarController.Acceleration.Neutral) //driving
                    {
                        //begin idle when first time steered
                        if (currentIdle > 0)
                        {
                            currentIdle += Time.deltaTime;
                            if (currentIdle > maxIdleTime)
                            {
                                lookDir = Vector2.MoveTowards(lookDir, defaultLookDirection, idleMoveSpeed * Time.deltaTime);
                            }
                        }
                        if (car.CurrentSteering != Prometeo.CarController.Steering.Straight) //if steered
                        {
                            currentIdle += Time.deltaTime;
                            //make it smallest angle
                            if (lookDir.y > 180)
                                lookDir.y -= 360;
                            else if (lookDir.y < -180)
                                lookDir.y += 360;
                        }
                    }
                }
                //… existing code …
            }
            lookDir.x = Mathf.Clamp(lookDir.x, MinLookHeight, MaxLookHeight);
            lookDir.y %= 360;
        }
        protected override void Start()
        {
            base.Start();

            if (!isLocalPlayer) return;

            rotationBlocked = true;
            Helpers.WaitSeconds(this, 0.5f, () => rotationBlocked = false);
        }

        protected override void Update()
        {
            base.Update();

            if (!isLocalPlayer)
                return;

            if (InGameMouse.IsLocked && !rotationBlocked)
            {
                HandleLook(lastLookInput); // Pass the last recorded look input to HandleLook

                // Idle logic
                if (lastLookInput == Vector2.zero)
                {
                    // Handle the camera’s idle return-to-center behavior
                    HandleIdleCameraBehavior();
                }
            }

            //Aiming logic should be handled separately as per your existing logic
            if (SettingsHandler.IsAimingMethodHold())
            {
                Aiming = isAiming;
            }
        }

        private void HandleIdleCameraBehavior()
        {
            // Handle the camera’s idle return-to-center behavior
            if (!carData.CameraController.Aiming)
            {
                var car = carData.carInstance.CarController;
                if (car.CurrentAcceleration != Prometeo.CarController.Acceleration.Neutral)
                {
                    // Start idle timer when first time steered
                    currentIdle += Time.deltaTime;
                    if (currentIdle > maxIdleTime)
                    {
                        lookDir = Vector2.MoveTowards(lookDir, defaultLookDirection, idleMoveSpeed * Time.deltaTime);
                    }
                }
                if (car.CurrentSteering != Prometeo.CarController.Steering.Straight)
                {
                    currentIdle += Time.deltaTime;
                    // Normalize the horizontal angle to the (-180, 180) range
                    lookDir.y = lookDir.y < -180 ? lookDir.y + 360 : lookDir.y > 180 ? lookDir.y - 360 : lookDir.y;
                }
            }
            else
            {
                currentIdle = 0;
            }
        }
        private void OnEnable()
        {
            playerControls.Enable(); // Make sure to enable the input when the object is active
        }
        private void OnDisable()
        {
            // If we subscribe in Awake, we still need to disable the input when the object is not active
            playerControls.Disable();
        }
        private void OnDestroy()
        {
            if (!this.gameObject.scene.isLoaded) //whole scene was closed
                return;
        }
    }
}



The looking in this code is not working. the camera isnt moving at all. why?