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

#include "displayScreens/ButtonsImages.h"

#include <string.h>				// praca so stringom
/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "task.h"
#include "timers.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/MesageWindow.h"
#include "displayScreens/MenuScreen.h"
#include "displayScreens/SMscreen.h"
#include "displayScreens/LockKeyScreen.h"
#include "displayScreens/LogoScreen.h"


#define NUM_BUTTONS		12
#define PASSWORD		1543

extern tContext sContext;

// ****************** SCREEN pre LockKeyScreen ***************************************
extern	tCanvasWidget		g_sLockKey;
extern	tCanvasWidget		g_sNumPad;
extern	tPushButtonWidget	g_sUnlock;

extern	tImageButtonWidget	g_psNumPadButtons[];

unsigned char ActualNumpadButtonsDist[NUM_BUTTONS];
unsigned long EnteredPassword;							// prave zadavane heslo

Canvas(g_sLockKey, WIDGET_ROOT, 0, &g_sNumPad, &g_sKitronix320x240x16_SSD2119, 0, 0,
		320, 240, (/*CANVAS_STYLE_APP_DRAWN | */CANVAS_STYLE_FILL),
		ClrBlack, 0, 0, 0, 0, 0, 0/* OnMenuPaint*/);
Canvas(g_sNumPad, &g_sLockKey, 0, g_psNumPadButtons, &g_sKitronix320x240x16_SSD2119, 0, 0,
		320, 240, 0/*(CANVAS_STYLE_APP_DRAWN | CANVAS_STYLE_FILL)*/,
		ClrBlack, 0, 0, 0, 0, 0, 0/* OnMenuPaint*/);
	
#define XOFSETNUMPAD	60//110
	
