#include <stdio.h>

#include "iodefine.h"
#include "key_ctrl.h"
#include "ui.h"
#include "TouchPanel.h"

extern unsigned short global_inX;
extern unsigned short global_inY;

#define KEY_ON	0
#define KEY_OFF	1

static volatile int cmd_up;
static volatile int cmd_down;
static volatile int cmd_repeat;
static volatile int cmd_shuffle;
static volatile int cmd_pause;

static volatile int stat_repeat;
static volatile int stat_shuffle;
static volatile int flag_up;

void init_key(void)
{
	TOUCH_Initialize();
	cmd_up = 0;
	cmd_down = 0;
	cmd_repeat = 0;
	cmd_shuffle = 0;
	cmd_pause = 0;

	stat_repeat = 0;
	stat_shuffle = 0;
	flag_up = 0;
	return;
}

int get_pd(void)
{
	return TOUCH_GetPenDown();
}

unsigned int get_ad_x(void)
{
	return TOUCH_GetXYvalue() & 0xffff;
}

unsigned int get_ad_y(void)
{
	return (TOUCH_GetXYvalue() >> 16) & 0xffff;
}

unsigned int get_x(void)
{
	return TOUCH_GetXY() & 0xffff;
}

unsigned int get_y(void)
{
	return 240 - ((TOUCH_GetXY() >> 16) & 0xffff);
}

void key_event(void)
{
	{
		int key = (get_pd() == 1) ? KEY_ON : KEY_OFF;
		static int status = -1;

		switch(status)
		{
			case 0:
			case 1:
			case 2:
				status = (key == KEY_ON) ? status + 1 : 0;
				break;
			case 3:
				//printf("X=%d, Y=%d, x=%d, y=%d\r\n", get_ad_x(), get_ad_y(), get_x(), get_y());
				if(get_y() > ((240-20)/2 + 20))
					cmd_up = 1;
				else
				if(get_y() > 20)
					cmd_down = 1;
				else
				if(get_x() < 320/3*1)
					cmd_pause = cmd_pause == 0 ? 1 : 0;
				else
				if(get_x() < 320/3*2)
					cmd_repeat = 1;
				else
					cmd_shuffle = 1;

				status = (key == KEY_ON) ? status + 1 : 0;
				break;
			default:
				status = (key == KEY_ON) ? status : 0;
				break;
		}
	}
	return;
}

int key_cmd_chk(void)
{
	if(cmd_repeat)
	{
		cmd_repeat = 0;
		stat_repeat = stat_repeat ? 0 : 1;
		ui_disp_repeat(1);
		printf("repeat %s\r\n", stat_repeat ? "on" : "off");
	}

	if(cmd_shuffle)
	{
		cmd_shuffle = 0;
		stat_shuffle = stat_shuffle ? 0 : 1;
		ui_disp_shuffle(1);
		printf("shuffle %s\r\n", stat_shuffle ? "on" : "off");
	}

	if(cmd_pause)
	{
		ui_disp_pause(1);
		printf("pause\r\n");
		dma1_stop();
		while(cmd_pause)
		{
			if(cmd_repeat)
			{
				cmd_repeat = 0;
				stat_repeat = stat_repeat ? 0 : 1;
				ui_disp_repeat(1);
				printf("repeat %s\r\n", stat_repeat ? "on" : "off");
			}

			if(cmd_shuffle)
			{
				cmd_shuffle = 0;
				stat_shuffle = stat_shuffle ? 0 : 1;
				ui_disp_shuffle(1);
				printf("shuffle %s\r\n", stat_shuffle ? "on" : "off");
			}

			if(cmd_down)break;
			if(cmd_up)break;
		}
		dma1_start();
		ui_disp_pause(1);
		printf("continue\r\n");
	}

	if(cmd_down)
	{
		cmd_pause = 0;
		cmd_down = 0;
		flag_up = 0;
		printf("down\r\n");
		return 1;
	}

	if(cmd_up)
	{
		cmd_pause = 0;
		cmd_up = 0;
		flag_up = 1;
		printf("up\r\n");
		return 1;
	}

	return 0;
}

int key_pause(void)
{
	return cmd_pause;
}

int key_repeat(void)
{
	return stat_repeat;
}

int key_shuffle(void)
{
	return stat_shuffle;
}

int key_up(void)
{
	int tmp = flag_up;
	flag_up = 0;
	return tmp;
}
