#include <avr/io.h>

#include "main.h"
#include "keyb.h"
#include "pinout.h"
#include "config.h"
#include "menu.h"
#include "system.h"
#include "lcd.h"
#include <ctype.h>

#ifdef RFID_LCD_TERMINAL
#include "RFID_LCDTERM.h"
#endif

unsigned char menuType = INACTIVE_MENU;
unsigned char curs, oldcurs;
unsigned char event;
unsigned char menu_active;
char *text;
unsigned char i;
const unsigned char MAIN_MENU_POS = 60;
unsigned char num[2];
unsigned long int number;
enum e_num_type num_type;
unsigned char num_len;
unsigned char num_nonzero = 0;
enum e_int_set_state int_set_state = INT_SET_CANCEL;

//-----------------------------------------
void deactive_menu()
{
	menuType = INACTIVE_MENU;
}
//-----------------------------------------
/*void menu_show_info()
{
	menuType = INFO_MENU;
	lcd_clr();
	printf("CUTTER Systems\nSSI Schaefer\nLCD Terminal v1.01\n(C) 2010");
	menu_timer = 4;
}*/
//-----------------------------------------
/*void preset_number(unsigned char len, unsigned char *preset_num)
{
	unsigned char i, j;

	num_nonzero = 0;	
	if(len <= sizeof(num))
	{
		j = sizeof(num);
		for(i = len; i > 0; i--)
			if(isdigit(preset_num[i - 1]))
			{
				if(j > 0)
				{
					j--;
					num[j] = preset_num[i - 1];	//kopirovani od nejnizsiho radu
				}
				num_nonzero = 1;
			}
		
		while(j > 0)
		{
			j--;
			num[j] = ' ';
		}
	}
}*/
//-----------------------------------------
//zadavani cisla
//void SetNumber(enum e_num_type nt, unsigned char len, unsigned char *preset_num)
void SetNumber(enum e_num_type nt, unsigned char len)
{
	num_len = len;
	//preset_number(len, preset_num);
	IntSetKey(INT_SET_INIT);
	menuType = INT_SET_MENU;
	num_type = nt;
}
//-----------------------------------------
void IntSetKey(char ch)
{
	unsigned char i;
//	unsigned long int dec;
	static unsigned char setpos;
	switch (ch) 
	{
		case INT_SET_INIT:
		case 1:
			for (i = 0; i < num_len; i++)
			{
				num[i] = ' ';
			}
/*
			if(sizeof(num) > num_len)
				num[sizeof(num) - 1] = '\0';			//nevypise nadbytecne nuly
*/
			//num[num_len-1] = 0x00;					//posledni znak (nejnizsi rad) je 0
			//setpos = num_len - 1;					//pocet znaku zbyvajicich k zapsani
			setpos = num_len;					//pocet znaku zbyvajicich k zapsani
			
/*			if(num_nonzero)
			{
				num_nonzero = 0;
				lcd_set_pos(INT_SET_POS - 9);
				//for(i = 0;i < num_len;i++)
				for(i = 0;i < sizeof(num);i++)
				{
					if(isdigit(num[i]))
					{
						printf("%c", num[i]);
						setpos--;
					}
					else if(num[i] == ' ')
						printf("%c", num[i]);
*/
/*					else if(num[i] == '\0')
						break;
*/
/*				}
			}
			else
*/
			lcd_set_pos(INT_SET_POS);

			//lcd_cursor_on();
			int_set_state = INT_SET_PROGRESS;
			break;
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			if (setpos > 0)
			{
				/*for (i = 0; i < num_len - 1; i++)
				{
					num[i] = num[i+1];				//posun znaku doleva
				}
				setpos--;							//dalsi zapsany znak
				num[num_len-1] = ch;					//nejnizsi rad
			*/
				for (i = 0; i < sizeof(num); i++)
				{
					num[i] = num[i+1];				//posun znaku doleva
				}
				setpos--;							//dalsi zapsany znak
				num[sizeof(num) - 1] = ch;					//nejnizsi rad
				
				//lcd_cursor_off();
				lcd_set_pos(INT_SET_POS);
				//printf("%s", num);
				for(i = 0;i < sizeof(num);i++)
					printf("%c", num[i]);
				//lcd_cursor_on();
			
			}
			//num[num_len-2] = ch;					//druhy nejnizsi rad
			int_set_state = INT_SET_PROGRESS;
			break;
		case BCK:
			//if (setpos <= (num_len - 2))			
			if (setpos <= (num_len - 1))
			{
				/*for (i = (num_len - 1); i > 0; i--)
				{
					num[i] = num[i-1];			//posun znaku doprava
				}
				setpos++;						//smazany znak
				*/
				for (i = sizeof(num) - 1; i > 0; i--)
				{
					num[i] = num[i-1];			//posun znaku doprava
				}
				setpos++;						//smazany znak
			}
			else
			{
				menuType = INACTIVE_MENU;
				//lcd_cursor_off();
				int_set_state = INT_SET_CANCEL;
				break;
			}
			num[0] = 0x20;						//nejvyssi rad je ' '
			//lcd_cursor_off();
			lcd_set_pos(INT_SET_POS);
			//printf("%s", num);
			for(i = 0;i < sizeof(num);i++)
				printf("%c", num[i]);

			//lcd_cursor_on();
			int_set_state = INT_SET_PROGRESS;
			break;
		case ENTER:
			//i = num_len - 2;					
			//i = num_len - 1;					
			/*number = 0;
			dec = 1;
			while (i > 0)		//preformatovani
			{
				if (num[i] == 0x20)
					num[i] = 0;
				else
					num[i] -= 0x30;
				number += num[i] * dec;
				i--;
				dec *= 10;			
			}
			number += num[i] * dec;*/
/*
			do		//preformatovani
			{
				if (num[i] == 0x20)
					num[i] = '0';

			} while (i-- > 0);
*/

			for(i = 0;i < sizeof(num);i++)
				if(num[i] == ' ')
					num[i] = '0';
			//prazdne pozice jsou vyplnene mezerami
			//lcd_cursor_off();
			
			switch (num_type)
			{
				case NUM_YEAR:
					temp_rtc.year = (num[0] - 0x30) * 10 + (num[1] - 0x30)+ 2000;
					menuType = INACTIVE_MENU;
					int_set_state = INT_SET_OK;
					break;
				case NUM_MONTH:
					temp_rtc.month = (num[0] - 0x30) * 10 + (num[1] - 0x30);
					menuType = INACTIVE_MENU;
					int_set_state = INT_SET_OK;
					break;
				case NUM_DAY:
					temp_rtc.day = (num[0] - 0x30) * 10 + (num[1] - 0x30);
					menuType = INACTIVE_MENU;
					int_set_state = INT_SET_OK;
					break;
				case NUM_HOUR:
					temp_rtc.hour = (num[0] - 0x30) * 10 + (num[1] - 0x30);
					menuType = INACTIVE_MENU;
					int_set_state = INT_SET_OK;
					break;
				case NUM_MINUTE:
					temp_rtc.minute = (num[0] - 0x30) * 10 + (num[1] - 0x30);
					menuType = INACTIVE_MENU;
					int_set_state = INT_SET_OK;
					break;
				case NUM_SECOND:
					temp_rtc.second = (num[0] - 0x30) * 10 + (num[1] - 0x30);
					menuType = INACTIVE_MENU;
					int_set_state = INT_SET_OK;
					break;

/*				case PROD_CODE:
					//for(i = 0;i < num_len;i++)
					//	protocol.prod_code[i] = num[i];		//odesila se v ascii
					//protocol.prod_code[i + 1] = '\0';
					
					for(i = sizeof(num);i > sizeof(num) - num_len;i--)
					{
						j--;
						protocol.prod_code[j] = num[i - 1];		//odesila se v ascii
					}
					protocol.prod_code[num_len] = '\0';

					//protocol.prod_code = number;
					menuType = INACTIVE_MENU;
					int_set_state = INT_SET_OK;
					break;
				case CHARGE_NUMBER:
					//for(i = 0;i < num_len;i++)
					//	protocol.charge_number[i] = num[i];	//odesila se v ascii
					//protocol.charge_number[i + 1] = '\0';
					
					for(i = sizeof(num);i > sizeof(num) - num_len;i--)
					{
						j--;
						protocol.charge_number[j] = num[i - 1];	//odesila se v ascii
					}
					protocol.charge_number[num_len] = '\0';
					
					//protocol.charge_number = number;
					menuType = INACTIVE_MENU;
					int_set_state = INT_SET_OK;
					break;
				case EXP_DATE:
					//for(i = 0;i < num_len;i++)
					//	protocol.expiration_date[i] = num[i] - 0x30;	//prepocitava se
					
					for(i = sizeof(num);i > sizeof(num) - num_len;i--)
					{
						j--;
						if(num[i - 1] >= 0x30)
							protocol.expiration_date[j] = num[i - 1] - 0x30;	//prepocitava se
					}
					
					//protocol.expiration_date = number;
					menuType = INACTIVE_MENU;
					int_set_state = INT_SET_OK;
					break;
*/				
				default:
					int_set_state = INT_SET_CANCEL;
					break;
			}
			break;
		default:
			int_set_state = INT_SET_PROGRESS;
			break;
	}
}

