/****************************************************************************
	Informacni aplikace pro zobrazeni informaci o autorovi

*****************************************************************************/
#ifdef PCSIM
	#include "stdafx.h"
#endif

#include "Applications.h"
#include "GRAPHICS.h"
#include "OS.h"
#include "ImagesAbout.h"
#include "GLCD.h"
TScreen AboutScreen;																		//Okno aplikace
TApplicationInternal AboutInternal;															//Ridici struktura aplikace

FONT AboutScreenFont = {RGB_CYAN,FONT_SMALL,RGB_LIGHT_GRAY};								//Pismo aplikace

TScreenObjectParams AboutParams = {ABOUT_XC-ABOUT_XA,ABOUT_YC-ABOUT_YA,&AboutScreenFont};	//Parametry objektu v obraze

int AboutResult = -1;																		//Vysledek ukonceni

/****************************************************************************
	Funkce vykresli jediny objekt, informacni okno

	Vstup: 
		obj:			instance objektu
		message:		parametr kresleni

	Výstup:
		neni

*****************************************************************************/
void AboutInfo(TScreenObject* obj ,OBJECT_MESSAGE message)
{
	TRect r = {18,30,462,250};												//definice obdelniku aplikace
	TRect r_roi = {27,35,462,250};											//definice ROI
	FONT f = {RGB_DARK_GREEN,FONT_SMALL,RGB_WHITE};							//Definice fontu
	LCD_frect(&r,RGB_WHITE);												//Vykresleni pozadi
	LCD_rect(&r,3,RGB_DARK_GRAY);											//Vykresleni ramecku
	LCD_Set_ROI(&r_roi);													//Nastaveni ROI
	LCD_Set_Font(&f);														//Nastaveni fontu
	LCD_Goto_XY(27,55);														//Presun na pozici a postupny vypis vsech informaci
	LCD_printf((unsigned char*)" Autor:\n");
	LCD_Set_Font_Color(RGB_RED);
	LCD_printf((unsigned char*)"  Jiří Macháček\n");
	LCD_Goto_XY(27,95);
	LCD_Set_Font_Color(RGB_DARK_GREEN);
	LCD_printf((unsigned char*)" ID:\n");
	LCD_Set_Font_Color(RGB_RED);
	LCD_printf((unsigned char*)"  106 608\n\n");
	LCD_Goto_XY(27,135);
	LCD_Set_Font_Color(RGB_DARK_GREEN);
	LCD_printf((unsigned char*)" Obor:\n");
	LCD_Set_Font_Color(RGB_RED);
	LCD_printf((unsigned char*)"  M-KAM\n\n\n");
	LCD_Set_Font_Color(RGB_DARK_GREEN);
	LCD_printf((unsigned char*)"                      FEKT VUT Brno");
	LCD_draw_bitmap(300,55,BMP_LogoFekt);
	LCD_draw_bitmap(30,190,BMP_Logo);
}

/****************************************************************************
	Kliknuti vyvola ukonceni aplikace

	Vstup: 
		obj:			instance objektu
		message:		parametr kresleni

	Výstup:
		neni

*****************************************************************************/
void AboutCallback(TScreenObject* obj ,OBJECT_MESSAGE message)
{
	AboutInternal.State = ABOUT_STOP;
}

