using Godot;
using System;
using Cirno.Scripts.Enums;
using Cirno.Scripts.UI;
using Godot.Collections;

public partial class MainMenu : CanvasLayer
{

	[Export]
	public string GameScene { get; set; }

	[Export]
	public string MainMenuScene { get; set; }

	[Export]
	public Control SubMenuHolder { get; set; } = new Control();

	[Export]
	public Button DefaultSelectedButton { get; private set; }
	
	[Export]
	public AudioStreamPlayer2D MusicPlayer { get; private set; }

	[ExportCategory("Languages")]
	[Export] public Button ItalianButton { get; private set; }
	[Export] public Button EnglishButton { get; private set; }
	
	[ExportCategory("Music Room")]
	[Export]
	public PackedScene MusicRoomTemplate { get; set; }

	[ExportCategory("Debug Menu")]
	[Export]
	public PackedScene DebugMenuTemplate { get; set; }
	
	[ExportCategory("Credits Menu")]
	[Export]
	public PackedScene CreditsMenuTemplate { get; set; }
	
	[ExportCategory("Options Menu")]
	[Export]
	public PackedScene OptionsMenuTemplate { get; set; }

	[ExportCategory("Difficulty Menu")]
	[Export]
	public PackedScene DifficultyMenuTemplate { get; set; }
	
	
	
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		DefaultSelectedButton.GrabFocus();
		GlobalState.Instance.ChangeCursor(true);
		
		var locale = TranslationServer.GetLocale();
		if (locale is "en")
		{
			EnglishButton.ButtonPressed = true;
			ItalianButton.ButtonPressed = false;
		}
		else if (locale is "it")
		{
			EnglishButton.ButtonPressed = false;
			ItalianButton.ButtonPressed = true;
		}

		EnglishButton.Pressed += OnEnglishButtonPressed;
		ItalianButton.Pressed += OnItalianButtonPressed;
	}

	private void OnEnglishButtonPressed()
	{
		EnglishButton.ButtonPressed = true;
		ItalianButton.ButtonPressed = false;
		TranslationServer.SetLocale("en");
	}

	private void OnItalianButtonPressed()
	{
		EnglishButton.ButtonPressed = false;
		ItalianButton.ButtonPressed = true;
		TranslationServer.SetLocale("it");
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}

	private void _on_start_button_pressed()
	{
		SpawnMenu<DifficultyMenu>(DifficultyMenuTemplate, SubMenuHolder);
		
		//GetTree().ChangeSceneToFile(GameScene);
	}

	private void OnContinuePressed()
	{
		if (GameScene == null) return;
		if (GlobalState.Instance.LoadGame())
		{
			GlobalState.Session.GameMode = GameMode.Game;
			GlobalState.Session.AllowSaving = true;
		}
	}

	private void _on_debug_button_pressed()
	{
		SpawnMenu<DebugMenu>(DebugMenuTemplate, SubMenuHolder);

		// if (DebugMenuTemplate is null || DebugMenuHolder is null) return;
		// DebugMenuHolder.Visible = true;

		// var children = DebugMenuHolder.GetChildren();
		// foreach (var child in children)
		// {
		// 	child.QueueFree();
		// }

		// var menu = DebugMenuTemplate.Instantiate<DebugMenu>();

		// DebugMenuHolder.CallDeferred("add_child", menu);

		// menu.DebugMenuClosed += () =>
		// {
		// 	DebugMenuHolder.Visible = false;
		// };
	}

	private void _on_music_room_button_pressed()
	{
		SpawnMenu<MusicRoom>(MusicRoomTemplate, SubMenuHolder);
		MusicPlayer?.Stop();
	}

	private void SpawnMenu<T>(PackedScene template, Control holder) where T : MenuBase
	{
		if (template is null || holder is null) return;
		holder.Visible = true;

		var children = holder.GetChildren();
		foreach (var child in children)
		{
			child.QueueFree();
		}

		var menu = template.Instantiate<T>();

		holder.CallDeferred("add_child", menu);

		menu.MenuClosed += () =>
		{
			holder.Visible = false;
			if (!MusicPlayer.Playing)
			{
				MusicPlayer.Play();
			}
			
			DefaultSelectedButton.GrabFocus();
		};
	}

	private void OnCreditsButtonPressed()
	{
		SpawnMenu<CreditsMenu>(CreditsMenuTemplate, SubMenuHolder);
	}
	
	private void OnOptionsButtonPressed()
	{
		SpawnMenu<OptionsMenu>(OptionsMenuTemplate, SubMenuHolder);
	}

	private void OnRogueliteButtonPressed()
	{
		GlobalState.Session.NewSession();
		GlobalState.Session.AllowSaving = false;
		GlobalState.Session.Difficulty = DifficultyLevel.Normal;
		GlobalState.Session.GameMode = GameMode.Roguelite;
		GlobalState.Session.LevelNumber = 0;
		GlobalState.Instance.GotoScene("uid://bf1kqr3o6r6d4");
	}
	
	private void _on_mainmenu_button_pressed()
	{
		if (MainMenuScene != null)
		{
			GlobalState.Instance.GotoScene(MainMenuScene);
			//GetTree().ChangeSceneToFile(MainMenuScene);
		}
	}

	private void _on_exit_button_pressed()
	{
		GetTree().Quit();
	}
}
