using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;

public class CameraManager : Singleton<CameraManager>
{
    public Camera rawCam;
    public CinemachineVirtualCamera vCam;

    // public Transform focus { get => vCam.m_Follow; set => vCam.m_Follow = value; }

    // private bool isStillAlive = true;

    // void Awake() {
    //     PlayerManager.I.playerDeath += OnPlayerDeath;
    // }

    // void OnDestroy() {
    //     PlayerManager.I.playerDeath -= OnPlayerDeath;
    // }

    private void Update()
    {
        if(/*isStillAlive && */PlayerManager.I.unit != null) {
            var target = PlayerManager.I.unit.transform;
            vCam.Follow = target;
            vCam.LookAt = target;
        }
    }

    // private void OnPlayerDeath() {
    //     this.isStillAlive = false;
    // }

    private void Noise(float amplitudeGain, float frequencyGain)
    {
        var noise = vCam.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
        if (!noise)
            noise = vCam.AddCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
        noise.m_AmplitudeGain = amplitudeGain;
        noise.m_FrequencyGain = frequencyGain;
    }
    private IEnumerator Shake0(float duration, float intensity)
    {
        Noise(1, intensity);
        yield return new WaitForSeconds(duration);
        Noise(0, 0);
    }
    public void Shake(float duration, float intensity)
    {
        StartCoroutine(Shake0(duration, intensity));
    }
}
