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

#include <string.h>				// praca so stringom
#include "displayScreens/ButtonsImages.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

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

#include "MySound.h"
#include "displayScreens/ImageMesageWindow.h"
#include "displayScreens/MesageWindow.h"
#include "displayScreens/MenuScreen.h"
#include "displayScreens/LogoScreen.h"

xTimerHandle xTimerMesageWindowAnimation;

bool 	MesageWindowShowed;

char	MesageWindowMesage[MAX_MESAGE_LENGHT]; 		// sprava ktora sa ma zobrazit

extern tContext sContext;

// ****************** SCREEN pre MENU ***************************************
extern	tCanvasWidget		g_sMesageWindow;
extern	tImageButtonWidget	g_sCloseMesageW;


Canvas(g_sMesageWindow, WIDGET_ROOT, 0, &g_sCloseMesageW, &g_sKitronix320x240x16_SSD2119, 40, 50,
		240, 140, (CANVAS_STYLE_APP_DRAWN | CANVAS_STYLE_IMG /*CANVAS_STYLE_OUTLINE | CANVAS_STYLE_FILL*/),
		ClrBlack, ClrWhite, 0, 0, 0, g_pucMesageWindow, OnMesageWindowPaint);

ImageButton(g_sCloseMesageW, &g_sMesageWindow, 0, 0,
			&g_sKitronix320x240x16_SSD2119, 150, 140, 120, 40,
			(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
			&g_sFontCmss20b, "Close", g_pucButtonBlueGrey120x40, g_pucButtonBlueGrey120x40Press, 0, 0, 0,
			0, 0, OnCancelPress);
           
void InitMesageWindow(void)
{
	MesageWindowShowed= false;
	xTimerMesageWindowAnimation= xTimerCreate( "Timer pre animovanieMW",  ( 500 / portTICK_RATE_MS ), pdTRUE, 0, vTimerMesageWindowAnimation );
}

void vTimerMesageWindowAnimation( xTimerHandle pxTimer )
{
	static unsigned char statAnim= 0;
	statAnim++;
	if( statAnim > 4 )  statAnim= 0;
	GrContextFontSet(&sContext, &g_sFontCmss24);
	GrContextForegroundSet(&sContext, ClrWhite);
	switch(statAnim)
	{
		case 0:
			GrStringDraw(&sContext, ".        ", -1, 60, 150, true);
			break;
		case 1:
			GrStringDraw(&sContext, ". .      ", -1, 60, 150, true);
			break;
		case 2:
			GrStringDraw(&sContext, ". . .    ", -1, 60, 150, true);
			break;
		case 3:
			GrStringDraw(&sContext, ". . . .  ", -1, 60, 150, true);
			break;
		case 4:
			GrStringDraw(&sContext, ". . . . .", -1, 60, 150, true);
			break;
	}
}

void ShowMesageWindow( char *sprava, unsigned char animates )	// ak je > 0 tak animuje
{
	if( animates > 0 )xTimerStart( xTimerMesageWindowAnimation, 100 );
	memcpy( MesageWindowMesage, sprava, MAX_MESAGE_LENGHT );
	if(MesageWindowShowed == false)
	{
		WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sMesageWindow);
	}
	RemoveMenuScreen();
	WidgetPaint( (tWidget *)&g_sMesageWindow );
	MesageWindowShowed= true;
	SoundPlayPassWrong();
}        

void RemoveMesageWindow(void)
{
	xTimerStop( xTimerMesageWindowAnimation, 100 );
	WidgetRemove((tWidget *)&g_sMesageWindow );
	MesageWindowShowed= false;
}

void CloseMesageWindow( void )
{
	RemoveMesageWindow();
	SetMenuScreen();
	WidgetPaint(WIDGET_ROOT);
	ResetLogoScreenTimer();
}
   
void OnMesageWindowPaint(tWidget *pWidget, tContext *pContext)
{
	GrContextFontSet(pContext, &g_sFontCmss22b);
	GrContextBackgroundSet(pContext, ClrDimGray);
	GrContextForegroundSet(pContext, ClrBlack/*ClrWhite*/);
	GrStringDraw(pContext, MesageWindowMesage, -1, 55, 80, true);
	GrStringDraw(pContext, "         ", -1, 60, 150, true);
}

void OnCancelPress(tWidget *pWidget)
{
	CloseMesageWindow();	
	SoundPlayKeyClick();
}
