#include "ClearRegistedInf.h"
#include "Runtime.h"


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

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

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

static const int xFirst	=  10;
class TItemList	*TClearRegistedInf::sIList;

void TClearRegistedInf::Init()
{
	CtlIndex = 0;
	IsShowing = 0;
	
	PermissionCmd =0;
	IsPermission =0;
	
	Eraser.OnGo = 0;
	Eraser.Step = 0xFF;
	Eraser.NotDone = 0;
}

void TClearRegistedInf::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 TClearRegistedInf::RenderBackGround(void)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);
}

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

void TClearRegistedInf::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->Clear Registed Data");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "Registed Data");
		TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clBlue, "Clear", 6, clNearWhite);
	}
}

void TClearRegistedInf::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 TClearRegistedInf::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	IsShowing = 1;
}

void TClearRegistedInf::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 TClearRegistedInf::PermissionCmdReturnDraw()
{
	RectFillRender(0, Lv.Grid.Bottom[0] + 20, 799, Lv.Grid.Bottom[0] + 20 + 160, ContentColor);
	DrawFixText();
}

void TClearRegistedInf::sDoClear()
{
	volatile unsigned  int aFlashState, i, m, aWait, aAddr, fAddr;
	TDMix tDm;
	
	if(sIList == nullptr)return;
	sIList->ReadWriteData.Reset();
	
	do{
		sDelayMs(2);
		aFlashState = nor_get_busy_state(0);
	}while(aFlashState);
	
	for(i=0; i<8; i++){
		SysLoopTick = 0;
		fAddr = dFlashRegisterDataAddr + (0x000010000 * i);
		nor_erase_sector_64k(0,fAddr);
		sDelayMs(250);
		do{
			sDelayMs(30);
			aFlashState = nor_get_busy_state(0);
		}while(aFlashState);
		LoopCheck1S();
	}

	for(i=0; i<dPORT_MAX_COUNT; i++){
		SysLoopTick = 0;
		LoopCheck1S();
		//write ExistTable 
		for(m=0; m<dEP_MAX_COUNT_PER_PORT; m++){
			Port[i].ExistTable[m] = 0;
			Port[i].dTypeTable[m] = 0;
		}
		//write DisplaySetting
		for(m=0;m<dDISPLAYPAD_MAX_COUNT; m++){
			for(int x=0; x<16;x++)Port[i].DisplayPadSet[m].MemBytes[x] =0;
		}
		TRegister::sIList = sIList;
		TRegister::sWriteOnePort(i+1);
	}
	
	
	SysLoopTick = 0;
	for(m=0; m<dPORT_MAX_COUNT; m++){
		MainCtl.fData.Split.ExistTablePort[m] =0;
	}
	MainCtl.fData.Split.PortCount = 0;
	TRegister::sIList = &ItemList;
	TRegister::sWritePortList();
	
	SysLoopTick = 0;
	do{
		sDelayMs(2);
		aFlashState = nor_get_busy_state(dFlashRegisterDataChip);
	}while(aFlashState);
	
	Record.ReadRegisterData();
	InnerCan.SendCmdClearRegData();
}

void TClearRegistedInf::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, "Clearing");
		Eraser.OnGo =0xB6;
		Eraser.Step = 0;
		Eraser.NotDone = 1;
		sDoClear();
		InnerCan.SendCmdClearRegData();
		Record.ReadRegisterData();
		if(LanguageEnCn==0)	TextRender_string24(400, Lv.Grid.Top[0]+ 36, clNearWhite, ItemColor, "清除完成        ");
		else 				TextRender_string24(400, Lv.Grid.Top[0]+ 36, clNearWhite, ItemColor, "Cleared        ");
		Record.OperateAdd_CleanUp(0);
		Eraser.OnGo =0;
		Eraser.NotDone =0;
	}
}

//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TClearRegistedInf::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) && (Eraser.OnGo == 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, "Clear", 6, clNearBlack);
						PmsIntervene4Gui.Show4Gui(100, Lv.Grid.Bottom[0] + 40, 600 , 120, 3);
					}
				}
				break;
			case VK_RETURN:
				IsShowing = 0;
				if(Eraser.OnGo){
					ForceExit();
				}
				aMsg = guiMsgReturn;
				break;
		}
	}

	return aMsg;	
}

void TClearRegistedInf::Task1000Ms()
{
	volatile unsigned  int uiVal;
	return;
	if(IsShowing){
		if(Eraser.OnGo == 0xB6){
			if(Eraser.Step < 8){
				sDelayMs(2);
				uiVal = nor_get_busy_state(dFlashRegisterDataChip);
				if(uiVal)return;
				uiVal = dFlashRegisterDataAddr + (0x000010000 * Eraser.Step);
				nor_erase_sector_64k(dFlashRegisterDataChip,uiVal);
				Eraser.Step++;
			}else
			if(Eraser.Step < 9){
				sDelayMs(2);
				uiVal = nor_get_busy_state(dFlashRegisterDataChip);
				if(uiVal)return;
				Eraser.Step++;
			}else
			if(Eraser.Step < 10){
				for(uiVal=0; uiVal<40; uiVal++){
					for(int i=0; i<250; i++){
						Port[uiVal].ExistTable[i] = 0;
						Port[uiVal].dTypeTable[i] = 0;
						//EpDev[m][i].fData.Split.dType = 0;
						//EpDev[m][i].fData.Split.Id = 0;
					}
					MainCtl.fData.Split.ExistTablePort[uiVal] = 0;
				}
				MainCtl.fData.Split.PortCount = 0;
				Eraser.Step++;
			}else{
				sDelayMs(2);
				uiVal = nor_get_busy_state(dFlashRegisterDataChip);
				if(uiVal)return;
				InnerCan.SendCmdClearRegData();
				Record.ReadRegisterData();
				if(LanguageEnCn==0)	TextRender_string24(400, Lv.Grid.Top[0]+ 36, clNearWhite, ItemColor, "清除完成        ");
				else 				TextRender_string24(400, Lv.Grid.Top[0]+ 36, clNearWhite, ItemColor, "Cleared        ");
				Record.OperateAdd_CleanUp(0);
				Eraser.OnGo =0;
				Eraser.NotDone =0;
			}
		}
	}
}

void TClearRegistedInf::ForceExit()
{
	sDelayMs(20);
	Record.ReadRegisterData();
	Record.OperateAdd_CleanUp(0xF0);	
	Eraser.OnGo =0;
}

int TClearRegistedInf::ExtRequst(unsigned char Prm)
{
	IsShowing =0;
	if(Eraser.OnGo){
		ForceExit();
	}
	return 0;
}






