/*
 * 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>
#include <vector>

extern Manager manager;

UIText::UIText(std::string 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(std::string inputText, int x, int y, int letterScale, std::string tag)
{
	//Parse input text into an array of char
	int posX = x;
	int posY = y;
	int i = 0;
//	printf(inputText);
	char current = inputText[i];
//     const char* newLineChar{10};
    
//     std::vector<char> writableStr(inputText.begin(), inputText.end());
//     writableStr.push_back('\0');
        
	do
	{
//         for (int i = 0; i < writableStr.size(); i++)
//         {
//             if (writableStr.at(i) == '\n')
//             {
//                 printf("found new line");
//             }
//         }
		++i;
		if (strcmp(&current,"\n")!=0)
		{
			posX += letterWidth;
		}
		else
		{
//             printf("new line detected/n");
			posX = x;
			posY += letterHeight;
		}
		UIText::AddLetter(posX, posY, current, tag, letterScale);
		current = inputText[i];
	} while ((strcmp(&current,"\0"))!=0);
}


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

// void UIText::RemoveLetter()
// {
// }
