#include "RuntimeData.h"
#include "FwUpgrade.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

const int xFirst	= 10;
const int xSec	= 160;
const int xThi	= 310;
const int xFourth	= 460;
const int xFifth	= 610;

void TFwUpgrade::Init()
{
	CtlIndex = 0;
	IsShowing = 0;
	IsOnGoing = 0;
	ReadWrite =0;
	
	State.Connection =0;
	State.Rsv0 =0;
	State.Rsv0 =1;
	State.Rsv0 =2;
	
	IsPermission =0;
}

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

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

void TFwUpgrade::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, "USB");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "升级部位");
	
		TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 40, 128, 30, 7, clNearWhite, "   连接   ", 4, clNearBlack);
		TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[0] + 40, 128, 30, 7, clNearWhite, "   断开   ", 4, clNearBlack);
	
		TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "   全部   ", 4, clNearBlack);
		TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "   主板   ", 4, clNearBlack);
		TRoundRect::sDrawBySize(xThi, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "   按键   ", 4, clNearBlack);
		TRoundRect::sDrawBySize(xFourth, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "  多线盘  ", 4, clNearBlack);
		TRoundRect::sDrawBySize(xFifth, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "  总线盘  ", 4, clNearBlack);

		//TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, "  全部  ", 12, clNearBlack);
		TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, " 回路母板 ", 4, clNearBlack);
		TRoundRect::sDrawBySize(xThi, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, "  回路卡  ", 4, clNearBlack);
		TRoundRect::sDrawBySize(xFourth, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, "联网接口卡", 4, clNearBlack);
		TRoundRect::sDrawBySize(xFifth, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, " 存储单元 ", 4, clNearBlack);
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Data Transmission->Firmware Upgrade");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "USB");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "Part Choice");
	
		TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 40, 128, 30, 7, clNearWhite, " Connect  ", 4, clNearBlack);
		TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[0] + 40, 128, 30, 7, clNearWhite, "Disconnect", 4, clNearBlack);
	
		TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "   All    ", 4, clNearBlack);
		TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "Main Board", 4, clNearBlack);
		TRoundRect::sDrawBySize(xThi, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, " Keyboard ", 4, clNearBlack);
		TRoundRect::sDrawBySize(xFourth, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "Direct Pad", 4, clNearBlack);
		TRoundRect::sDrawBySize(xFifth, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, " Bus Pad  ", 4, clNearBlack);

		//TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, "  全部  ", 12, clNearBlack);
		TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, "Mother Brd", 4, clNearBlack);
		TRoundRect::sDrawBySize(xThi, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, "   Loop   ", 4, clNearBlack);
		TRoundRect::sDrawBySize(xFourth, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, "interface ", 4, clNearBlack);
		TRoundRect::sDrawBySize(xFifth, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, "Ext Memory", 4, clNearBlack);		
	}
}

void TFwUpgrade::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);
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	DrawFixText();
	Check4Selected();
}

void TFwUpgrade::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	DrawConnState();
}

void TFwUpgrade::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<2; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
		DrawFixText();
		DrawConnState();
		if(IsPermission){
			PmsIntervene4Gui.ReDraw4GuiRecall();
		}else{
			Check4Selected();	
		}
	}
}

void TFwUpgrade::DrawConnState()
{
	if(LanguageEnCn==0){
		if(State.Connection){
			TextRender_string24(Lv.Grid.Left[0] + 100, 	Lv.Grid.Top[0] + 6, clDeepGreen, ItemColor, "已连接");
		}else{
			TextRender_string24(Lv.Grid.Left[0] + 100, 	Lv.Grid.Top[0] + 6, clRed, ItemColor, "未连接");
		}
	}else{
		if(State.Connection){
			TextRender_string24(Lv.Grid.Left[0] + 100, 	Lv.Grid.Top[0] + 6, clDeepGreen, ItemColor, 	"Connected");
		}else{
			TextRender_string24(Lv.Grid.Left[0] + 100, 	Lv.Grid.Top[0] + 6, clRed, ItemColor, 		"UnConnect");
		}
	}
}


