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

public class MainMenu : MonoBehaviour
{
    public GameObject mainScreen;
    public GameObject levelsScreen;

    public int levels;

    public GameObject levelsContainer;

    public GameObject levelObject;

    private void Start()
    {
        for (int i = 0; i < levels; i++)
        {
            GameObject newLevel = Instantiate(levelObject, levelsContainer.transform);
            newLevel.transform.GetChild(0).GetComponent<TextMeshProUGUI>().text = i.ToString();
            int levelIndex = i;
            newLevel.GetComponent<Button>().onClick.AddListener(() => LoadLevel(levelIndex));
        }
    }

    private void LoadLevel(int index)
    {
        string sceneName = "Level" + index;
        SceneManager.LoadScene(sceneName);
    }

    public void GoToLevelsScreen()
    {
        mainScreen.SetActive(false);
        levelsScreen.SetActive(true);
    }

    public void backToMainScreen()
    {
        mainScreen.SetActive(true);
        levelsScreen.SetActive(false);
    }

    public void ExitGame()
    {
        Application.Quit();
    }
}
