using TMPro;
using UnityEditor.UI;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine.UIElements;
using Button = UnityEngine.UI.Button;

public class Menu : MonoBehaviour
{
    public TMP_InputField seedInput;
    public TMP_Text saveInfos;
    public Button loadGameButton;

    public Canvas mainCanvas;
    public Canvas newGame;
    public Canvas loadGame;

    private SaveManager sm;
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        sm = GetComponent<SaveManager>();
        newGame.enabled = false;
        loadGame.enabled = false;
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void NewGame()
    {
        GameData gameData = new();
        if(!int.TryParse(seedInput.text, out int number))
        {
            System.Random rnd = new();
            gameData.seed = rnd.Next();
        }
        else
        {
            gameData.seed = number;
        }
        sm.SaveGame(gameData);

        SceneManager.LoadScene("Game");
    }

    public void LoadGame()
    {
        GameData gameData = sm.LoadGame();
        if (gameData != null)
        {
            SceneManager.LoadScene("Game");
        }
        else
        {
            Debug.Log("No saves!");
        }
    }
    public void MainMenu()
    {
        mainCanvas.enabled = true;
        newGame.enabled = false;
        loadGame.enabled = false;
    }

    public void NewGameSubMenu()
    {
        mainCanvas.enabled = false;
        newGame.enabled = true;
        loadGame.enabled = false;
    }

    public void LoadGameSubMenu()
    {
        mainCanvas.enabled = false;
        newGame.enabled = false;
        loadGame.enabled = true;

        GameData gameData = sm.LoadGame();
        if (gameData != null)
        {
            saveInfos.text = "Current time : " + gameData.hours.ToString("D2") + "h" + gameData.minutes.ToString("D2") + 
                "\nScore : " + gameData.playerScore.ToString("D2") + 
                "\nSeed : " + gameData.seed;
        }
        else
        {
            saveInfos.text = "No saves found!";
            loadGameButton.interactable = false;
        }
    }
}
