#include "ClearDescp.h"
#include "Runtime.h"


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

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

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

static const int xFirst	=  10;


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

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

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

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

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

void TClearDescp::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 TClearDescp::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, "Clear", 6, clNearWhite);
}

void TClearDescp::sDoClear()
{
	volatile unsigned  int aFlashState;
	unsigned int i;
	unsigned int fAddr64K;
	for(i=0; i<8; i++){
		SysLoopTick = 0;
		fAddr64K = dFlashAddrDescpBase + (SizeOf64K * i);
		do{
			sDelayMs(2);
			aFlashState = nor_get_busy_state(dEepDescpChip);
		}while(aFlashState);
		nor_erase_sector_64k(dEepDescpChip,fAddr64K);
		do{
			sDelayMs(400);
			aFlashState = nor_get_busy_state(dEepDescpChip);
		}while(aFlashState);
	}
	SysLoopTick = 0;
	do{
		sDelayMs(2);
		aFlashState = nor_get_busy_state(dEepDescpChip);
	}while(aFlashState);
	Record.ReadDescp();
	
	/* Move To Function sDoClearNetwork
	int ChipInx;
	for(i=1; i<17; i++){
		ChipInx = NwDescpUsingChip[i];
		fAddr64K = NwDescpUsingFlashAddr[i];
		do{
			sDelayMs(2);
			aFlashState = nor_get_busy_state(ChipInx);
		}while(aFlashState);
		nor_erase_sector_64k(ChipInx,fAddr64K);
		do{
			sDelayMs(400);
			aFlashState = nor_get_busy_state(ChipInx);
		}while(aFlashState);
	}
	Record.ReadDescpNw();*/
}

void TClearDescp::sDoClearNetwork()
{
	volatile unsigned  int aFlashState;
	unsigned int i;
	unsigned int fAddr64K;

	int ChipInx;
	for(i=1; i<dNETWORK_MACHINE_COUNT+1; i++){
		ChipInx = NwDescpUsingChip[i];
		for(unsigned int m=0; m<6; m++){
			fAddr64K = NwDescpUsingFlashAddr[i] + (m * SizeOf64K);
			do{
				sDelayMs(2);
				aFlashState = nor_get_busy_state(ChipInx);
			}while(aFlashState);
			nor_erase_sector_64k(ChipInx,fAddr64K);
			do{
				sDelayMs(400);
				aFlashState = nor_get_busy_state(ChipInx);
			}while(aFlashState);
		}
	}
	Record.ReadDescpNw();
}

void TClearDescp::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;
	}
}

//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TClearDescp::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, "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 TClearDescp::Task1000Ms()
{
	volatile unsigned  int uiVal;
	if(IsShowing){
		if(Eraser.OnGo == 0xB6){
			if(Eraser.Step < 6){
				sDelayMs(2);
				uiVal = nor_get_busy_state(dEepDescpChip);
				if(uiVal)return;
				uiVal = dFlashAddrDescpBase + (Eraser.Step * 0x10000);
				nor_erase_sector_64k(dEepDescpChip,uiVal);
				Eraser.Step++;
			}else
			if(Eraser.Step < 7){
				sDelayMs(2);
				uiVal = nor_get_busy_state(dEepDescpChip);
				if(uiVal)return;
				Eraser.Step++;
			}else{
				sDelayMs(2);
				uiVal = nor_get_busy_state(dEepDescpChip);
				if(uiVal)return;
				Record.ReadDescp();
				
				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(4);
				Eraser.OnGo =0;
			}
		}
	}		
}

void TClearDescp::ForceExit()
{
	Record.ReadDescp();
	Record.OperateAdd_CleanUp(0xF4);
	Eraser.OnGo =0;
}

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





