#include "game_ui.h"

GameUI *GameUI::singleton_instance{nullptr};

void GameUI::_bind_methods() {
	ClassDB::bind_method(D_METHOD("display_message", "text"), &self_type::display_message);
	ClassDB::bind_static_method("GameUI", D_METHOD("get_singleton"), &self_type::get_singleton);
}

void GameUI::_notification(int what) {
	if(Engine::get_singleton()->is_editor_hint()) {
		return;
	}
	if(what == NOTIFICATION_ENTER_TREE) {
		ERR_FAIL_COND_EDMSG(self_type::singleton_instance != nullptr, "GameUI instance already exists, deleting second instance");
		self_type::singleton_instance = this;
	} else if(what == NOTIFICATION_EXIT_TREE) {
		self_type::singleton_instance = nullptr;
	}
	if(what == NOTIFICATION_READY) {
		this->message = Object::cast_to<RichTextLabel>(this->get_node(NodePath("%MessageLabel")));
		this->clear_message_timer = Object::cast_to<Timer>(this->get_node(NodePath("%ClearMessageTimer")));
	}
}

GameUI *GameUI::get_singleton() {
	return self_type::singleton_instance;
}

void GameUI::display_message(String text) {
	this->message->set_text(text);
	this->clear_message_timer->start(0.0);
}
