#include "mybool.h"
#include "packet.h"

#include <string.h>				// praca so stringom

/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "timers.h"
#include "task.h"

// vsetko pre pracu s displejom
#include "grlib/grlib.h"
#include "grlib/widget.h"
#include "grlib/canvas.h"
#include "grlib/pushbutton.h"
#include "grlib/imgbutton.h"
#include "grlib/container.h"
#include "Drivers/kitronix320x240x16_ssd2119_idm_sbc.h"		// driver pre display

#include "MySound.h"

#include "displayScreens/ButtonsImages.h"
#include "displayScreens/MesageWindow.h"
#include "displayScreens/MenuScreen.h"
#include "displayScreens/SMscreen.h"
#include "displayScreens/LEDscreen.h"
#include "displayScreens/LockKeyScreen.h"
#include "displayScreens/LogoScreen.h"
#include "MainTask.h"

extern tContext sContext;

bool SPModulOnline;
bool LEDModulOnline;

t_cons_buff		ConsBuff[LENGHT_CONS_BUFF];
unsigned char	ConsIndex= 0;//LENGHT_CONS_BUFF;
bool 	ScreenMenuActiv;


// ****************** SCREEN pre MENU ***************************************
extern	tCanvasWidget		g_sMenu;
extern	tContainerWidget	g_sMenuConsole;
extern	tCanvasWidget		g_sConsole;
extern	tImageButtonWidget	g_sMenuSPmodul;
extern	tImageButtonWidget	g_sMenuLEDmodul;
extern	tImageButtonWidget	g_sMenuButtOPEN;
extern	tCanvasWidget		g_sMenuIndicstorOPEN;
extern	tImageButtonWidget	g_sMenuButtLOCK;
extern	tContainerWidget	g_sMenuConsole;

Canvas(g_sMenu, WIDGET_ROOT, 0, &g_sMenuConsole, &g_sKitronix320x240x16_SSD2119, 0, 0,
		320, 240, (CANVAS_STYLE_APP_DRAWN | CANVAS_STYLE_FILL),
		ClrBlack, 0, 0, 0, 0, 0, OnMenuPaint);
		
Container(g_sMenuConsole, &g_sMenu, &g_sMenuSPmodul, &g_sConsole, &g_sKitronix320x240x16_SSD2119,
			0, 0, 180, 240, (CTR_STYLE_OUTLINE | CTR_STYLE_FILL | CTR_STYLE_TEXT), ClrMidnightBlue, ClrLime, ClrLime,/*ClrGray, ClrSilver,*/
			&g_sFontCmss22, "Console");
Canvas(g_sConsole, &g_sMenuConsole, 0, 0, &g_sKitronix320x240x16_SSD2119, 0, 0,
		320, 240, CANVAS_STYLE_APP_DRAWN,
		ClrBlack, 0, 0, 0, 0, 0, OnConsolePaint);	