tImageButtonWidget g_psNumPadButtons[] =
{
    ImageButtonStruct(&g_sNumPad, (g_psNumPadButtons + 1), 0,
                      &g_sKitronix320x240x16_SSD2119, XOFSETNUMPAD, 5, 60, 50,
                      (IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrSilver, 0, 0,
                      &g_sFontCmss28b, "1", g_pucButtonBlue60x50, g_pucButtonBlue60x50Press, 0, 0, 0,
                      0, 0, OnNumPadButtonsPress),	
    ImageButtonStruct(&g_sNumPad, g_psNumPadButtons + 2, 0,
                      &g_sKitronix320x240x16_SSD2119, XOFSETNUMPAD + 70, 5, 60, 50,
                      (IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrSilver, 0, 0,
                      &g_sFontCmss28b, "2", g_pucButtonBlue60x50, g_pucButtonBlue60x50Press, 0, 0, 0,
                      0, 0, OnNumPadButtonsPress),
    ImageButtonStruct(&g_sNumPad, g_psNumPadButtons + 3, 0,
                      &g_sKitronix320x240x16_SSD2119, XOFSETNUMPAD + 140, 5, 60, 50,
                      (IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrSilver, 0, 0,
                      &g_sFontCmss28b, "3", g_pucButtonBlue60x50, g_pucButtonBlue60x50Press, 0, 0, 0,
                      0, 0, OnNumPadButtonsPress),	
                      
    ImageButtonStruct(&g_sNumPad, g_psNumPadButtons + 4, 0,
                      &g_sKitronix320x240x16_SSD2119, XOFSETNUMPAD, 65, 60, 50,
                      (IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrSilver, 0, 0,
                      &g_sFontCmss28b, "4", g_pucButtonBlue60x50, g_pucButtonBlue60x50Press, 0, 0, 0,
                      0, 0, OnNumPadButtonsPress),	
    ImageButtonStruct(&g_sNumPad, g_psNumPadButtons + 5, 0,
                      &g_sKitronix320x240x16_SSD2119, XOFSETNUMPAD + 70, 65, 60, 50,
                      (IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrSilver, 0, 0,
                      &g_sFontCmss28b, "5", g_pucButtonBlue60x50, g_pucButtonBlue60x50Press, 0, 0, 0,
                      0, 0, OnNumPadButtonsPress),
    ImageButtonStruct(&g_sNumPad, g_psNumPadButtons + 6, 0,
                      &g_sKitronix320x240x16_SSD2119, XOFSETNUMPAD + 140, 65, 60, 50,
                      (IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrSilver, 0, 0,
                      &g_sFontCmss28b, "6", g_pucButtonBlue60x50, g_pucButtonBlue60x50Press, 0, 0, 0,
                      0, 0, OnNumPadButtonsPress),
                      
    ImageButtonStruct(&g_sNumPad, g_psNumPadButtons + 7, 0,
                      &g_sKitronix320x240x16_SSD2119, XOFSETNUMPAD, 125, 60, 50,
                      (IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrSilver, 0, 0,
                      &g_sFontCmss28b, "7", g_pucButtonBlue60x50, g_pucButtonBlue60x50Press, 0, 0, 0,
                      0, 0, OnNumPadButtonsPress),	
    ImageButtonStruct(&g_sNumPad, g_psNumPadButtons + 8, 0,
                      &g_sKitronix320x240x16_SSD2119, XOFSETNUMPAD + 70, 125, 60, 50,
                      (IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrSilver, 0, 0,
                      &g_sFontCmss28b, "8", g_pucButtonBlue60x50, g_pucButtonBlue60x50Press, 0, 0, 0,
                      0, 0, OnNumPadButtonsPress),
    ImageButtonStruct(&g_sNumPad, g_psNumPadButtons + 9, 0,
                      &g_sKitronix320x240x16_SSD2119, XOFSETNUMPAD + 140, 125, 60, 50,
                      (IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrSilver, 0, 0,
                      &g_sFontCmss28b, "9", g_pucButtonBlue60x50, g_pucButtonBlue60x50Press, 0, 0, 0,
                      0, 0, OnNumPadButtonsPress),
                      
    ImageButtonStruct(&g_sNumPad, g_psNumPadButtons + 10, 0,
                      &g_sKitronix320x240x16_SSD2119, XOFSETNUMPAD, 185, 60, 50,
                      (IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrSilver, 0, 0,
                      &g_sFontCmss28b, "*", g_pucButtonBlue60x50, g_pucButtonBlue60x50Press, 0, 0, 0,
                      0, 0, OnNumPadButtonsPress),	
    ImageButtonStruct(&g_sNumPad, g_psNumPadButtons + 11, 0,
                      &g_sKitronix320x240x16_SSD2119, XOFSETNUMPAD + 70, 185, 60, 50,
                      (IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrSilver, 0, 0,
                      &g_sFontCmss28b, "0", g_pucButtonBlue60x50, g_pucButtonBlue60x50Press, 0, 0, 0,
                      0, 0, OnNumPadButtonsPress),
    ImageButtonStruct(&g_sNumPad, 0, 0,
                      &g_sKitronix320x240x16_SSD2119, XOFSETNUMPAD + 140, 185, 60, 50,
                      (IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrSilver, 0, 0,
                      &g_sFontCmss28b, "#", g_pucButtonBlue60x50, g_pucButtonBlue60x50Press, 0, 0, 0,
                      0, 0, OnNumPadButtonsPress)
};

const char * const NumPadButtonslabels[NUM_BUTTONS] =
{
    "0",
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "*",
    "#"
};

void SetLockKeyScreen(void)
{
	unsigned char i;
	EnteredPassword= 0;
	RandomizeNumPadButtons();
	for(i=0; i<NUM_BUTTONS; i++ )
	{
		ImageButtonTextSet((g_psNumPadButtons + i), NumPadButtonslabels[ActualNumpadButtonsDist[i]]);
	}
	WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sLockKey);	     
}
void RemoveLockKeyScreen(void)
{
	WidgetRemove((tWidget *)&g_sLockKey );
}
   
void OnNumPadButtonsPress(tWidget *pWidget)
{
	unsigned char i;
	for(i=0; i<NUM_BUTTONS; i++ )
    {
        if(pWidget == (tWidget *)(g_psNumPadButtons + i))
        {
            break;
        }
    }	
    if(i != NUM_BUTTONS)
    {
    	if( ActualNumpadButtonsDist[i] == 10 )		// bola stracena *
    	{
    		//SoundPlayPassWrong();
			SoundPlayKeyClick();
    	}
    	else if( ActualNumpadButtonsDist[i] == 11 )	// bola stracena #
    	{
    		if(EnteredPassword == PASSWORD)			// bolo zadane spravne heslo 
    		{
    			SetTimerLogoScreenT2();
    			ResetLogoScreenTimer();
	 			RemoveLockKeyScreen(); 
				SetMenuScreen();
				WidgetPaint(WIDGET_ROOT);   		
				EnteredPassword= 0;	
				SoundPlayPassOK();
    		}
    		else SoundPlayPassWrong();
    	}
    	else										// bolo stlacene cislo
    	{
    		EnteredPassword= EnteredPassword * 10;
    		EnteredPassword= EnteredPassword + ActualNumpadButtonsDist[i];
    		EnteredPassword= EnteredPassword % 10000;
    		SoundPlayKeyClick();
    	}
    }
   // ResetLogoScreenTimer();
}   

unsigned long g_ulTimeCount;
unsigned long g_ulTime = 1204034400;

void UpdateRandomTime(void)
{
    g_ulTimeCount += 1000 / 1000;
    if(g_ulTimeCount >= 1000)
    {
        g_ulTime++;
        g_ulTimeCount -= 1000;
    }	
}

void RandomizeNumPadButtons(void)
{
	unsigned long ulIdx, ulTemp, ulRand;
	RandomAddEntropy((g_ulTime * 1000) + g_ulTimeCount);
	RandomSeed();
    for(ulIdx = 0; ulIdx < NUM_BUTTONS; ulIdx++)
    {
        ActualNumpadButtonsDist[ulIdx] = ulIdx;
    }
    for(ulIdx = 0; ulIdx < (NUM_BUTTONS - 1); ulIdx++)
    {
        ulRand = (((RandomNumber() >> 16) * (11 - ulIdx)) >> 16) + 1;

        ulTemp = ActualNumpadButtonsDist[ulIdx];
        ActualNumpadButtonsDist[ulIdx] = ActualNumpadButtonsDist[ulIdx + ulRand];
        ActualNumpadButtonsDist[ulIdx + ulRand] = ulTemp;
    }	
}
