using Godot;
using Mr.BrickAdventures.Autoloads;

namespace Mr.BrickAdventures.scripts.UI;

public partial class MainMenu : Control
{
    [Export] public Control MainMenuControl { get; set; }
    [Export] public Button NewGameButton { get; set; }
    [Export] public Button ContinueButton { get; set; }
    [Export] public Button SettingsButton { get; set; }
    [Export] public Button CreditsButton { get; set; }
    [Export] public Button ExitButton { get; set; }
    [Export] public Label VersionLabel { get; set; }
    [Export] public Control SettingsControl { get; set; }
    [Export] public Control CreditsControl { get; set; }

    private SaveSystem SaveSystem => SaveSystem.Instance;
    private GameManager GameManager => GameManager.Instance;
    private UIManager UIManager => UIManager.Instance;

    public override void _Ready()
    {
        NewGameButton.FocusMode = Control.FocusModeEnum.All;
        ContinueButton.FocusMode = Control.FocusModeEnum.All;
        SettingsButton.FocusMode = Control.FocusModeEnum.All;
        CreditsButton.FocusMode = Control.FocusModeEnum.All;
        ExitButton.FocusMode = Control.FocusModeEnum.All;

        NewGameButton.Pressed += OnNewGamePressed;
        ContinueButton.Pressed += OnContinuePressed;
        SettingsButton.Pressed += OnSettingsPressed;
        CreditsButton.Pressed += OnCreditsPressed;
        ExitButton.Pressed += OnExitPressed;

        VersionLabel.Text = $"v. {ProjectSettings.GetSetting("application/config/version")}";
        ContinueButton.Disabled = !SaveSystem.CheckSaveExists();

        if (InputDeviceManager.Instance != null)
            InputDeviceManager.Instance.DeviceChanged += OnDeviceChanged;
        VisibilityChanged += OnVisibilityChanged;

        if (InputDeviceManager.Instance?.IsPointerless == true)
            GrabFirstFocus();
    }

    public override void _ExitTree()
    {
        if (InputDeviceManager.Instance != null)
            InputDeviceManager.Instance.DeviceChanged -= OnDeviceChanged;
        VisibilityChanged -= OnVisibilityChanged;
    }

    private void OnVisibilityChanged()
    {
        if (IsVisibleInTree() && InputDeviceManager.Instance?.IsPointerless == true)
            GrabFirstFocus();
    }

    private void OnDeviceChanged(int device)
    {
        var d = (InputDeviceManager.InputDevice)device;
        if (d != InputDeviceManager.InputDevice.Mouse && IsVisibleInTree())
            GrabFirstFocus();
    }

    private void GrabFirstFocus()
    {
        if (SaveSystem.CheckSaveExists())
            ContinueButton.GrabFocus();
        else
            NewGameButton.GrabFocus();
    }

    private void OnExitPressed()
    {
        GameManager.QuitGame();
    }

    private void OnCreditsPressed()
    {
        UIManager.PushScreen(CreditsControl);
    }

    private void OnSettingsPressed()
    {
        UIManager.PushScreen(SettingsControl);
    }

    private void OnContinuePressed()
    {
        GameManager.ContinueGame();
    }

    private void OnNewGamePressed()
    {
        GameManager.StartNewGame();
    }
}