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

/// <summary>
/// Attach to a manager GameObject in "are u sure.unity".
/// "yes" = confirm leave lobby and go to main menu.
/// "no" = go back to previous lobby scene.
/// </summary>
public class ConfirmLeaveUI : MonoBehaviour
{
    [Header("Optional refs (auto-found by name if null)")]
    public Button yesButton;
    public Button noButton;

    [Tooltip("Scene to load after leaving lobby")]
    public string mainMenuScene = "main menu asi idk lol";

    [Tooltip("Scene to go back to when player presses No")]
    public string previousScene = "create";

    void Start()
    {
        if (yesButton == null)
        {
            var go = GameObject.Find("yes");
            if (go != null) yesButton = go.GetComponent<Button>();
        }
        if (noButton == null)
        {
            var go = GameObject.Find("no");
            if (go != null) noButton = go.GetComponent<Button>();
        }

        if (yesButton != null) yesButton.onClick.AddListener(OnYesClicked);
        if (noButton  != null) noButton.onClick.AddListener(OnNoClicked);
    }

    private void OnYesClicked()
    {
        GameManager.Instance?.LeaveLobbyButton();
        SceneManager.LoadScene(mainMenuScene, LoadSceneMode.Single);
    }

    private void OnNoClicked()
    {
        SceneManager.LoadScene(previousScene, LoadSceneMode.Single);
    }
}
