#include "widgets.h"

#define MIN(a, b) ((a)<(b)?(a):(b))
#define MAX(a, b) ((a)>(b)?(a):(b))
#define TOUCHDISTANCE 4
#define distance(x1, y1, x2, y2)	(((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1)))

s8 costable[255] = {127, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 125, 125, 125, 125, 124, 124, 124, 123, 123, 123, 122, 122, 121, 121, 121, 120, 120, 119, 118, 118, 117, 117, 116, 116, 115, 114, 114, 113, 112, 111, 111, 110, 109, 108, 107, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 84, 83, 82, 81, 80, 79, 77, 76, 75, 74, 72, 71, 70, 68, 67, 66, 64, 63, 62, 60, 59, 58, 56, 55, 53, 52, 50, 49, 48, 46, 45, 43, 42, 40, 39, 37, 36, 34, 33, 31, 30, 28, 27, 25, 24, 22, 21, 19, 17, 16, 14, 13, 11, 10, 8, 7, 5, 3, 2, 0, 0, -2, -3, -5, -7, -8, -10, -11, -13, -14, -16, -17, -19, -21, -22, -24, -25, -27, -28, -30, -31, -33, -34, -36, -37, -39, -40, -42, -43, -45, -46, -48, -49, -50, -52, -53, -55, -56, -58, -59, -60, -62, -63, -64, -66, -67, -68, -70, -71, -72, -74, -75, -76, -77, -79, -80, -81, -82, -83, -84, -86, -87, -88, -89, -90, -91, -92, -93, -94, -95, -96, -97, -98, -99, -100, -101, -102, -103, -104, -105, -106, -107, -107, -108, -109, -110, -111, -111, -112, -113, -114, -114, -115, -116, -116, -117, -117, -118, -118, -119, -120, -120, -121, -121, -121, -122, -122, -123, -123, -123, -124, -124, -124, -125, -125, -125, -125, -126, -126, -126, -126, -126, -126, -126, -126, -126, -126};

s8 fcos(int val)
{
	return costable[(254 - abs(254 - MIN(val, 508)))];
}

Widget *head;

void PrintWidgets(void)
{
	Widget *current = head;
	while (current != NULL)
	{
		iprintf("(%d 0x%x)->", current->id, current);
		current = current->next;
	}
	iprintf("NULL\n");
}

void AddWidget(Widget *add)
{
	// find the end of the chain
	Widget *current = head;
	Widget *last = NULL;
	bool found = false;

	while (current != NULL && !found)
	{
		// if the current one is lower than we are
		if (current->y > add->y)
		{
			if (last)
			{
				// drop it in between the last one and this one
				add->next = last->next;
				last->next = add;
			}
			else
			{
				// drop it before the start of the head
				add->next = head;
				head = add;
			}
			found = true;
		}

		last = current;
		if (current)
			current = current->next;
	}
	
	if (!found)
	{
		// if we have a start
		if (last)
		{
			// add it after the end
			last->next = add;
		}
		else
		{
			head = add;
		}
	}
	
	//PrintWidgets();
}

void DelWidget(int id)
{
	Widget *current = head;
	Widget *last = NULL;
	
	while (current != NULL)
	{
		// if we've found one with the ID we're looking for
		if (current->id == id)
		{
			// if we're not the start of the chain
			if (last)
			{
				last->next = current->next;
				free(current->data);
				free(current);
				current = last;
			}
			else
			{
				// we're the start of the chain
				head = current->next;
				free(current->data);
				free(current);
				current = head;
			}
		}
		
		last = current;
		if (current)
			current = current->next;
	}
	//PrintWidgets();
}

Widget *MakeSlider(int id, int x, int y, int value, int range, int size, char orientation)
{
	Widget *new = malloc(sizeof(Widget));
	Slider *data = malloc(sizeof(Slider));
	new->id = id;
	new->x = x;
	new->y = y;
	new->value = value;
	new->range = range;
	new->type = 's';
	new->data = (void *)data;
	new->next = NULL;
	((Slider *)new->data)->size = size;
	((Slider *)new->data)->orientation = orientation;
	return new;
}

Widget *MakeKnob(int id, int x, int y, int value, int range, int radius)
{
	Widget *new = malloc(sizeof(Widget));
	Knob *data = malloc(sizeof(Knob));
	new->id = id;
	new->x = x;
	new->y = y;
	new->value = value;
	new->range = range;
	new->type = 'k';
	new->data = (void *)data;
	new->next = NULL;
	((Knob *)new->data)->radius = radius;
	return new;
}

Widget *MakeToggle(int id, int x, int y, int value, int range, int size)
{
	Widget *new = malloc(sizeof(Widget));
	Toggle *data = malloc(sizeof(Toggle));
	new->id = id;
	new->x = x;
	new->y = y;
	new->value = value;
	new->range = range;
	new->type = 't';
	new->data = (void *)data;
	new->next = NULL;
	((Toggle*)new->data)->size = size;
	return new;
}

void SetSlider(Widget *which, int dx, int dy)
{
	char buf[255];
	int val = 0;
	
	if (((Slider *)which->data)->orientation == 'h')
		val = which->value + dx;
	else
		val = which->value - dy;
	
	which->value = MIN(which->range, MAX(0, val));
	
	sprintf(buf, "%d %d;\n", which->id, which->value);
	sendpacket(buf);
}

