using UnityEngine;
using UnityEngine.InputSystem;

namespace VOID.Generic.Player.Input
{
    [DefaultExecutionOrder(1)]
    public class CameraInputManager : MonoBehaviour
    {
        public static CameraInputManager current {get; private set;}
        
        private Inputs _inputs;

        // Input actions for camera controlling
        public InputAction movement {get; private set;}
        public InputAction movementFaster {get; private set;}
        public InputAction rotatingAndTiltingStart {get; private set;}
        public InputAction rotatingAndTilting {get; private set;}
        public InputAction zooming {get; private set;}

        // States
        public bool isMoving {get; private set;}
        public bool isMovingFaster {get; private set;}
        public bool isRotatingAndTilting {get; private set;}
        public bool isZooming {get; private set;}
        
        // Deltas
        public Vector2 movementDelta { get; private set; }
        public Vector2 rotateAndTiltDelta { get; private set; }
        public float zoomDelta { get; private set; }

        private void Awake()
        {
            current = this;
            _inputs = new Inputs();

            // CameraMovement
            movement = _inputs.CameraControls.Movement;
            movement.started += _ => isMoving = true;
            movement.performed += _ => OnCameraMovement();
            movement.canceled += _ => isMoving = false;
            
            // CameraMovementFaster
            movementFaster = _inputs.CameraControls.MovementFaster;
            movementFaster.started += _ => isMovingFaster = true;
            movementFaster.canceled += _ => isMovingFaster = false;

            // CameraRotatingAndTiltingStart
            rotatingAndTiltingStart = _inputs.CameraControls.RotatingAndTiltingStart;
            rotatingAndTiltingStart.started += _ => isRotatingAndTilting = true;
            rotatingAndTiltingStart.canceled += _ => isRotatingAndTilting = false;

            // CameraRotatingAndTilting
            rotatingAndTilting = _inputs.CameraControls.RotatingAndTilting;
            rotatingAndTilting.performed += _ => OnCameraRotateAndTilt();

            // CameraZooming
            zooming = _inputs.CameraControls.Zooming;
            zooming.performed += _ => OnCameraZoom();
            zooming.performed += _ => isZooming = true;
        }

        private void OnEnable()
        {
            movement.Enable();
            movementFaster.Enable();
            rotatingAndTiltingStart.Enable();
            rotatingAndTilting.Enable();
            zooming.Enable();
        }

        private void OnDisable()
        {
            movement.Disable();
            movementFaster.Disable();
            rotatingAndTiltingStart.Disable();
            rotatingAndTilting.Disable();
            zooming.Disable();
        }

        private void FixedUpdate()
        {
            
            // Reset delta after all ->FixedUpdate() 
            isZooming = false;
            if (!isMoving) movementDelta = Vector2.zero;
            rotateAndTiltDelta = Vector2.zero;
            zoomDelta = 0f;
        }

        private void OnCameraMovement() => movementDelta = movement.ReadValue<Vector2>();

        private void OnCameraRotateAndTilt() => rotateAndTiltDelta += rotatingAndTilting.ReadValue<Vector2>();

        private void OnCameraZoom() => zoomDelta += zooming.ReadValue<Vector2>().y;
    }
}
