#include "empire.h"

#define OFFSET_JUMP 40

void _textline::init(int _x, int _y, int _w, int _h, bool _enter_clears, int charlimit)
{
	reset();
	x = _x;
	y = _y;
	w = _w;
	h = _h;
	offset = 0;
	limit = charlimit;
	enter_clears = _enter_clears;
}

void _textline::reset()
{
	edittext = "";
	iter = edittext.begin();
	caret = 0;
	offset = 0;
	insert = true;
	focused = true;
}

void _textline::set_text(string text)
{
	edittext = text;
	caret = edittext.length();
	iter = edittext.end();
}

string _textline::current_text() const
{
	return edittext;
}

void _textline::set_color(int c)
{
	color = c;
}

string _textline::check(float bgalpha)
{  //textline code by 23yrold3yrold, just modified into a class
	char ASCII, scancode;
	int newkey;
	string returntext;
	
	if (!focused)
	{
		draw(bgalpha);
		return "/NOTHING YET/";
	}
	
	while(keypressed())
	{
		newkey = readkey();
		ASCII = newkey & 0xff;
		scancode = newkey >> 8;

		// a character key was pressed; add it to the string
		if(ASCII >= 32 && ASCII <= 126)
		{  // add the new char, inserting or replacing as need be
			if ((limit == 0) || (caret < limit))
			{
				if (insert || iter == edittext.end())
				{
					iter = edittext.insert(iter, ASCII);
					caret++;  // increment both the caret and the iterator
					iter++;
				}
				else
				{
					edittext.replace(caret, 1, 1, ASCII);
					caret++;  // increment both the caret and the iterator
					iter++;
				}
				
				if (normal.Width(edittext.substr(0, caret + 1)) - offset > w)
					offset += OFFSET_JUMP;
			}
		}
		else  // some other, "special" key was pressed; handle it here
		{
			switch(scancode)
			{
				case KEY_DEL:
					if (iter != edittext.end()) iter = edittext.erase(iter);
					break;
				case KEY_BACKSPACE:
					if(iter != edittext.begin())
					{
						caret--;
						iter--;
						iter = edittext.erase(iter);
						
						if (normal.Width(edittext.substr(0, caret + 1)) - offset < 1)
						{
							offset -= OFFSET_JUMP;
							if (offset < 0)
								offset = 0;
						}
					}
					break;
				case KEY_RIGHT:
					if (iter != edittext.end())
					{
						caret++;
						iter++;

						if (normal.Width(edittext.substr(0, caret + 1)) - offset > w)
							offset += OFFSET_JUMP;
					}
					break;
				case KEY_LEFT:
					if (iter != edittext.begin())
					{
						caret--;
						iter--;
						
						if (normal.Width(edittext.substr(0, caret + 1)) - offset < 1)
						{
							offset -= OFFSET_JUMP;
							if (offset < 0)
								offset = 0;
						}
					}
					break;
				case KEY_INSERT:
					if(insert) insert = 0; else insert = 1;
					break;
				case KEY_ENTER:
					if (enter_clears)
					{
						returntext = edittext;
						reset();
						return returntext;
					}
					break;
				case KEY_HOME:
					caret = 0;
					iter = edittext.begin();
					offset = 0;
					break;
				case KEY_END:
					caret = edittext.length();
					iter = edittext.end();
					offset = normal.Width(edittext) - w;
					if (offset < 0)
						offset = 0;
					break;
			}
		}
	}
	draw(bgalpha);
	
	return "/NOTHING YET/";
}

void _textline::draw(float bgalpha)
{
	int width = normal.Width(edittext.substr(0, caret));
	
	int texty = y + (h - normal.Height("qjhk")) / 2;
	
	buffer_rectfill(x, y, x + w, y + h, DGREY, bgalpha);
	
	clipbox_textout_ex(normal, edittext, x - offset, texty, x, y, w, h, color);
	
	if (focused)
	{
		if (insert)
		{
			buffer_vline(x + width - offset, y, y + h, color);
		}
		else
		{
			buffer_rect(x + width - offset, y, x + normal.Width(edittext.substr(0, caret + 1)) - offset, y + h, color);
		}
	}
}

bool _textline::mouseclicked()
{
	if (mouse_clicked())
	{
		if ((mouse_x >= x) && (mouse_x <= x + w) && (mouse_y >= y) && (mouse_y <= y + h))
			return true;
	}
	return false;
}

void _textline::enable()
{
	focused = true;
}

void _textline::disable()
{
	focused = false;
}
