#include "Morault_Canvas.h"
#include "Morault_Resources.h"
#include <raymath.h>
#include "pugixml.hpp"
#include <regex>

void Morault::UI::Image::Draw(Morault::Gameplay::Player player)
{
	using namespace Morault::Resources;
	Vector2 anchorPosition;
	if (anchor == BL) anchorPosition = { 0, (float)GetScreenHeight() };
	if (anchor == BR) anchorPosition = { (float)GetScreenWidth(), (float)GetScreenHeight() };
	if (anchor == TL) anchorPosition = { 0, 0 };
	if (anchor == TR) anchorPosition = { (float)GetScreenWidth(), 0};
	if (anchor == CN) anchorPosition = { (float)GetScreenWidth() / 2, (float)GetScreenHeight() / 2 };
	Vector2 finalPosition = anchorPosition + Vector2((float)posx, (float)posy);

	DrawTextureEx(std::get<Texture2D>(Textures(get(src))), finalPosition, 0, scale, WHITE);
}

void Morault::UI::Text::Draw(Morault::Gameplay::Player player)
{
	using namespace Morault::Resources;
	Vector2 anchorPosition;
	if (anchor == BL) anchorPosition = { 0, (float)GetScreenHeight() };
	if (anchor == BR) anchorPosition = { (float)GetScreenWidth(), (float)GetScreenHeight() };
	if (anchor == TL) anchorPosition = { 0, 0 };
	if (anchor == TR) anchorPosition = { (float)GetScreenWidth(), 0 };
	if (anchor == CN) anchorPosition = { (float)GetScreenWidth() / 2, (float)GetScreenHeight() / 2 };
	Vector2 finalPosition = anchorPosition + Vector2((float)posx, (float)posy);
	std::string temp = text;
	temp.replace(temp.find("[PLAYERHP]"), std::string("[PLAYERHP]").size(), std::to_string(player.data.hp));
	DrawText(temp.c_str(), finalPosition.x, finalPosition.y, size, color);
}

Morault::UI::Canvas::Canvas()
{
}

Morault::UI::Canvas::Canvas(std::string canvasfile)
{
	pugi::xml_document doc;
	pugi::xml_parse_result result = doc.load_file(canvasfile.c_str());

	if (!result) throw;
	
	pugi::xml_node root = doc.child("canvas");

	for (const auto& img : root.children("img")) {
		UI::Image newimg;
		newimg.anchor = (std::string)img.attribute("anchor").as_string() == "bl" ? BL :
			(std::string)img.attribute("anchor").as_string() == "br" ? BR :
			(std::string)img.attribute("anchor").as_string() == "tl" ? TL :
			(std::string)img.attribute("anchor").as_string() == "tr" ? TR :
			CN;
		newimg.posx = img.attribute("posx").as_int();
		newimg.posy = img.attribute("posy").as_int();


		newimg.scale = img.attribute("scale").as_float();

		newimg.src = (std::string)img.attribute("src").as_string();
		elements.push_back(std::make_shared<UI::Image>(newimg));
	}

	for (const auto& txt : root.children("text")) {
		UI::Text newtxt;
		newtxt.anchor = (std::string)txt.attribute("anchor").as_string() == "bl" ? BL :
			(std::string)txt.attribute("anchor").as_string() == "br" ? BR :
			(std::string)txt.attribute("anchor").as_string() == "tl" ? TL :
			(std::string)txt.attribute("anchor").as_string() == "tr" ? TR :
			CN;
		newtxt.posx = txt.attribute("posx").as_int();
		newtxt.posy = txt.attribute("posy").as_int();
		newtxt.color.r = (unsigned char)std::stoi(txt.attribute("colr").as_string());
		newtxt.color.g = (unsigned char)std::stoi(txt.attribute("colg").as_string());
		newtxt.color.b = (unsigned char)std::stoi(txt.attribute("colb").as_string());
		newtxt.color.a = (unsigned char)std::stoi(txt.attribute("cola").as_string());
		


		newtxt.size = txt.attribute("size").as_int();

		newtxt.text = txt.child_value();
		elements.push_back(std::make_shared<UI::Text>(newtxt));
	}


}

void Morault::UI::Canvas::DrawCanvas(Morault::Gameplay::Player player)
{
	for (int i = 0; i < elements.size(); i++) elements[i]->Draw(player);
}

Morault::UI::Canvas::~Canvas()
{
	elements.clear();
}