//______________________________________________________________________________________________________________________________
void TFwUpgrade::ClearSelected(void)
{
	if(LanguageEnCn==0){
		switch(CtlIndex){
			case 0:	TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 40, 128, 30, 7, clNearWhite, "   连接   ", 4, clNearBlack); break;
			case 1:	TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[0] + 40, 128, 30, 7, clNearWhite, "   断开   ", 4, clNearBlack); break;
		
			case 2:	TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "   全部   ", 4, clNearBlack); break;
			case 3:	TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "   主板   ", 4, clNearBlack); break;
			case 4:	TRoundRect::sDrawBySize(xThi, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "   按键   ", 4, clNearBlack); break;
			case 5:	TRoundRect::sDrawBySize(xFourth, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "  多线盘  ", 4, clNearBlack); break;
			case 6:	TRoundRect::sDrawBySize(xFifth, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "  总线盘  ", 4, clNearBlack); break;
			
			
			case 7:	TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, " 回路母板 ", 4, clNearBlack); break;
			case 8:	TRoundRect::sDrawBySize(xThi, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, "  回路卡  ", 4, clNearBlack); break;
			case 9:	TRoundRect::sDrawBySize(xFourth, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, "联网接口卡", 4, clNearBlack); break;
			case 10:	TRoundRect::sDrawBySize(xFifth, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, " 存储单元 ", 4, clNearBlack); break;

			default:

				break;
		}		
	}else{
		switch(CtlIndex){
			case 0:	TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 40, 128, 30, 7, clNearWhite, " Connect  ", 4, clNearBlack); break;
			case 1:	TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[0] + 40, 128, 30, 7, clNearWhite, "DisConnect", 4, clNearBlack); break;
		
			case 2:	TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "   All    ", 4, clNearBlack); break;
			case 3:	TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "Main Board", 4, clNearBlack); break;
			case 4:	TRoundRect::sDrawBySize(xThi, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, " KeyBoard ", 4, clNearBlack); break;
			case 5:	TRoundRect::sDrawBySize(xFourth, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, "Direct Pad", 4, clNearBlack); break;
			case 6:	TRoundRect::sDrawBySize(xFifth, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clNearWhite, " Bus Pad  ", 4, clNearBlack); break;
			
			
			case 7:	TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, "Mother Brd", 4, clNearBlack); break;
			case 8:	TRoundRect::sDrawBySize(xThi, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, "   Loop   ", 4, clNearBlack); break;
			case 9:	TRoundRect::sDrawBySize(xFourth, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, "Interface ", 4, clNearBlack); break;
			case 10:	TRoundRect::sDrawBySize(xFifth, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clNearWhite, "Ext Memory", 4, clNearBlack); break;

			default:

				break;
		}
	}
}

void TFwUpgrade::Check4Selected(void)
{
	if(LanguageEnCn==0){
		switch(CtlIndex){
			case 0:	TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 40, 128, 30, 7, clBlue, "   连接   ", 4, clNearWhite); break;
			case 1:	TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[0] + 40, 128, 30, 7, clBlue, "   断开   ", 4, clNearWhite); break;
		
			case 2:	TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clBlue, "   全部   ", 4, clNearWhite); break;
			case 3:	TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clBlue, "   主板   ", 4, clNearWhite); break;
			case 4:	TRoundRect::sDrawBySize(xThi, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clBlue, "   按键   ", 4, clNearWhite); break;
			case 5:	TRoundRect::sDrawBySize(xFourth, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clBlue, "  多线盘  ", 4, clNearWhite); break;
			case 6:	TRoundRect::sDrawBySize(xFifth, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clBlue, "  总线盘  ", 4, clNearWhite); break;
			
			
			case 7:	TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clBlue, " 回路母板 ", 4, clNearWhite); break;
			case 8:	TRoundRect::sDrawBySize(xThi, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clBlue, "  回路卡  ", 4, clNearWhite); break;
			case 9:	TRoundRect::sDrawBySize(xFourth, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clBlue, "联网接口卡", 4, clNearWhite); break;
			case 10:	TRoundRect::sDrawBySize(xFifth, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clBlue, " 存储单元 ", 4, clNearWhite); break;
			default:

				break;
		}		
	}else{
		switch(CtlIndex){
			case 0:	TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 40, 128, 30, 7, clBlue, " Connect  ", 4, clNearWhite); break;
			case 1:	TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[0] + 40, 128, 30, 7, clBlue, "DisConnect", 4, clNearWhite); break;
		
			case 2:	TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clBlue, "   All   ", 4, clNearWhite); break;
			case 3:	TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clBlue, "Main Board", 4, clNearWhite); break;
			case 4:	TRoundRect::sDrawBySize(xThi, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clBlue, " KeyBoard ", 4, clNearWhite); break;
			case 5:	TRoundRect::sDrawBySize(xFourth, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clBlue, "Direct Pad", 4, clNearWhite); break;
			case 6:	TRoundRect::sDrawBySize(xFifth, 	Lv.Grid.Top[1] + 40, 128, 30, 7, clBlue, " Bus Pad  ", 4, clNearWhite); break;
			
			
			case 7:	TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clBlue, " Mother Brd ", 4, clNearWhite); break;
			case 8:	TRoundRect::sDrawBySize(xThi, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clBlue, "    Loop    ", 4, clNearWhite); break;
			case 9:	TRoundRect::sDrawBySize(xFourth, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clBlue, "Interface ", 4, clNearWhite); break;
			case 10:	TRoundRect::sDrawBySize(xFifth, 	Lv.Grid.Top[1] + 80, 128, 30, 7, clBlue, "Ext Memory", 4, clNearWhite); break;
			default:

				break;
		}
	}
}

