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

public class ComputerCoordinator : MonoBehaviour
{
    [SerializeField] private List<Computer> Computers;
    [SerializeField] private GameObject phase2GameObject;
    
    private int _brokenComputerIdx = 0;

    private void Start()
    {
        BreakNextComputer(999);
    }

    public void BreakNextComputer(int previousComputerHealth)
    {
        FindFirstObjectByType<CinemachineShakeService>().Shake(1.2f, 5f, 0.2f);
        
        if (previousComputerHealth <= 0)
        {
            _brokenComputerIdx -= 1;
            if (_brokenComputerIdx < 0) _brokenComputerIdx = 0;
            Computers.RemoveAt(_brokenComputerIdx);
        }

        if (Computers.Count < 1)
        {
            //Call the boss
            AdvanceToTheNextPhase();
            return; //no computers left, we can advance to the phase two
        }
        
        //break the very next computer
        Computers[_brokenComputerIdx].BreakComputer();
        
        //move to the next computer index
        _brokenComputerIdx++;
        _brokenComputerIdx %= Computers.Count;
    }

    private void AdvanceToTheNextPhase()
    {
        phase2GameObject.SetActive(true);
        gameObject.GetComponentInParent<Transform>().gameObject.SetActive(false);
    }
}
