/*
 * UIText.cpp
 *
 *  Created on: May 21, 2020
 *      Author: ayoungblood
 */

#include "UIText.h"
#include "../game/Game.hpp"
#include "../ecs/ECS.h"
#include "../ecs/Components.h"
#include "string.h"
#include <iostream>
#include <stdio.h>

extern Manager manager;

UIText::UIText(const char* text, std::string texId, int x, int y, int letterW, int letterH, int lScale)
{
	inputText = text;
	textureID = texId;
	posX = x;
	posY = y;
	letterWidth = letterW;
	letterHeight = letterH;
	scale = lScale;
}

UIText::~UIText()
{
}

void UIText::ParseString(const char* inputText, int x, int y, int scale)
{
	//Parse input text into an array of char
	int posX = x;
	int posY = y;
	int i = 0;
//	printf(inputText);
	char current = inputText[i];
	do
	{
		++i;
		if (strcmp(&current,"\n"))
		{
			posX += letterWidth;
		}
		else
		{
//			printf("new line detected");
			posX = x;
			posY += letterHeight;
		}
		UIText::AddLetter(posX, posY, current);
		current = inputText[i];
	} while ((strcmp(&current,"\0"))!=0);
}


void UIText::AddLetter(int xpos, int ypos, char crnt)
{
	auto& letter(manager.addEntity());
	letter.addComponent<TransformComponent>(xpos*scale, ypos*scale, letterWidth, letterHeight, 1);
//	printf("Scale: %d\n",scale);
	letter.addComponent<SpriteComponent>("font", SpriteComponent::spriteText, crnt, letterWidth, letterHeight, scale);
	letter.addGroup(Game::groupUI_Layer1);
}
