/****************************************************************************
	Aplikace Klavesnice pro zadavani textovych povelu zarizeni a ovladani
	vzdaleneho Pocitace.

	TODO:	v pripade pouziti masky by bylo vhodne vlozit misto masky nejaky
			defaultni text, aby to nemusel uzivatel cele vyplnovat.

*****************************************************************************/

#ifndef KEYBOARD_H
#define KEYBOARD_H
#include "OS_types.h"
#include "GLCD_types.h"
#include "GR_object.h"

/****************************************************************************
	Definice stavu klavesnice

*****************************************************************************/
typedef enum KEYBOARD_STATE
{
	KEYBOARD_NOT_INITIALIZED = M_TASK_NOT_INITIALIZED,
	KEYBOARD_INITIALIZE,
	KEYBOARD_RESTART = M_TASK_RESTART,
	KEYBOARD_DRAW,
	KEYBOARD_ACTIVE = M_TASK_RUNNING,
	KEYBOARD_SWITCH,
	KEYBOARD_READY = M_TASK_READY,
	KEYBOARD_STOP,
	KEYBOARD_ERROR = M_TASK_FAILED,
}KEYBOARD_STATE;

/****************************************************************************
	Nezbytne definice vseho mozneho

*****************************************************************************/
#define KEYBOARD_TEXT_BOX_WIDTH 440
#define KEYBOARD_TEXT_BOX_HEIGHT 65

#define MAX_ROLL_COUNT 20					//maximalni mozny pocet obrazovek

#define BACKSPACE 171
#define LEFT '<'
#define RIGHT '>'

#define KEYBOARD_YA 21
#define KEYBOARD_XA 0
#define KEYBOARD_XC 480
#define KEYBOARD_YC 272

/****************************************************************************
	Dostupne rezimy klavesnice

*****************************************************************************/
typedef enum KEYBOARD_TYPES
{
	KEYBOARD_NORMAL,
	KEYBOARD_CAPITAL,
	KEYBOARD_NUMERIC,
	KEYBOARD_SYMBOLIC,
	KEYBOARD_NUMERIC_ONLY,
}KEYBOARD_TYPES;

/****************************************************************************
	Geometricke rozmery aplikace

*****************************************************************************/
#define KEY_SIZE 30
#define KEY_DISTANCE 45


#define ROW_LEFT_UPPER 20
#define ROW_LEFT_MIDDLE 42
#define ROW_LEFT_LOWER 20
#define ROW_LEFT_SPECIAL 20

#define KEY_SPECIAL_SIZE 80
#define KEY_SPECIAL_DISTANCE 90



#define LAYOUT_UPPER_COUNT 10
#define LAYOUT_MIDDLE_COUNT 9
#define LAYOUT_LOWER_COUNT 10
#define LAYOUT_SPECIAL_COUNT 5

/****************************************************************************
	Aplikace Klavesnice pro zadavani textovych povelu zarizeni a ovladani
	vzdaleneho Pocitace.

	TODO:	v pripade pouziti masky by bylo vhodne vlozit misto masky nejaky
			defaultni text, aby to nemusel uzivatel cele vyplnovat.

*****************************************************************************/
typedef enum ROWS
{
	ROW_UPPER,
	ROW_MIDDLE,
	ROW_LOWER,
	ROW_SPECIAL,
}ROWS;

/****************************************************************************
	Rady klaves - pozice odzhora

*****************************************************************************/
typedef enum ROW_TOPS
{
	ROW_TOP_UPPER = 100,
	ROW_TOP_MIDDLE = ROW_TOP_UPPER+KEY_DISTANCE,
	ROW_TOP_LOWER = KEY_DISTANCE+ ROW_TOP_MIDDLE,
	ROW_TOP_SPECIAL = KEY_DISTANCE+ ROW_TOP_LOWER,
}ROW_TOPS;

/****************************************************************************
	Definice zvlastnich klaves

*****************************************************************************/
typedef enum SPECIAL_KEYS
{
	OK = 1,
	SPACE,
	SHIFT,
	ENTER,
	CANCEL,
}SPECIAL_KEYS;

/****************************************************************************
	Definice klaves

*****************************************************************************/
typedef enum KEYBOARD_LAYOUT
{
	KEYBOARD_LETTERS = 0,
	KEYBOARD_TB = LAYOUT_UPPER_COUNT + LAYOUT_MIDDLE_COUNT + LAYOUT_LOWER_COUNT + LAYOUT_SPECIAL_COUNT,
	KEYBOARD_OBJECTS_COUNT,
} KEYBOARD_LAYOUT;

#define KEYBOARD_TEXT_BUFFER_LENGTH 500

void AppKeyboard(void);
RESULT KBMsgBox(MESSAGE *msg);
RESULT keyboardInitialize(void);


/****************************************************************************
	Klavesnicovy zasobnik - jednoucelova struktura pro shrnuti promennych
	nezbytnych pro spravnou funkci klavesnice.

*****************************************************************************/
typedef struct KEYBOARD_BUFFER
{
	unsigned char TextBuffer[KEYBOARD_TEXT_BUFFER_LENGTH];
	unsigned char* Mask;
	int Length;
	int Counter;
	short First;
	short FirstBuffer[MAX_ROLL_COUNT];
	short Last;
	short LastBuffer[MAX_ROLL_COUNT];
	short Index;
	int TopCounter;
	int BottomCounter;

}KEYBOARD_BUFFER;

/****************************************************************************
	Makro pro vypocet indexu klavesy v rade.

*****************************************************************************/
#define	CalculateIndex(left, distance, size)	if (pos->x-left < 0)\
				return -1;\
			index = integerDiv((pos->x-left),distance);\
			if ((pos->x-left)%distance > size)\
			{\
				DebugOut("Click between buttons..\n");\
				return -1;\
			}


#endif