void SetKnob(Widget *which, int dx, int dy)
{
	char buf[255];
	
	which->value = MIN(which->range, MAX(0, which->value + dx));
	
	sprintf(buf, "%d %d;\n", which->id, which->value);
	sendpacket(buf);
}

void SetToggle(Widget *which)
{
	char buf[255];
	
	which->value = which->value > 0 ? 0 : which->range;
	
	sprintf(buf, "%d %d;\n", which->id, which->value);
	sendpacket(buf);
}

void SetValue(int id, int value)
{
	Widget *current = head;
	while (current != NULL)
	{
		if (current->id == id)
		{
			current->value = MIN(current->range, MAX(0, value));
		}
		current = current->next;
	}
}

void DrawAllWidgets()
{
	Widget *current = head;
	while (current != NULL)
	{
		DrawWidget(current);
		current = current->next;
	}
}

Widget *FindTouched(Widget *head, int x, int y)
{
	Widget *current = head;
	bool found = false;
	
	// search through every widget in a linked list until we find one we've touched
	while (current != NULL && !found)
	{
		// is this a slider?
		if (current->type == 's')
		{
			// is it horizontally oriented?
			if (((Slider *)current->data)->orientation == 'h')
			{
				// are we touching the bar?
				if ((y >= current->y && y <= current->y + ((Slider *)current->data)->size) &&
					(x >= current->x + current->value - TOUCHDISTANCE && x <= current->x + current->value + TOUCHDISTANCE))
					found = true;
			}
			else
			{
				// are we touching the bar?
				if ((x >= current->x && x <= current->x + ((Slider *)current->data)->size) &&
					(y >= (current->y + current->range) - (current->value + TOUCHDISTANCE) && y <= (current->y + current->range) - (current->value - TOUCHDISTANCE)))
					found = true;
			}
		}
		// a knob?
		else if (current->type == 'k')
		{
			if (distance(current->x, current->y, x, y) < (((Knob *)current->data)->radius * ((Knob *)current->data)->radius))
				found = true;
		}
		// a toggle?
		else if (current->type == 't')
		{
			if ((x >= current->x && x <= current->x + ((Toggle *)current->data)->size) && (y >= current->y && y <= current->y + ((Toggle *)current->data)->size))
				found = true;
		}
		
		if (!found)
			current = current->next;
	}
	return current;
}

void DrawWidget(Widget *which)
{
	// is this a slider?
	if (which->type == 's')
	{
		if (((Slider *)which->data)->orientation == 'h')
		{
			draw_line(which->x, which->y, which->x + which->range, which->y, FGCOLOR, 1);
			draw_line(which->x, which->y + ((Slider *)which->data)->size, which->x + which->range, which->y + ((Slider *)which->data)->size, FGCOLOR, 1);
			draw_line(which->x, which->y, which->x, which->y + ((Slider *)which->data)->size, FGCOLOR, 1);
			draw_line(which->x + which->range, which->y, which->x + which->range, which->y + ((Slider *)which->data)->size, FGCOLOR, 1);
			draw_line(which->x + which->value, which->y, which->x + which->value, which->y + ((Slider *)which->data)->size, FGCOLOR, 1);
		}
		else
		{
			draw_line(which->x, which->y, which->x + ((Slider *)which->data)->size, which->y, FGCOLOR, 1);
			draw_line(which->x, which->y + which->range, which->x + ((Slider *)which->data)->size, which->y + which->range, FGCOLOR, 1);
			draw_line(which->x, which->y, which->x, which->y + which->range, FGCOLOR, 1);
			draw_line(which->x + ((Slider *)which->data)->size, which->y, which->x + ((Slider *)which->data)->size, which->y + which->range, FGCOLOR, 1);
			draw_line(which->x, which->y + which->range - which->value, which->x + ((Slider *)which->data)->size, which->y + which->range - which->value, FGCOLOR, 1);
		}
	}
	else if (which->type == 'k')
	{
		draw_circle(which->x, which->y, ((Knob *)which->data)->radius, FGCOLOR, 1);
		draw_line(which->x, which->y, which->x - ((Knob *)which->data)->radius * fcos(255 * which->value / (which->range * 2 / 3)) / 127, which->y + ((Knob *)which->data)->radius * fcos(255 * which->value / (which->range * 2 / 3) + 127) / 127, FGCOLOR, 1);
	}
	else if (which->type == 't')
	{
		// Proto: draw_line(int x0, int y0, int x1, int y1, uint16 color, int thickness);
		int toggle_size = ((Toggle *)which->data)->size;
		draw_line(which->x,               which->y,               which->x + toggle_size, which->y,               FGCOLOR, 1);
		draw_line(which->x + toggle_size, which->y,               which->x + toggle_size, which->y + toggle_size, FGCOLOR, 1);
		draw_line(which->x + toggle_size, which->y + toggle_size, which->x              , which->y + toggle_size, FGCOLOR, 1);
		draw_line(which->x              , which->y + toggle_size, which->x              , which->y,               FGCOLOR, 1);
		
		if (which->value > 0)
		{
			// the cross:
			draw_line(which->x + toggle_size, which->y,               which->x,               which->y + toggle_size, FGCOLOR, 1);
			draw_line(which->x,               which->y,               which->x + toggle_size, which->y + toggle_size, FGCOLOR, 1);
	        }
	}
}

