#include "RunModeCfg.h"
#include "Runtime.h"

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

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

void TRunModeCfg::Init()
{
	IsShow = 0;
	Tag.Reset();
	SaveSta = 0;
	IsPermission = 0;
	
	IsEnteringNewMode =0;
	EnteringModeWhat =0;
	
	for(int i=0; i<dDIRECTCTL_MAX_COUNT; i++){
		DirectPadMode[i] = 0;
	}
}

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

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

void TRunModeCfg::DrawSelf(void)
{
	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(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "工程->运行模式");
		TextRender_string24(Lv.Grid.Left[0] + 6, Lv.Grid.Top[0] +8, clNearWhite,  "工程名称");
		TextRender_string24(Lv.Grid.Left[0] + 520, Lv.Grid.Top[0] +8, clNearWhite,  "运行时长:");
		TextDigitRender5Right24(Lv.Grid.Left[0] + 636, Lv.Grid.Top[0] +8, clNearWhite, TimeSecHasRun / 24);
		TextRender_string24(Lv.Grid.Left[0] + 698, Lv.Grid.Top[0] +8, clNearWhite,  "天");
		TextDigitRender2Right24(Lv.Grid.Left[0] + 722, Lv.Grid.Top[0] +8, clNearWhite, TimeSecHasRun % 24);
		TextRender_string24(Lv.Grid.Left[0] + 746, Lv.Grid.Top[0] +8, clNearWhite,  "小时");
		TextRender_string24(Lv.Grid.Left[1] + 6, Lv.Grid.Top[1] +10, clNearWhite,  "运行模式");
		TextRender_string24(Lv.Grid.Left[1] + 46, Lv.Grid.Top[1] +50, clNearWhite,  "正常运行");
		TextRender_string24(Lv.Grid.Left[1] + 46, Lv.Grid.Top[1] +90, clNearWhite,  "调试运行");
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "Project->Maintenance Reminder");
		TextRender_string24(Lv.Grid.Left[0] + 6, Lv.Grid.Top[0] +8, clNearWhite,  "PrjName");
		TextRender_string24(Lv.Grid.Left[0] + 520, Lv.Grid.Top[0] +8, clNearWhite,  "Duration:");
		TextDigitRender5Right24(Lv.Grid.Left[0] + 636, Lv.Grid.Top[0] +8, clNearWhite, TimeSecHasRun / 24);
		TextRender_string24(Lv.Grid.Left[0] + 698, Lv.Grid.Top[0] +8, clNearWhite,  "D");
		TextDigitRender2Right24(Lv.Grid.Left[0] + 722, Lv.Grid.Top[0] +8, clNearWhite, TimeSecHasRun % 24);
		TextRender_string24(Lv.Grid.Left[0] + 746, Lv.Grid.Top[0] +8, clNearWhite,  "Hour");
		
		TextRender_string24(Lv.Grid.Left[1] + 6, Lv.Grid.Top[1] +10, clNearWhite,  "Run Mode");
		TextRender_string24(Lv.Grid.Left[1] + 46, Lv.Grid.Top[1] +50, clNearWhite,  "Normal Run");
		TextRender_string24(Lv.Grid.Left[1] + 46, Lv.Grid.Top[1] +90, clNearWhite,  "Test Mode ");
	}

	
	ShowIfNormal();
}

void TRunModeCfg::Show(void)
{	
	IsShow =1;
	RenderBackGround();
	DrawSelf();
}

void TRunModeCfg::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();aPanel->ReDrawItems();
		Check4Selected();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], ContentColor);
		DrawSelf();
		aPanel->ReDrawItems();
		Check4Selected();
		
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}

void TRunModeCfg::PermissionCmdReturnDraw()
{
	RectFillRender(Lv.Grid.Left[2] + 200 -10, Lv.Grid.Top[2] + 40 -10, Lv.Grid.Left[2] + 200 -10 + 400 + 20, Lv.Grid.Top[2] + 40 -10 + 140 + 20, ContentColor);
}

void TRunModeCfg::PermissionCmdGO()
{
	//for(int i=0; i<24; i++)DirectPadMode[i] = 0;
	//DirectPadHasUpdata =0;
	//RetryTimes =0;
	//SpendSecond =0;
	//IsEnteringNewMode =1;
	//EnteringModeWhat =SettingState;
	//SendNewMode(EnteringModeWhat);
	//StateBarReflash();
	//ShowSta(2);
	EnteringModeWhat =SettingState;
	IsEnteringNewMode =0;
	RunState = EnteringModeWhat;
	if(IsSelfChecking == 0){
		StateBarReflash();
		if(IsShow){
			ShowSta(1);
		}
	}
}

void TRunModeCfg::StartupSetRunMode()
{
	for(int i=0; i<24; i++)DirectPadMode[i] = 0;
	DirectPadHasUpdata =0;
	RetryTimes =0;
	SpendSecond =0;
	IsEnteringNewMode =1;
	EnteringModeWhat = RunModeRun;
	SendNewMode(EnteringModeWhat);
}