//-----------------------------------
/*void ShowBarG(unsigned char bar)
{
	static unsigned char oldb = 0;
	if (bar == 0xff) 
	{
		oldb = 0;
		lcd_set_pos(BARG_POS);
		printf("                    ");
	} 
	else 
	{
		if (bar > 20) bar = 20;
		while (oldb != bar)
		{
			if (oldb > bar)
			{
				oldb--;
				lcd_set_pos(BARG_POS + oldb);
				lcd_send_byte(' ');
			}
			else 
			{
				lcd_set_pos(BARG_POS + oldb);
				lcd_send_byte(0xff);
				oldb++;
			}
		}
		oldb = bar;
		lcd_set_pos(BARG_POS + 23);
		printf("%3d%% ",bar*5);
	}
}*/
//-----------------------------------
/*void SetContrKey(char ch)
{
	static unsigned char contr;
	switch (ch)
	{
		case BARG_SET_INIT:
			time_disp = 0;
			lcd_clr();
			printf("Set LCD Contrast");
			ShowBarG(0xff);
			contr = cfg.lcd_contr;
			ShowBarG(contr);
			menuType = SET_CONTR_MENU;
			break;
		case LEFT:
		case DOWN:
			if (contr)
				contr --;
			ShowBarG(contr);
			LCD_Contr(contr);
			break;
		case UP:
		case RIGHT:
			if (contr < 20)
				contr ++;	
			ShowBarG(contr);
			LCD_Contr(contr);
			break;
		case ENTER:
			cfg.lcd_contr = contr;
			save_config();
			menuType = INACTIVE_MENU;
			break;
		case BCK:
			LCD_Contr(cfg.lcd_contr);
			menuType = INACTIVE_MENU;
			break;
		default:
			break;				
	}
}*/
//-----------------------------------
/*void SetContr()
{
	SetContrKey(BARG_SET_INIT);
}*/
//-----------------------------------
/*void SetLightKey(char ch)
{
	static unsigned char light;
	switch (ch)
	{
		case BARG_SET_INIT:
			time_disp = 0;
			lcd_clr();
			printf("Set LCD Light");
			ShowBarG(0xff);
			light = cfg.lcd_light;
			ShowBarG(light);
			menuType = SET_LIGHT_MENU;
			break;
		case LEFT:
		case DOWN:
			if (light)
				light --;
			ShowBarG(light);
			LCD_Light(light);
			break;
		case UP:
		case RIGHT:
			if (light < 20)
				light ++;	
			ShowBarG(light);
			LCD_Light(light);
			break;
		case ENTER:
			cfg.lcd_light = light;
			save_config();
			menuType = INACTIVE_MENU;
			break;
		case BCK:
			LCD_Light(cfg.lcd_light);
			menuType = INACTIVE_MENU;
			break;
		default:
			break;				
	}
}*/
//-----------------------------------
/*void SetLight()
{
	SetLightKey(BARG_SET_INIT);
}*/
//-----------------------------------
/*void infoKey(char ch)
{
	if (!menu_timer || ch)
	{
		lcd_clr();
		menuType = INACTIVE_MENU;
	}
}*/
//-----------------------------------
void menu(char ch) 
{
	switch (menuType)
	{
		case INT_SET_MENU:
			IntSetKey(ch);
			break;
/*		case INFO_MENU:
			infoKey(ch);
			break;
		case SET_CONTR_MENU:
			SetContrKey(ch);
			break;
		case SET_LIGHT_MENU:
			SetLightKey(ch);
*/			break;
		case INACTIVE_MENU:
			//lcd_clr();
			//printf("---");
			//default_screen();
			//menuType = DEFAULT_SCREEN;
			break;

		default:
			break;			
	}
}
