#include "OnlineCtrl.h"
#include "XBox.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

const int xUsb	= 20;
const int xRs485	= 160;
const int xCan	= 300;
const int xReadOnly	= 20;
const int xReadWrite	= 160;
const int xActive = 20;
const int xClose	= 160;

void TOnlineCtrl::Init()
{
	CtlIndex = 0;
	IsShowing = 0;
	IsOnGoing = 0;
	LineType =0;
	ReadWrite =0;
	
	IsPermission =0;
}

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

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

void TOnlineCtrl::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, "调试接口选择");
		TextRender_string24(xUsb + 30, Lv.Grid.Top[0] + 40, clNearWhite, "USB");
		TextRender_string24(xRs485 + 30, Lv.Grid.Top[0] + 40, clNearWhite, "UART");
		TextRender_string24(xCan + 30, Lv.Grid.Top[0] + 40, clNearWhite, "内CAN");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "模式选择");
		TextRender_string24(xReadOnly + 30, Lv.Grid.Top[1] +40, clNearWhite, "只读");
		TextRender_string24(xReadWrite + 30, Lv.Grid.Top[1] +40, clNearWhite, "读写");
		TRoundRect::sDrawBySize(xActive, 	Lv.Grid.Top[2] + 4, 112, 30, 7, clNearWhite, "开启", 26, clNearBlack);
		TRoundRect::sDrawBySize(xClose, 	Lv.Grid.Top[2] + 4, 112, 30, 7, clNearWhite, "关闭", 26, clNearBlack);
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Data Transmission->Online configuration");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "Port Choice");
		TextRender_string24(xUsb + 30, Lv.Grid.Top[0] + 40, clNearWhite, "USB");
		TextRender_string24(xRs485 + 30, Lv.Grid.Top[0] + 40, clNearWhite, "UART");
		TextRender_string24(xCan + 30, Lv.Grid.Top[0] + 40, clNearWhite, "Inner-CAN");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "Mode");
		TextRender_string24(xReadOnly + 30, Lv.Grid.Top[1] +40, clNearWhite, "ReadOnly");
		TextRender_string24(xReadWrite + 30, Lv.Grid.Top[1] +40, clNearWhite, "Read Write");
		TRoundRect::sDrawBySize(xActive, 	Lv.Grid.Top[2] + 4, 112, 30, 7, clNearWhite, "Enable", 14, clNearBlack);
		TRoundRect::sDrawBySize(xClose, 	Lv.Grid.Top[2] + 4, 112, 30, 7, clNearWhite, "Disable", 14, clNearBlack);
	}
}

void TOnlineCtrl::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();
	DrawLineType();
	DrawReadWrite();
	Check4Selected();
}

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

void TOnlineCtrl::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();
		DrawLineType();
		DrawReadWrite();
		if(IsPermission){
			PmsIntervene4Gui.ReDraw4GuiRecall();
		}else{
			Check4Selected();
		}
	}
}