//______________________________________________________________________________________________________________________________
void TRunModeCfg::ClearSelected(void)
{
	unsigned int i;
	for(i=1; i<8;i++)aPanel->Edit[i].Selected = 0;
	if(Tag.Group == 0){
		switch(Tag.Inx0){
			case 0:
				TRoundSquare::sSelected(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 52, dWWidth, dWHeigh, 4, ItemColor);
				break;
			case 1:
				TRoundSquare::sSelected(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 92, dWWidth, dWHeigh, 4, ItemColor);
				break;
			default:

				break;
		}	
	}
}

void TRunModeCfg::Check4Selected(void)
{
	unsigned int i;
	for(i=1; i<8;i++)aPanel->Edit[i].Selected = 0;
	if(Tag.Group == 0){
		switch(Tag.Inx0){
			case 0:
				TRoundSquare::sSelected(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 52, dWWidth, dWHeigh, 4, clNearWhite);
				break;
			case 1:
				TRoundSquare::sSelected(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 92, dWWidth, dWHeigh, 4, clNearWhite);
				break;
			default:

				break;
		}	
	}
}

void TRunModeCfg::ShowIfNormal()
{
	if(SettingState == RunModeAdjust){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] + 52, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] + 92, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
	}else{
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] + 52, dWWidth, dWHeigh, dRoundR, dWdClr, 1);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] + 92, dWWidth, dWHeigh, dRoundR, dWdDisabelClr, 0);
	}
}

void TRunModeCfg::ShowMth()
{

}

void TRunModeCfg::ShowVision()
{

}

void TRunModeCfg::ShowSound()
{

}

void TRunModeCfg::ExtLoadData()
{

}

//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TRunModeCfg::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn MBoxMsg = guiMsgNone;
	int iVal;
	TGuiMsgReturn PmsRtnMsg;
	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_DELETE: 
			case  VK_0:	
			case  VK_1:
			case  VK_2:
			case  VK_3:
			case  VK_4:
			case  VK_5:
			case  VK_6:	
			case  VK_7:
			case  VK_8:
			case  VK_9:
			case  VK_LEFT:
			case  VK_RIGHT:
				if(Tag.Group == 0){
					if(Tag.Inx0 == 0){
						if(SettingState == RunModeAdjust) SettingState = RunModeRun; else SettingState = RunModeAdjust;
						ShowIfNormal();
					}else
					if(Tag.Inx0 == 1){
						if(SettingState == RunModeAdjust) SettingState = RunModeRun; else SettingState = RunModeAdjust;
						ShowIfNormal();
					}
				}
				break;
			case VK_UP:
				if(Tag.Group == 0){
					ClearSelected();
					Tag.Inx0 =0;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(Tag.Group == 0){
					ClearSelected();
					Tag.Inx0 =1;
					Check4Selected();
				}
				break;
			case VK_F1:
				if(Tag.Group == 0){
					if(Tag.Inx0 == 0){
						if(SettingState == RunModeAdjust) SettingState = RunModeRun; else SettingState = RunModeAdjust;
						ShowIfNormal();
					}else
					if(Tag.Inx0 == 1){
						if(SettingState == RunModeAdjust) SettingState = RunModeRun; else SettingState = RunModeAdjust;
						ShowIfNormal();
					}
				}
				break;
			case VK_EXECUTE:
				if(SystemPermission > 2){
					PermissionCmdGO();
				}else{
					IsPermission = 1;
					PmsIntervene4Gui.Show4Gui(Lv.Grid.Left[2] + 200, Lv.Grid.Top[2] + 40, 400, 140, 3);
				}
				break;
			case VK_RETURN:
				aMsg = guiMsgReturn;
				break;
		}
		if( (aKey != VK_EXECUTE) && (aKey != VK_RETURN) ){
			ShowSta(0);
		}
	}
	
	return aMsg;	
}

void TRunModeCfg::InitPanel(void)
{
	int i;
	aPanel->Init(400,400,10,10,0,0,clFrmFace, bvRaised);
	
	aPanel->Edit[0].Init(Lv.Grid.Left[0] +104, Lv.Grid.Top[0] + 2, 400, 	32, 	0,0,	1,ItemColor);
	
	aPanel->Edit[1].Init(Lv.Grid.Left[1] +280, Lv.Grid.Top[1] + 86,46,	30,	0,0,	1,ItemColor);
	
	
	for(i=0; i<1; i++){
		aPanel->Edit[i].IsShowCursor = 0;
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
	}
	for(i=1; i<2; i++){
		//aPanel->Edit[i].IsShowCursor = 1;
		//aPanel->Edit[i].Enable = 1;
		//aPanel->Edit[i].Visible = 1;
		//aPanel->Edit[i].MaxLen = 2;
		//aPanel->Edit[i].IsShowCursor = 1;
	}
}

