#include "MainMenu.h"

#include <godot_cpp/classes/resource_loader.hpp>
#include <godot_cpp/classes/scene_tree.hpp>

using namespace godot;

namespace blitz {

void MainMenu::_bind_methods() {
	godot::ClassDB::bind_method(godot::D_METHOD("on_connected"), &MainMenu::OnConnected);
	ADD_SIGNAL(MethodInfo("create_game", PropertyInfo(Variant::INT, "port"), PropertyInfo(Variant::BOOL, "dedicated")));
	ADD_SIGNAL(MethodInfo("join_game", PropertyInfo(Variant::STRING, "address"), PropertyInfo(Variant::INT, "port")));
	ADD_SIGNAL(MethodInfo("change_scene"));
}

MainMenu::MainMenu() {}

MainMenu::~MainMenu() {}

void MainMenu::_ready() {
	Node* container = find_child("Container");
	DEV_ASSERT(container);

	m_JoinButton = Object::cast_to<Button>(container->find_child("JoinButton"));
	m_CreateButton = Object::cast_to<Button>(container->find_child("CreateButton"));
	m_QuitButton = Object::cast_to<Button>(container->find_child("QuitButton"));

	DEV_ASSERT(m_JoinButton);
	DEV_ASSERT(m_CreateButton);
	DEV_ASSERT(m_QuitButton);

	m_JoinButton->connect("pressed", callable_mp(this, &MainMenu::OnJoinPressed));
	m_CreateButton->connect("pressed", callable_mp(this, &MainMenu::OnCreatePressed));
	m_QuitButton->connect("pressed", callable_mp(this, &MainMenu::OnQuitPressed));
}

void MainMenu::OnConnected() {
	emit_signal("change_scene");
	set_visible(false);
}

void MainMenu::OnJoinPressed() {
	emit_signal("join_game", "localhost", 25565);
}

void MainMenu::OnCreatePressed() {
	emit_signal("create_game", 25565, false);
}

void MainMenu::OnQuitPressed() {
	get_tree()->quit();
}

}  // namespace blitz