#include "RecoverAll.h"
#include "Runtime.h"


extern "C"{
	#include "user_norflash.h"
	#include "user_eeprom.h"
	#include "uart_key_drv.h"
}

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

#define dWWidth	24
#define dWHeigh	24
#define dRoundR	7

static const int xFirst	=  10;


void TRecoverAll::Init()
{
	CtlIndex = 0;
	IsShowing = 0;
	
	PermissionCmd =0;
	IsPermission =0;
}

void TRecoverAll::sDelayMs(unsigned int aMs)
{
	volatile unsigned int mTick;
	volatile unsigned int Tick;
	for(Tick=0; Tick<aMs; Tick++){
		for(mTick=0; mTick<160000; mTick++);
		sys_error_led_task();
	}
}

void TRecoverAll::RenderBackGround(void)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);
}

void TRecoverAll::RenderBackGround(unsigned int aClr)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, aClr);
}

void TRecoverAll::DrawFixText()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "高级设置->恢复出厂设置");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "恢复出厂设置");
		TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clBlue, "确定", 12, clNearWhite);
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Advanced Setup->Factory Reset");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "Factory Reset");
		TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clBlue, "Reset", 6, clNearWhite);	
	}
}

void TRecoverAll::DrawSelf(void)
{
	RenderBackGround();
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	for(int i=0; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	DrawFixText();
}

void TRecoverAll::Show(void)
{	
	RenderBackGround();
	DrawSelf();
}

void TRecoverAll::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], ContentColor);
		for(int i=0; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
		DrawFixText();
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}


//______________________________________________________________________________________________________________________________

void TRecoverAll::PermissionCmdReturnDraw()
{
	RectFillRender(0, Lv.Grid.Bottom[0] + 20, 799, Lv.Grid.Bottom[0] + 20 + 160, ContentColor);
	if(LanguageEnCn==0)	TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clBlue, "确定", 12, clNearWhite);
	else 				TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clBlue, "Reset", 6, clNearWhite);
}

void TRecoverAll::DoCover()
{
	DoRecover();
}

void TRecoverAll::PermissionCmdGO()
{
	if(PermissionCmd == 1){
		//Go Action
		if(LanguageEnCn==0)	TextRender_string24(400, Lv.Grid.Top[0]+ 36, clNearWhite, ItemColor, "正在恢复出厂设置");
		else 				TextRender_string24(400, Lv.Grid.Top[0]+ 36, clNearWhite, ItemColor, "Resetting       ");
		DoCover();
		if(LanguageEnCn==0)	TextRender_string24(400, Lv.Grid.Top[0]+ 36, clNearWhite, ItemColor, "恢复出厂设置完成");
		else 				TextRender_string24(400, Lv.Grid.Top[0]+ 36, clNearWhite, ItemColor, "Reset Completed ");
	}
}

//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TRecoverAll::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg = guiMsgNone;
	unsigned char aP0,aP1,Err;
	unsigned char x,y,aCh;
	unsigned int aAddr, HadPrc;
	TDMix aFullPath;

	if(IsPermission){
		PmsRtnMsg = PmsIntervene4Gui.KeyIn(aKey);
		if(PmsRtnMsg == guiMsgOkAndReturn){
			//Enter And Run Go
			IsPermission = 0;
			PermissionCmdReturnDraw();
			PermissionCmdGO();
		}else
		if(PmsRtnMsg == guiMsgReturn){
			//Not Enter Redraw
			IsPermission = 0;
			PermissionCmdReturnDraw();
		}
		return aMsg;
	}else{
		switch(aKey){
			case VK_EXECUTE:
				if(CtlIndex == 0){
					//Active
					PermissionCmd = 1;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						if(LanguageEnCn==0)	TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clNearWhite, "确定", 12, clNearBlack);
						else 				TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clNearWhite, "Reset", 6, clNearBlack);
						PmsIntervene4Gui.Show4Gui(100, Lv.Grid.Bottom[0] + 40, 600 , 120, 3);
					}
				}
				break;
			case VK_RETURN:
				IsShowing = 0;
				aMsg = guiMsgReturn;
				break;
		}
	}

	return aMsg;	
}

int TRecoverAll::ExtRequst(unsigned char Prm)
{
	IsShowing =0;
	return 0;
}

void TRecoverAll::Task1000Ms()
{
	
}






