using Cinemachine;
using UnityEngine;

public class CameraManager : MonoBehaviour {
    private CinemachineVirtualCamera activeCam;

    private static CameraManager instance;

    public static CameraManager GetInstance() {
        return instance;
    }

    private void Start() {
        instance = this;
    }

    private void Update() {
        if (Input.GetAxis("Mouse ScrollWheel") != 0f) {
            CinemachineComponentBase componentBase =
                activeCam.GetCinemachineComponent(CinemachineCore.Stage.Body);
            if (componentBase is CinemachineFramingTransposer) {
                float m_CameraDistance =
                    (componentBase as CinemachineFramingTransposer)
                        .m_CameraDistance;
                if (
                    m_CameraDistance <= 1 &&
                    Input.GetAxis("Mouse ScrollWheel") > 0
                ) {
                    m_CameraDistance = 1f;
                } else {
                    m_CameraDistance -= Input.GetAxis("Mouse ScrollWheel") * 10;
                }
                (componentBase as CinemachineFramingTransposer)
                    .m_CameraDistance = m_CameraDistance;
            }
        }
    }

    private void Awake() {
        activeCam = GetComponentInChildren<CinemachineVirtualCamera>();
    }

    public void FocusOnGameObject(GameObject gameObject) {
        activeCam.Follow = gameObject.transform;
    }
}
