using Unity.VisualScripting;
using UnityEngine;

public class CameraSystem : MonoBehaviour
{
    [SerializeField] private GameObject[] cams;
    [SerializeField] private GameObject mainCam;
    [SerializeField] private int currentCam;
    [SerializeField] private KeyCode openCam;
    [SerializeField] private bool camIsOpen;
    [SerializeField] private float cdTimer;
    [SerializeField] private float cdTime = 0.5f;


    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        for (int i = 0; i < cams.Length; i++) {
            cams[i].SetActive(false);
        }
        mainCam.SetActive(true);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(openCam)) {
            camIsOpen = !camIsOpen;
            ShowCam();
        }

        if (cdTimer <= 0) {
            if (Input.GetAxis("Horizontal") > 0) {
                cams[currentCam].SetActive(false);
                currentCam = currentCam + 1;
                if (currentCam >= cams.Length) {
                    currentCam = 0;
                }
                Go2Cam(currentCam);
                cdTimer = cdTime;
            }
            else if (Input.GetAxis("Horizontal") < 0) {
                cams[currentCam].SetActive(false);
                currentCam = currentCam - 1;
                if (currentCam < 0) {
                    currentCam = cams.Length - 1;
                }
                Go2Cam(currentCam);
                cdTimer = cdTime;
            }
        }
        else {
            cdTimer -= Time.deltaTime;
        }
    }

    private void ShowCam() {
        if (camIsOpen) {
            cams[currentCam].SetActive(true);
            mainCam.SetActive(false);
        }
        else {
            cams[currentCam].SetActive(false);
            mainCam.SetActive(true);
        }
    }

    private void Go2Cam(int progression) {
        cams[currentCam].SetActive(false);
        currentCam = progression;
        ShowCam();
    }
}