void TOnlineCtrl::DrawLineType()
{
	if( LineType == 0){
		TRoundSquare::sDrawBySize(xUsb, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(xRs485,  Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(xCan,  Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}else
	if( LineType == 1){
		TRoundSquare::sDrawBySize(xUsb, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(xRs485, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(xCan,  Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, dRoundR,  dWdDisabelClr , 0);
	}else{
		TRoundSquare::sDrawBySize(xUsb, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(xRs485, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(xCan,  Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, dRoundR,  dWdClr , 1);
	}
}

void TOnlineCtrl::DrawReadWrite()
{
	if( ReadWrite == 0){
		TRoundSquare::sDrawBySize(xReadOnly, Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(xReadWrite,  Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}else{
		TRoundSquare::sDrawBySize(xReadOnly, Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(xReadWrite,  Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, dRoundR,  dWdClr , 1);
	}
}

//______________________________________________________________________________________________________________________________
void TOnlineCtrl::ClearSelected(void)
{
	switch(CtlIndex){
		case 0:
			TRoundSquare::sSelected(xUsb, Lv.Grid.Top[0]+40, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 1:
			TRoundSquare::sSelected(xRs485, Lv.Grid.Top[0]+40, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 2:
			TRoundSquare::sSelected(xCan, Lv.Grid.Top[0]+40, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 3:
			TRoundSquare::sSelected(xReadOnly, Lv.Grid.Top[1]+40, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 4:
			TRoundSquare::sSelected(xReadWrite, Lv.Grid.Top[1]+40, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 5:
			if(LanguageEnCn==0)	TRoundRect::sDrawBySize(xActive, 	Lv.Grid.Top[2] + 4, 112, 30, 7, clNearWhite, "开启", 26, clNearBlack);
			else 				TRoundRect::sDrawBySize(xActive, 	Lv.Grid.Top[2] + 4, 112, 30, 7, clNearWhite, "Enable", 14, clNearBlack);
			break;
		case 6:
			if(LanguageEnCn==0)	TRoundRect::sDrawBySize(xClose, 	Lv.Grid.Top[2] + 4, 112, 30, 7, clNearWhite, "关闭", 26, clNearBlack);
			else				TRoundRect::sDrawBySize(xClose, 	Lv.Grid.Top[2] + 4, 112, 30, 7, clNearWhite, "Disable", 14, clNearBlack);
			break;
		default:

			break;
	}		
}

void TOnlineCtrl::Check4Selected(void)
{
	switch(CtlIndex){
		case 0:
			TRoundSquare::sSelected(xUsb, Lv.Grid.Top[0]+40, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 1:
			TRoundSquare::sSelected(xRs485, Lv.Grid.Top[0]+40, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 2:
			TRoundSquare::sSelected(xCan, Lv.Grid.Top[0]+40, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 3:
			TRoundSquare::sSelected(xReadOnly, Lv.Grid.Top[1]+40, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 4:
			TRoundSquare::sSelected(xReadWrite, Lv.Grid.Top[1]+40, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 5:
			if(LanguageEnCn==0)	TRoundRect::sDrawBySize(xActive, 	Lv.Grid.Top[2] + 4, 112, 30, 7, clBlue, "开启", 26, clNearWhite);
			else 				TRoundRect::sDrawBySize(xActive, 	Lv.Grid.Top[2] + 4, 112, 30, 7, clBlue, "Enable", 14, clNearWhite);
			break;
		case 6:
			if(LanguageEnCn==0)	TRoundRect::sDrawBySize(xClose, 	Lv.Grid.Top[2] + 4, 112, 30, 7, clBlue, "关闭", 26, clNearWhite);
			else 				TRoundRect::sDrawBySize(xClose, 	Lv.Grid.Top[2] + 4, 112, 30, 7, clBlue, "Disable", 14, clNearWhite);
			break;
		default:

			break;
	}		
}

void TOnlineCtrl::PermissionCmdReturnDraw()
{
	RectFillRender(90, Lv.Grid.Bottom[2] +8, 706, Lv.Grid.Bottom[2] +28 + 140, ContentColor);
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	Check4Selected();
}

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


//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TOnlineCtrl::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 '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case VK_LEFT:
			case VK_RIGHT:
			case VK_DELETE:
				HadPrc =0;
				if( (aKey == VK_LEFT) || (aKey == VK_RIGHT) ){
					if(CtlIndex == 0){
						if(aKey == VK_RIGHT){
							ClearSelected();
							CtlIndex++;
							Check4Selected();
							HadPrc =1;
						}
					}else
					if(CtlIndex == 1){
						if(aKey == VK_LEFT){
							ClearSelected();
							CtlIndex--;
							Check4Selected();
							HadPrc =1;
						}else
						if(aKey == VK_RIGHT){
							ClearSelected();
							CtlIndex++;
							Check4Selected();
							HadPrc =1;
						}
					}else
					if(CtlIndex == 2){
						if(aKey == VK_LEFT){
							ClearSelected();
							CtlIndex--;
							Check4Selected();
							HadPrc =1;
						}
					}else
					if(CtlIndex == 3){
						if(aKey == VK_RIGHT){
							ClearSelected();
							CtlIndex++;
							Check4Selected();
							HadPrc =1;
						}
					}else
					if(CtlIndex == 4){
						if(aKey == VK_LEFT){
							ClearSelected();
							CtlIndex--;
							Check4Selected();
							HadPrc =1;
						}
					}else
					if(CtlIndex == 5){
						if(aKey == VK_RIGHT){
							ClearSelected();
							CtlIndex++;
							Check4Selected();
							HadPrc =1;
						}
					}else
					if(CtlIndex == 4){
						if(aKey == VK_LEFT){
							ClearSelected();
							CtlIndex--;
							Check4Selected();
							HadPrc =1;
						}
					}
				}
				if(HadPrc == 0){

				}
				break;
			case VK_UP:
				if(CtlIndex >0){
					ClearSelected();
					CtlIndex--;
					Check4Selected();
				}else{
					ClearSelected();
					CtlIndex=6;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(CtlIndex <6){
					ClearSelected();
					CtlIndex++;
					Check4Selected();
				}else{
					ClearSelected();
					CtlIndex=0;
					Check4Selected();
				}
				break;
			case VK_TAB:

				break;
			case dfKEY_SETUP:
				if(CtlIndex == 0){
					LineType = 0;
					DrawLineType();
				}else
				if(CtlIndex == 1){
					LineType = 1;
					DrawLineType();
				}else
				if(CtlIndex == 2){
					LineType = 2;
					DrawLineType();
				}else
				if(CtlIndex == 3){
					ReadWrite = 0;
					DrawReadWrite();
				}else
				if(CtlIndex == 4){
					ReadWrite = 1;
					DrawReadWrite();
				}
				break;
			case VK_EXECUTE:
				if(CtlIndex == 0){
					LineType = 0;
					DrawLineType();
				}else
				if(CtlIndex == 1){
					LineType = 1;
					DrawLineType();
				}else
				if(CtlIndex == 2){
					LineType = 2;
					DrawLineType();
				}else
				if(CtlIndex == 3){
					ReadWrite = 0;
					DrawReadWrite();
				}else
				if(CtlIndex == 4){
					ReadWrite = 1;
					DrawReadWrite();
				}else
				if(CtlIndex == 5){
					//Active
					PermissionCmd = 1;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(100, Lv.Grid.Bottom[2] + 20, 600 , 140, 3);
					}
				}else
				if(CtlIndex == 6){
					//Close
					PermissionCmd = 2;
					PermissionCmdGO();
				}
				break;
			case VK_RETURN:
				IsShowing = 0;
				aMsg = guiMsgReturn;
				break;
		}
	}

	return aMsg;	
}

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

void TOnlineCtrl::Task1000Ms()
{
	
}