void TFwUpgrade::PermissionCmdReturnDraw()
{
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	Check4Selected();
}

void TFwUpgrade::PermissionCmdGO()
{
	if(PermissionCmd == 1){
		//Go Active
		IsOnGoing = 1;
	}else
	if(PermissionCmd == 2){
		//Do Close
		IsOnGoing = 0;
	}
}
//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TFwUpgrade::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_RIGHT:
				switch(CtlIndex){
					case 0:
					case 2:
					case 3:
					case 4:
					case 5:
					case 7:
					case 8:
					case 9:
						ClearSelected();
						CtlIndex++;
						Check4Selected();
						break;
					default:
						break;
				}
				break;				
			case VK_LEFT:
				switch(CtlIndex){
					case 1:
					case 3:
					case 4:
					case 5:
					case 6:
					case 9:
					case 10:
						ClearSelected();
						CtlIndex--;
						Check4Selected();
						break;
					default:
						break;
				}
				break;
			case VK_UP:
				if(CtlIndex >6){
					ClearSelected();
					CtlIndex -=4;
					Check4Selected();
				}else
				if(CtlIndex >3){
					ClearSelected();
					CtlIndex =1;
					Check4Selected();
				}else
				if(CtlIndex >1){
					ClearSelected();
					CtlIndex -=2;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(CtlIndex <2){
					ClearSelected();
					CtlIndex +=2;
					Check4Selected();
				}else
				if(CtlIndex <3){
					ClearSelected();
					CtlIndex =7;
					Check4Selected();
				}else
				if(CtlIndex <7){
					ClearSelected();
					CtlIndex +=4;
					Check4Selected();
				}
				break;
			case VK_TAB:

				break;
			case dfKEY_SETUP:
				
				break;
			case VK_EXECUTE:
				if(CtlIndex == 0){
					//Active
					PermissionCmd = 1;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(100, Lv.Panel.Top + 12, 600 , 120, 3);
					}
				}else
				if(CtlIndex == 1){
					PermissionCmd = 2;
					if(SystemPermission > 1){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(100, Lv.Panel.Top + 12, 600 , 120, 2);
					}
				}else
				if(CtlIndex == 2){
					//All
				}else
				if(CtlIndex == 3){
					//Ctl
				}else
				if(CtlIndex == 4){
					//Key Pad
				}else
				if(CtlIndex == 5){
					//DirectCtlBoard 
				}else
				if(CtlIndex == 6){
					//HandCtlBoard
				}else
				if(CtlIndex == 7){
					//MotherBoard 
				}else
				if(CtlIndex == 8){
					//Port[0..39] 
				}else
				if(CtlIndex == 9){
					//Network Board
				}else
				if(CtlIndex == 10){
					//Stroge Card 
				}
				break;
			case VK_RETURN:
				IsShowing = 0;
				aMsg = guiMsgReturn;
				break;
		}
	}

	return aMsg;	
}

void TFwUpgrade::Task1000Ms()
{
	if(IsShowing){
		/*if(IsRunCmd){
			RunTick++;

		}
		if( (StartShowWarning == 0) && (IsSelfChecking == 0) ){
			if(ShowingWarning){
				TextRender_string24(120, 88, clNearBlack, Color,  "                    ");
				ShowingWarning = 0;
			}else{
				TextRender_string24(120, 88, clNearBlack, Color,  "升级期间不要关闭电源");
				ShowingWarning =1;
			}
		}*/
	}	
}

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


