#include "NumText.hpp"
#include "Echo.hpp"
#include <cmath>

NumText::NumText()
{
	_semicolon = false;
	_percent = false;
}

NumText::~NumText()
{

}

void NumText::setTexture(Texture* texture)
{
	_font = texture;
}
void NumText::setNumber(unsigned number)
{
	_nums.clear();
	if(!number) {
		_nums.resize(1);
		_nums.back().setTexture(*_font);
		_nums.back().setPosition(0 * 13, 0);
		_nums.back().setTextureRect(IntRect(0, 0 * 14, 13, 14));
	}
	else {
		int count = std::log10(number) + 1 + _percent + _semicolon;
		_nums.resize(count);
		for(int i = 0; i < count; ++i) {
			_nums[i].setTexture(*_font);
			_nums[i].setPosition((count - i - 1) * 8, 0);
			unsigned offset = 0;
			if(_semicolon && i == 2) {
				offset = 10;
			}
			else if(_percent && i == 0) {
				offset = 11;
			}
			else {
				offset = number % 10;
				number /= 10;
			}
			if(offset < 11) {
				_nums[i].setTextureRect(IntRect(0, offset * 14, 8, 7));
			}
			else {
				_nums[i].setTextureRect(IntRect(0, offset * 14, 13, 10));
				_nums[i].move(0, -1.5f);
			}
		}
	}
}

void NumText::semicolon()
{
	_semicolon = true;
}
void NumText::percent()
{
	_percent = true;
}

void NumText::draw(RenderTarget& target, RenderStates states) const
{
	states.transform *= getTransform();

	for(unsigned i = 0; i < _nums.size(); ++i) {
		target.draw(_nums[i], states);
	}
}