void TRunModeCfg::LoadData()
{
	SettingState = RunState;

	for(int i=0; i<32; i++){
		aPanel->Edit[0].Str.Text[i] = PrjName[i];
	}
	aPanel->Edit[0].Str.Text[32] = '\0';
	aPanel->Edit[0].Str.Text[33] = '\0';
	
	//aPanel->Edit[1].Str.FromUInt2Dg(Setting.Hours);

	ShowIfNormal();
	aPanel->ReDrawItems();
	
	Check4Selected();
}

void TRunModeCfg::WriteData()
{

}

void TRunModeCfg::sRecoverSetDefault()
{

}

void TRunModeCfg::ShowSta()
{
	if(IsShow == 0)return;
	if(LanguageEnCn==0){
		if(SaveSta == 0){
			TextRender_string24(ResultSavePoint.x, ResultSavePoint.y, clNearWhite, ContentColor, "              ");
		}else
		if(SaveSta == 1){
			TextRender_string24(ResultSavePoint.x, ResultSavePoint.y, clNearWhite, ContentColor, "设置成功      ");
		}else
		if(SaveSta == -1){
			TextRender_string24(ResultSavePoint.x, ResultSavePoint.y, clNearWhite, ContentColor, "设置失败      ");
		}else
		if(SaveSta == 2){
			TextRender_string24(ResultSavePoint.x, ResultSavePoint.y, clNearWhite, ContentColor, "等待多线盘就绪");
		}
	}else{
		if(SaveSta == 0){
			TextRender_string24(ResultSavePoint.x, ResultSavePoint.y, clNearWhite, ContentColor, "                               ");
		}else
		if(SaveSta == 1){
			TextRender_string24(ResultSavePoint.x, ResultSavePoint.y, clNearWhite, ContentColor, "Set Successful                 ");
		}else
		if(SaveSta == -1){
			TextRender_string24(ResultSavePoint.x, ResultSavePoint.y, clNearWhite, ContentColor, "Failed To Set                  ");
		}else
		if(SaveSta == 2){
			TextRender_string24(ResultSavePoint.x, ResultSavePoint.y, clNearWhite, ContentColor, "Wait for Direct Pad Board Ready");
		}
	}
}

void TRunModeCfg::SendNewMode(unsigned char aNewMode)
{
	TCanHeaderID aIde;
	unsigned int i;
	unsigned int Len;
	
	Len =4;
	for(i=0; i<8; i++){
		CommData[i] = 0;
	}
	
	CommData[0] = 0;
	CommData[1] = MainCtl.fData.Split.MyNum;
	CommData[2] = 0;
	CommData[3] = 0;

	if(aNewMode == RunModeAdjust){
		aIde.BitF.Cmd = cmEnterAdjustMode;
	}else{
		aIde.BitF.Cmd = cmExitAdjustMode;
	}
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, CommData, Len);

	RunState = 0;
}

void TRunModeCfg::GetInnerCanData(unsigned char aCmd, unsigned char aDirectPadNum)
{
	if(aCmd == cmEnterAdjustMode ){
		if((aDirectPadNum >= dADDR_DIRECTPAD_START) && (aDirectPadNum <= dADDR_DIRECTPAD_END)){
			RunModeCfg.DirectPadMode[aDirectPadNum - dADDR_DIRECTPAD_START] = RunModeAdjust;
			DirectPadHasUpdata =1;
		}
	}else
	if(aCmd == cmExitAdjustMode ){
		if((aDirectPadNum >= dADDR_DIRECTPAD_START) && (aDirectPadNum <= dADDR_DIRECTPAD_END)){
			RunModeCfg.DirectPadMode[aDirectPadNum - dADDR_DIRECTPAD_START] = RunModeRun;
			DirectPadHasUpdata =1;
		}
	}	
}

void TRunModeCfg::Task1000Ms()
{
	/*
	int NotEnterCount, i;
	if(IsEnteringNewMode){
		DirectPadHasUpdata =0;
		NotEnterCount = 0;
		for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
			if(DirectCtlBoard[i].PadIsProhibit == 0){
				if(EnteringModeWhat != DirectPadMode[i]){
					NotEnterCount =1;
				}
			}
		}
		if(NotEnterCount == 0){
			IsEnteringNewMode =0;
			RunState = EnteringModeWhat;
			if(IsSelfChecking == 0){
				StateBarReflash();
				if(IsShow){
					ShowSta(1);
				}
			}
			return;
		}
		SpendSecond++;
		if(SpendSecond > 6){
			SpendSecond =0;
			RetryTimes++;
			if(RetryTimes > 10){
				IsEnteringNewMode =0;
			}else{
				StartupSetRunMode();
			}
		}
	}*/
}

void TRunModeCfg::ShowSta(int aSta)
{
	SaveSta = aSta;
	ShowSta();
}






