using Godot;

namespace Cirno.Scripts.UI;

public partial class PauseMenu : Control
{
    [Export]
    public StringName PauseActionName = "pause";
    
    [ExportGroup("Scenes")]
    [Export]
    public string MainMenuScene { get; private set; }
    
    [ExportGroup("Buttons")]
    [Export]
    public Button ResumeButton { get; private set; }

    [Export]
    public Button QuitButton { get; private set; }

    [Export]
    public Button OptionsButton { get; private set; }

    [Export]
    public Button DebugButton { get; private set; }

    private GameManager _gameManager;

    [Signal]
    public delegate void SpawnDebugMenuEventHandler();

    public override void _Ready()
    {
        //_gameManager = GetNode<GameManager>("/root/GameScene");
        _gameManager = GameManager.Instance;
        
        ResumeButton.Pressed += ResumeButtonOnPressed;
        QuitButton.Pressed += QuitButtonOnPressed;
        DebugButton.Pressed += DebugButtonOnPressed;
        
        ResumeButton.GrabFocus();
    }

    public override void _Process(double delta)
    {
        if (_gameManager.GameState == GameState.Paused && Input.IsActionJustPressed(PauseActionName))
        {
            _gameManager.Unpause();
        }
    }

    private void DebugButtonOnPressed()
    {
        EmitSignal(SignalName.SpawnDebugMenu);
    }

    private void ResumeButtonOnPressed()
    {
        _gameManager.Unpause();
    }
    
    private void QuitButtonOnPressed()
    {
        _gameManager.Unpause();
        GlobalState.Instance.ChangeCursor(true);
        GlobalState.Instance.GotoScene(MainMenuScene);
        //GetTree().ChangeSceneToFile(MainMenuScene);
    }
}