using UnityEngine;
using Mirror;
using static Mirror.NetworkIdentity;
using static Mirror.NetworkBehaviour;

public class PlayerCameraController : NetworkBehaviour
{
    [Header("Camera Settings")]
    public float sensitivity = 1f;
    public float verticalClampAngle = 90f; // Max felfelé/lefelé nézés szöge
    public float fov = 60f;
    public float zoomSpeed = 2f;
    public float minFov = 15f;
    public float maxFov = 90f;

    [Header("Effects")]
    public float crouchLerpSpeed = 5f;
    public float jumpBounceIntensity = 0.1f;
    public float jumpBounceDuration = 0.5f;
    public float crouchCameraOffset = 0.2f;

    private Transform player;
    private Camera cam;
    private Vector3 baseLocalPosition;
    private float xRotation = 0f;
    private float yRotation = 0f;
    private float bounceTimer = 0f;
    private float currentBounceOffset = 0f;
    private bool isCrouching = false;

    private void Awake()
    {
        player = transform.parent;
        cam = GetComponent<Camera>();
        baseLocalPosition = transform.localPosition;
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    private void Update()
    {
        if (player == null || cam == null) return;

        HandleRotation();
        HandleZoom();
        HandlePosition();
        
        fov = PlayerPrefs.GetInt("fov", 0);
        
        sensitivity = PlayerPrefs.GetInt("multiplier", 0);
    }

    private void HandleRotation()
    {
        float mouseX = Input.GetAxis("Mouse X") * sensitivity * 100 * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * sensitivity * 100 * Time.deltaTime;

        // Víszintes forgás (játékos test)
        yRotation += mouseX;
        player.localRotation = Quaternion.Euler(0f, yRotation, 0f);

        // Függőleges forgás (csak kamera)
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -verticalClampAngle, verticalClampAngle);
        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    }

    private void HandleZoom()
    {
        fov -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
        fov = Mathf.Clamp(fov, minFov, maxFov);
        cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, fov, 10f * Time.deltaTime);
    }

    private void HandlePosition()
    {
        // Alap pozíció (gugolás figyelembevételével)
        Vector3 targetPosition = baseLocalPosition;
        if (isCrouching)
        {
            targetPosition.y -= crouchCameraOffset;
        }

        // Ugrás effekt
        if (bounceTimer > 0)
        {
            float bounceProgress = 1f - (bounceTimer / jumpBounceDuration);
            currentBounceOffset = Mathf.Sin(bounceProgress * Mathf.PI) * jumpBounceIntensity;
            bounceTimer -= Time.deltaTime;
        }
        else
        {
            currentBounceOffset = Mathf.Lerp(currentBounceOffset, 0f, 10f * Time.deltaTime);
        }

        // Végső pozíció alkalmazása
        transform.localPosition = targetPosition + Vector3.up * currentBounceOffset;
    }

    public void SetCrouch(bool crouch) => isCrouching = crouch;
    public void JumpEffect() => bounceTimer = jumpBounceDuration;
}