using Godot;

// Главное меню. Кнопки ведут на отдельные сцены.
public partial class MainMenu : Control
{
	public override void _Ready()
	{
		SetAnchorsPreset(LayoutPreset.FullRect);

		// фон — игровая текстура + затемнение
		var bg = new TextureRect
		{
			Texture = Load("res://assets/backgrounds/forest.png"),
			StretchMode = TextureRect.StretchModeEnum.KeepAspectCovered,
			MouseFilter = MouseFilterEnum.Ignore
		};
		bg.SetAnchorsPreset(LayoutPreset.FullRect);
		AddChild(bg);
		var shade = new ColorRect { Color = new Color(0.05f, 0.06f, 0.08f, 0.72f), MouseFilter = MouseFilterEnum.Ignore };
		shade.SetAnchorsPreset(LayoutPreset.FullRect);
		AddChild(shade);

		Theme = BuildTheme();

		var center = new CenterContainer();
		center.SetAnchorsPreset(LayoutPreset.FullRect);
		AddChild(center);

		var vbox = new VBoxContainer();
		vbox.AddThemeConstantOverride("separation", 22);
		center.AddChild(vbox);

		var title = new Label { Text = "ProjectR", HorizontalAlignment = HorizontalAlignment.Center };
		title.AddThemeFontSizeOverride("font_size", 64);
		title.AddThemeColorOverride("font_color", new Color(0.95f, 0.9f, 0.7f));
		vbox.AddChild(title);

		var sub = new Label { Text = "тестовый прототип", HorizontalAlignment = HorizontalAlignment.Center };
		sub.AddThemeColorOverride("font_color", new Color(0.75f, 0.77f, 0.8f));
		vbox.AddChild(sub);

		vbox.AddChild(new Control { CustomMinimumSize = new Vector2(0, 20) });

		AddButton(vbox, "Подземелье", () => GetTree().ChangeSceneToFile("res://scenes/Dungeon.tscn"));
		AddButton(vbox, "Просмотр спрайтов", () => GetTree().ChangeSceneToFile("res://scenes/SpriteViewer.tscn"));
		AddButton(vbox, "Нативные анимации", () => GetTree().ChangeSceneToFile("res://scenes/NativeViewer.tscn"));
		AddButton(vbox, "Выход", () => GetTree().Quit());
	}

	private void AddButton(Node parent, string text, System.Action onPressed)
	{
		var b = new Button { Text = text, CustomMinimumSize = new Vector2(340, 58) };
		b.Pressed += onPressed;
		parent.AddChild(b);
	}

	private Theme BuildTheme()
	{
		var theme = new Theme();
		const string fp = "res://assets/fonts/NotoSansJP-Bold.otf";
		var font = ResourceLoader.Exists(fp) ? ResourceLoader.Load<Font>(fp) : null;
		if (font != null) { theme.DefaultFont = font; theme.DefaultFontSize = 24; }

		const string btn = "res://assets/ui/Button_textbar.png";
		var normal = MakeStyle(btn, 16, 12, 12, Colors.White);
		var hover = MakeStyle(btn, 16, 12, 12, new Color(1.2f, 1.2f, 1.2f));
		var pressed = MakeStyle(btn, 16, 12, 12, new Color(0.8f, 0.8f, 0.8f));
		if (normal != null)
		{
			theme.SetStylebox("normal", "Button", normal);
			theme.SetStylebox("hover", "Button", hover ?? normal);
			theme.SetStylebox("pressed", "Button", pressed ?? normal);
			theme.SetStylebox("focus", "Button", new StyleBoxEmpty());
			theme.SetColor("font_color", "Button", new Color(0.93f, 0.9f, 0.82f));
			theme.SetColor("font_hover_color", "Button", Colors.White);
		}
		return theme;
	}

	private static StyleBoxTexture MakeStyle(string path, int mx, int my, int content, Color modulate)
	{
		var tex = Load(path);
		if (tex == null) return null;
		var sb = new StyleBoxTexture { Texture = tex, ModulateColor = modulate };
		sb.TextureMarginLeft = mx; sb.TextureMarginRight = mx;
		sb.TextureMarginTop = my; sb.TextureMarginBottom = my;
		sb.ContentMarginLeft = mx + content; sb.ContentMarginRight = mx + content;
		sb.ContentMarginTop = my + content; sb.ContentMarginBottom = my + content;
		return sb;
	}

	private static Texture2D Load(string path)
		=> ResourceLoader.Exists(path) ? ResourceLoader.Load<Texture2D>(path) : null;
}
