using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class UISetup : UIBaseScreen
{
    [SerializeField]
    Button[] playerAmountBtn;
    [SerializeField]
    Button startBtn;
    [SerializeField]
    Button backBtn;

    protected override void OnAwake()
    {
        backBtn?.onClick.AddListener(OnBack);

        for (int i = 0; i < playerAmountBtn.Length; i++)
        {
            int copy = i;
            playerAmountBtn[copy]?.onClick.AddListener(delegate { OnPlayerAmount(copy); });
        }

        startBtn?.onClick.AddListener(OnStart);
    }

    private void OnEnable()
    {
        ToggleStartButton(false);

        TogglePlayersPlayingColor(-1);
    }

    void OnPlayerAmount(int amount)
    {
        GameData.ActivePlayers = amount + 1;
        ToggleStartButton(true);

        TogglePlayersPlayingColor(amount);
    }

    void OnBack()
    {
        UIManager.Instance.Show<UIMainMenu>();
    }

    void OnStart()
    {
        SceneManager.LoadScene("GameScene");
    }

    void ToggleStartButton(bool isEnabled)
    {
        if (startBtn != null)
        {
            startBtn.GetComponent<Image>().color = isEnabled ? new Color(1, 1, 1, 1) : new Color(0, 0, 0, 1);
            startBtn.enabled = isEnabled;
        }
    }

    void TogglePlayersPlayingColor(int index)
    {
        for (int i = 0; i < playerAmountBtn.Length; i++)
        {
            playerAmountBtn[i].image.color = i == index ? new Color(1, 1, 1, 1) : new Color(1, 1, 1, 0.35f);
        }
    }
}
