using UnityEngine;
using Unity.Cinemachine;

public class CinemachineShakeService : MonoBehaviour
{
    [SerializeField] private CinemachineBrain brain; // drag Main Camera brain here (or GetComponent)

    float timer;
    CinemachineBasicMultiChannelPerlin activeNoise;

    void Awake()
    {
        if (brain == null) brain = GetComponent<CinemachineBrain>();
    }

    public void Shake(float intensity, float frequency, float duration)
    {
        var activeCam = brain.ActiveVirtualCamera as CinemachineCamera;
        if (activeCam == null)
        {
            Debug.LogWarning("no active virtual camera found");
            return;
        }

        

        // CM3: the Perlin is typically an extension/component on the vcam
        activeNoise = activeCam.GetComponent<CinemachineBasicMultiChannelPerlin>();
        if (activeNoise == null)
        {
            Debug.LogWarning($"No CinemachineBasicMultiChannelPerlin on active camera: {activeCam.name}");
            return;
        }

        Debug.Log("are we even setting these values?");
        activeNoise.AmplitudeGain = intensity;
        activeNoise.FrequencyGain = frequency;
        timer = duration;
    }

    void Update()
    {
        if (timer <= 0f) return;
        timer -= Time.deltaTime;
        if (timer <= 0f && activeNoise != null)
        {
            activeNoise.AmplitudeGain = 0f;
            activeNoise.FrequencyGain = 0f;
            activeNoise = null;
        }
    }
}