/****************************************************************************
	Postovni schranka aplikace

	Vstup: 
		msg:		Zprava aplikaci

	Výstup:
		neni

*****************************************************************************/
RESULT AMsgBox(MESSAGE * msg)
{
	POSITION* tmpPosition = 0;
	int tmp = 0;
	switch (msg->msg)
	{
	case RUN:
		DebugOut("About RUN command received.\n");
		if (AboutInternal.State == ABOUT_READY)								//Pokud je aplikace inicializovana
		{
			AboutResult = -1;												
			AboutInternal.State = ABOUT_RESTART;							//Priprav ji na spusteni
			return PASSED;
		}
		DebugOut("About could not be run.\n");
		return FAILED;
			
		break;
	case INIT:
		if (AboutInternal.State == ABOUT_NOT_INITIALIZED)					//Pokud neni inicializovana
		{
			DebugOut("About INIT command received.\n");
			AboutInternal.State = ABOUT_INITIALIZE;							//Priprav inicializaci
			return PASSED;
		}
		break;
	case GET_STATE:
		return (RESULT)AboutInternal.State;									//Vraci stav aplikace
		break;
	case GET_RESULT:
		return (RESULT)AboutResult;											//Vraci vysledek
		break;
	case APP_CLICK:
		AboutInternal.ClickPosition.isFresh = YES;							//Prijato kliknuti od OS
		tmpPosition = (POSITION*)msg->data;									//Ulozeni dat ke zpracovani
		AboutInternal.ClickPosition.x = tmpPosition->x;
		AboutInternal.ClickPosition.y = tmpPosition->y;
		DebugOut("About click received.\n");
		return PASSED;
		break;
	default:
		break;
		
	}
	return FAILED;
}


/****************************************************************************
	Inicializace aplikace

	Vstup: 
		neni

	Výstup:
		RESULT:			Vysledek operace

*****************************************************************************/
RESULT aboutInitialize(void)
{
	AboutScreen.Init(ABOUT_OBJECT_COUNT,	&AboutScreenFont, YES);							//Inicializace okna
	AboutInternal.Init(ABOUT,ABOUT_XA,ABOUT_YA,ABOUT_XC,ABOUT_YC,&AboutScreen,0,YES);		//Inicializace ridici struktury

	if (AboutInternal.Screen == 0)
	{
		return FAILED;
	}

	/****************************************************************************
		Inicializace grafickych objektu

	*****************************************************************************/
	AboutInternal.Screen->objects[ABOUT_INFO].Init(	ABOUT_XA,
													ABOUT_YA,
													BUTTON,
													ABOUT_INFO,
													STR_ABOUT,
													OBJECT_ENABLED,
													&AboutParams,
													&AboutCallback,
													&AboutInfo);

	return PASSED;
}

/****************************************************************************
	Ridici stavovy automat aplikace

	Vstup: 
		neni

	Výstup:
		neni

*****************************************************************************/
void AppAbout(void)
{
	switch (AboutInternal.State)
	{
	case ABOUT_NOT_INITIALIZED:
		break;
	case ABOUT_INITIALIZE:
		AboutInternal.State = ABOUT_READY;									//Prechod do dalsiho stavu
		if (aboutInitialize() !=PASSED)										//Inicializace aplikace
		{
			DebugOut("About could not be initialized. Falling to error.\n");
			AboutInternal.State = ABOUT_ERROR;								//Pri neuspechu selzi
			break;
		}
		DebugOut("About initialized.\n");
		break;
	case ABOUT_RESTART:
		AboutInternal.Startup();											//Startovaci procedury

		AboutInternal.State = ABOUT_DRAW;									//Prechod do dalsiho stavu
		break;
	case ABOUT_DRAW:
		AboutInternal.Screen->Draw();										//Vykresleni aplikace
		AboutInternal.State = ABOUT_ACTIVE;									//Prechod do aktivniho stavu
		break;
	case ABOUT_ACTIVE:
		AboutInternal.ProceedClick();										//Zpracuj kliknuti
		break;
	case ABOUT_WAITING:
		switch(AboutInternal.TryToResume())									//Navrat z aplikace
		{
		case KEYBOARD:
			break;
		case NONE:
			break;
		default:
			DebugOut("Returned to About from unknown application.\n");
			break;
		}
		break;
	case ABOUT_READY:
		break;
	case ABOUT_STOP:
		AboutInternal.Stop();												//Ukoncovaci procedury
		break;
	case ABOUT_ERROR:
		break;
	default:
		break;
	}
}