ImageButton(g_sMenuSPmodul, &g_sMenu, &g_sMenuLEDmodul, 0,
			&g_sKitronix320x240x16_SSD2119, 190, 10, 120, 40,
			(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
			&g_sFontCmss20b, "SP modul", g_pucButtonBlue120x40, g_pucButtonBlue120x40Press, 0, 0, 0,
			0, 0, OnSpinModulPress);

ImageButton(g_sMenuLEDmodul, &g_sMenu, &g_sMenuButtOPEN, 0,
			&g_sKitronix320x240x16_SSD2119, 190, 70, 120, 40,
			(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
			&g_sFontCmss20b, "LED modul", g_pucButtonBlue120x40, g_pucButtonBlue120x40Press, 0, 0, 0,
			0, 0, OnLedModulPress);

ImageButton(g_sMenuButtOPEN, &g_sMenu, &g_sMenuIndicstorOPEN, 0,
			&g_sKitronix320x240x16_SSD2119, 220, 130, 90, 40,
			(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
			&g_sFontCmss20b, "Door", g_pucButtonBlue90x40, g_pucButtonBlue90x40Press, 0, 0, 0,
			0, 0, OnDeckPress);
                
Canvas(g_sMenuIndicstorOPEN, &g_sMenu, &g_sMenuButtLOCK, 0, &g_sKitronix320x240x16_SSD2119,
					195, 140, 20, 20,  CANVAS_STYLE_IMG, ClrBlue, ClrWhite, 0, 0, 0, g_pucLightOff, 0);

ImageButton(g_sMenuButtLOCK, &g_sMenu, 0, 0,
			&g_sKitronix320x240x16_SSD2119, 190, 190, 120, 40,
			(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
			&g_sFontCmss20b, "Lock", g_pucButtonBlue120x40, g_pucButtonBlue120x40Press, 0, 0, 0,
			0, 0, OnLockPress);

void OnMenuPaint(tWidget *pWidget, tContext *pContext)
{
	ScreenMenuActiv= true;
	CanvasImageSet( (tCanvasWidget *)&g_sMenuIndicstorOPEN, StateSM_Deck() ? g_pucLightOn : g_pucLightOff );
	GrContextForegroundSet(&sContext, ClrGreen);
	GrLineDraw(&sContext,160, 0, 160, 240);
	
	t_MainQueueStruct	Mesage;
	Mesage.Mesage_ID= C_QUEUE_READ_ACT_SETT_SM;	// ziadam o aktualne nastavenia modulov
	sendToMainQueue( &Mesage, 0 );
	Mesage.Mesage_ID= C_QUEUE_READ_ACT_SETT_LEDM;	// ziadam o aktualne nastavenia modulov
	sendToMainQueue( &Mesage, 0 );
}

void OnConsolePaint(tWidget *pWidget, tContext *pContext)
{
#define TEXT_SPACE	16
#define XCONSOLE_OFFSET		10
#define YCONSOLE_OFFSET		25
#define YTEXT_SIZE			16
#define XTEXT_MAXLENGHT		180 - 5

	tRectangle sRect;
	long i, temp;
	GrContextFontSet(pContext, &g_sFontCmss16);
 	GrContextBackgroundSet(pContext, ClrMidnightBlue);
 	   
	sRect.sXMax = XTEXT_MAXLENGHT	;   
	for( i= 0 ; i < LENGHT_CONS_BUFF ; i++ )
	{
		// mazanie zbitkov textov
	GrContextForegroundSet(pContext, ClrMidnightBlue/*ClrGreen*/);

	temp= GrStringWidthGet(pContext, ConsBuff[ (ConsIndex + 1 + i) % LENGHT_CONS_BUFF ].ConsMes, strlen(ConsBuff[ (ConsIndex + 1 + i) % LENGHT_CONS_BUFF ].ConsMes) );	
	sRect.sXMin = XCONSOLE_OFFSET + temp;
	sRect.sYMin = YCONSOLE_OFFSET + (i*TEXT_SPACE);
	sRect.sYMax = YCONSOLE_OFFSET + (i*TEXT_SPACE) + YTEXT_SIZE; 	
	GrRectFill(pContext, &sRect);  
		// vykreslenie noveho textu
    GrContextForegroundSet(pContext, ConsBuff[ (ConsIndex + 1 + i) % LENGHT_CONS_BUFF ].color);	
    GrStringDraw(pContext, ConsBuff[ (ConsIndex + 1 + i) % LENGHT_CONS_BUFF ].ConsMes, -1, XCONSOLE_OFFSET, (YCONSOLE_OFFSET + (i*TEXT_SPACE)), true);	
	}	
}
void OnSpinModulPress(tWidget *pWidget)
{
//	if( SPModulOnline )
//	{
		ScreenMenuActiv= false;
		RemoveMenuScreen();
		SetMenuSpinModulScrean();
		WidgetPaint(WIDGET_ROOT);
		SoundPlayKeyClick();
//	}
//	else SoundPlayFault();
	ResetLogoScreenTimer();
}		
void OnLedModulPress(tWidget *pWidget)
{
//	if( LEDModulOnline )
//	{
		ScreenMenuActiv= false;
		RemoveMenuScreen();
		SetMenuLedModulScrean();
		WidgetPaint(WIDGET_ROOT);
		SoundPlayKeyClick();
//	}
//	else SoundPlayFault();
	ResetLogoScreenTimer();
}

void OnDeckPress(tWidget *pWidget)
{
	if( SPModulOnline )
	{
		CanvasImageSet( (tCanvasWidget *)&g_sMenuIndicstorOPEN, UpdateSM_Deck() ? g_pucLightOn : g_pucLightOff );           
		WidgetPaint( (tWidget *)&g_sMenuIndicstorOPEN );
		SoundPlayKeyClick();
	}
	else SoundPlayFault();
	ResetLogoScreenTimer();
}

void OnLockPress(tWidget *pWidget)
{
	ScreenMenuActiv= false;
	RemoveMenuScreen();
	SetLogoScreen();
	WidgetPaint(WIDGET_ROOT);
	SoundPlayKeyClick();
}

void SetMenuScreen(void)
{
	//ScreenMenuActiv= true;		// nastavuje sa az po vykresleni celeho screnu
	WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sMenu);
	CanvasImageSet( (tCanvasWidget *)&g_sMenuIndicstorOPEN, StateSM_Deck() ? g_pucLightOn : g_pucLightOff );
	
	if( IsSPModulonline() > 0)
	{
		SPModulOnline= true;
		ImageButtonImageSet(&g_sMenuSPmodul, g_pucButtonBlue120x40);
		ImageButtonImagePressedSet(&g_sMenuSPmodul, g_pucButtonBlue120x40Press);
		ImageButtonImageSet(&g_sMenuButtOPEN, g_pucButtonBlue90x40);
		ImageButtonImagePressedSet(&g_sMenuButtOPEN, g_pucButtonBlue90x40Press);
	}
	else
	{
		SPModulOnline= false;
		ImageButtonImageSet(&g_sMenuSPmodul, g_pucButtonBlue120x40Inactive);
		ImageButtonImagePressedSet(&g_sMenuSPmodul, g_pucButtonBlue120x40ErorPress);
		ImageButtonImageSet(&g_sMenuButtOPEN, g_pucButtonBlue90x40Inactive);
		ImageButtonImagePressedSet(&g_sMenuButtOPEN, g_pucButtonBlue90x40ErorPress);
	}
	if( IsLEDModulonline() > 0)
	{
		LEDModulOnline= true;
		ImageButtonImageSet(&g_sMenuLEDmodul, g_pucButtonBlue120x40);
		ImageButtonImagePressedSet(&g_sMenuLEDmodul, g_pucButtonBlue120x40Press);
	}
	else
	{
		LEDModulOnline= false;
		ImageButtonImageSet(&g_sMenuLEDmodul, g_pucButtonBlue120x40Inactive);
		ImageButtonImagePressedSet(&g_sMenuLEDmodul, g_pucButtonBlue120x40ErorPress);
	}
}

void RemoveMenuScreen(void)
{
	ScreenMenuActiv= false;
	WidgetRemove((tWidget *)&g_sMenu );
}

unsigned long IsMenuScreenSet( void )
{
	if( ScreenMenuActiv == true ) return 1;
	else return 0;
}

void OnlineStavChanged(void)
{
		if( (SPModulOnline == false) && IsSPModulonline())
		{
			SPModulOnline= true;
			ImageButtonImageSet(&g_sMenuSPmodul, g_pucButtonBlue120x40);
			ImageButtonImagePressedSet(&g_sMenuSPmodul, g_pucButtonBlue120x40Press);
			ImageButtonImageSet(&g_sMenuButtOPEN, g_pucButtonBlue90x40);
			ImageButtonImagePressedSet(&g_sMenuButtOPEN, g_pucButtonBlue90x40Press);
			if( ScreenMenuActiv == true )
			{
				WidgetPaint((tWidget *)&g_sMenuSPmodul);
				WidgetPaint((tWidget *)&g_sMenuButtOPEN);
			}
		}
		if( (SPModulOnline == true) && (IsSPModulonline()== 0) )
		{
			SPModulOnline= false;
			ImageButtonImageSet(&g_sMenuSPmodul, g_pucButtonBlue120x40Inactive);
			ImageButtonImagePressedSet(&g_sMenuSPmodul, g_pucButtonBlue120x40ErorPress);
			ImageButtonImageSet(&g_sMenuButtOPEN, g_pucButtonBlue90x40Inactive);
			ImageButtonImagePressedSet(&g_sMenuButtOPEN, g_pucButtonBlue90x40ErorPress);
			if( ScreenMenuActiv == true )
			{
				WidgetPaint((tWidget *)&g_sMenuSPmodul);
				WidgetPaint((tWidget *)&g_sMenuButtOPEN);
			}
		}		
		if( (LEDModulOnline == false) && IsLEDModulonline())
		{
			LEDModulOnline= true;
			ImageButtonImageSet(&g_sMenuLEDmodul, g_pucButtonBlue120x40);
			ImageButtonImagePressedSet(&g_sMenuLEDmodul, g_pucButtonBlue120x40Press);
			if( ScreenMenuActiv == true )WidgetPaint((tWidget *)&g_sMenuLEDmodul);
		}
		if( (LEDModulOnline == true) && (IsLEDModulonline() == 0) )
		{
			LEDModulOnline= false;
			ImageButtonImageSet(&g_sMenuLEDmodul, g_pucButtonBlue120x40Inactive);
			ImageButtonImagePressedSet(&g_sMenuLEDmodul, g_pucButtonBlue120x40ErorPress);
			if( ScreenMenuActiv == true )WidgetPaint((tWidget *)&g_sMenuLEDmodul);
		}
}

void NewMasageToConsole( const char *mesage,  unsigned char typemesage )		// funkcia nieje reentrantna ale je osetrene pripadne skomolenie spravy
{												// v pripade sucasneho volania moze sa na displey objavyt nekompletna sprava ktora sa nasledne opravy
												// pri dokonceny vikonavania prerusenej funkcie
	unsigned char indexTemp;
	taskENTER_CRITICAL();			//kriticka cast
	ConsIndex++;
	ConsIndex= ConsIndex % LENGHT_CONS_BUFF;
	indexTemp= ConsIndex;
	taskEXIT_CRITICAL();			// konec kritickej sekcie
	
	switch( typemesage ){
		case 0:			// odoslana sprava
			ConsBuff[ indexTemp % LENGHT_CONS_BUFF ].color= ClrGold;//ClrWhite;
			break;	
		case 1:			// priata sprava (odpoved na RQS)
			ConsBuff[ indexTemp % LENGHT_CONS_BUFF ].color= ClrLime;
			break;	
		case 2:			// chybova sprava
			ConsBuff[ indexTemp % LENGHT_CONS_BUFF ].color= ClrRed;
			break;	
		case 3:			// debaag
			ConsBuff[ indexTemp % LENGHT_CONS_BUFF ].color= ClrYellow;
			break;	
		default:		
			ConsBuff[ indexTemp % LENGHT_CONS_BUFF ].color= ClrWhite;
			break;			
	}

	strncpy(ConsBuff[ indexTemp % LENGHT_CONS_BUFF ].ConsMes, mesage, LENGHT_CONS_MES);
	if(ScreenMenuActiv) WidgetPaint((tWidget *)&g_sConsole);
}


