#include "PowerSuplyInf.h"
#include "runtime.h"

//#define clOdd	clMoneyGreen
//#define clEven	clWhiteBlue

extern "C"{
	#include "uart_key_drv.h"
}

static const short svGridX [9] = {5, 	96, 	202, 	318, 	396, 476, 560, 638, 720};


void TPowerSuplyInf::Init()
{
	#define CH	30	//Caption Height
	PermissionCmd =0;
	IsPermission =0;
	IsAskingPowerCount =0;
	Result4Asking = 0;
}

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

void TPowerSuplyInf::DrawSelf(void)
{
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	if(LanguageEnCn==0)TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "本机->电源信息");
	else TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "Local->Power Supply Info");
	
	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(svGridX[0], Lv.Grid.Top[0] +4, clNearWhite,  "电源");
		TextRender_string24(svGridX[1], Lv.Grid.Top[0] +4, clNearWhite,  "主电状态");
		TextRender_string24(svGridX[2], Lv.Grid.Top[0] +4, clNearWhite,  "备电状态");
		TextRender_string24(svGridX[3], Lv.Grid.Top[0] +4, clNearWhite,  "输出1");
		TextRender_string24(svGridX[4], Lv.Grid.Top[0] +4, clNearWhite,  "输出2");
		TextRender_string24(svGridX[5], Lv.Grid.Top[0] +4, clNearWhite,  "输出3");
		TextRender_string24(svGridX[6], Lv.Grid.Top[0] +4, clNearWhite,  "电池1");
		TextRender_string24(svGridX[7], Lv.Grid.Top[0] +4, clNearWhite,  "电池2");
		TextRender_string24(svGridX[8], Lv.Grid.Top[0] +4, clNearWhite,  "电量");
	}else{
		TextRender_string24(svGridX[0], Lv.Grid.Top[0] +4, clNearWhite,  "Pwr");
		TextRender_string24(svGridX[1], Lv.Grid.Top[0] +4, clNearWhite,  "MP State");
		TextRender_string24(svGridX[2], Lv.Grid.Top[0] +4, clNearWhite,  "SP State");
		TextRender_string24(svGridX[3], Lv.Grid.Top[0] +4, clNearWhite,  "Vout1");
		TextRender_string24(svGridX[4], Lv.Grid.Top[0] +4, clNearWhite,  "Vout2");
		TextRender_string24(svGridX[5], Lv.Grid.Top[0] +4, clNearWhite,  "Vout3");
		TextRender_string24(svGridX[6], Lv.Grid.Top[0] +4, clNearWhite,  "Bat 1");
		TextRender_string24(svGridX[7], Lv.Grid.Top[0] +4, clNearWhite,  "Bat 2");
		TextRender_string24(svGridX[8], Lv.Grid.Top[0] +4, clNearWhite,  "Energy");
	}
}

void TPowerSuplyInf::Show(void)
{
	IsShowing = 1;
	
	RenderContent();
	DrawSelf();
	DrawData();
}

void TPowerSuplyInf::PermissionCmdReturnDraw()
{
	int x,y;
	RectFillRender(0, Lv.Grid.Top[5] + 20, 799, Lv.Grid.Top[5] + 40 + 160, ContentColor);
	y = Lv.Grid.Top[10] +3;
	x = svGridX[0];
	if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "设置键更新副电源数量");
	else TextRender_string24(x, y, clNearWhite,   "Key Set To Updata Aux Power Supply");
}

void TPowerSuplyInf::FullRedraw(int Prm)
{
	if(Prm == 0){
		Show();
		DrawGetSta();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		Show();
		DrawGetSta();
	}
}


void TPowerSuplyInf::Hide()
{
	if(IsShowing){
		IsShowing = 0;
	}
}

void TPowerSuplyInf::PermissionCmdGO()
{
	if(PermissionCmd == 1){
		IsAskingPowerCount =1;
		AskPowerCount();
		DrawGetSta(1);
	}
}


TGuiMsgReturn TPowerSuplyInf::KeyIn(unsigned char aKey)
{
	int ind,i,aIndex;
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg;
	//Up or Down One Index___________________
	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{
		if(aKey == dfKEY_SETUP){
			PermissionCmd = 1;
			if(SystemPermission > 2){
				PermissionCmdGO();
			}else{
				IsPermission = 1;
				PmsIntervene4Gui.Show4Gui(100, Lv.Grid.Top[5] + 40, 600 , 120, 3);
			}
		}else
		if(aKey == VK_EXECUTE){
			Hide();
			aMsg = guiMsgReturn;
		}else
		if(aKey == dfKEY_QUERY){
			if(IsShowing)
				Show();
		}else
		if(aKey == VK_RETURN){
			Hide();
			aMsg = guiMsgReturn;
		}
	}	
	return aMsg;
}

void TPowerSuplyInf::DrawData()
{
	int x,y;
	int Inx;
	Inx = 1;
	
	for(int i=1; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	if(PowerPrmValue.MainCount >0){
		y = Lv.Grid.Top[Inx] +3;
		
		x = svGridX[0];
		if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "主电源");
		else TextRender_string24(x, y, clNearWhite,   "PS-main");
		
		x = svGridX[1] +24;
		if(PowerSuplyState || PowerSuplyComm || PowerSuplyOutput){
			if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "故障");	//主电
			else TextRender_string24(x, y, clNearWhite,   "Fault");
		}else{
			if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "正常");	
			else TextRender_string24(x, y, clNearWhite,   "Work");
		}

		x = svGridX[2] +24;
		if(PowerBkState || BatState || Bat1UnderVoltage || Bat2UnderVoltage){
			if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "故障");	//备电
			else TextRender_string24(x, y, clNearWhite,   "Fault");	//备电
		}else{
			if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "正常");
			else TextRender_string24(x, y, clNearWhite,   "Work");
		}
		
		x = svGridX[3];
		TextDigitRender2Right24(x, y, clNearWhite,   PsBatData.Body.PsVoltageCh1.D16 / 1000 % 100);
		TextRender_string24(x+24, y, clNearWhite,  ".");
		TextDigitRender1Right24(x+36, y, clNearWhite,  PsBatData.Body.PsVoltageCh1.D16 / 100 % 10);
		TextRender_string24(x+48, y, clNearWhite,  "V");
		
		x = svGridX[4];
		TextDigitRender2Right24(x, y, clNearWhite,   PsBatData.Body.PsVoltageCh2.D16 / 1000 % 100);
		TextRender_string24(x+24, y, clNearWhite,  ".");
		TextDigitRender1Right24(x+36, y, clNearWhite,  PsBatData.Body.PsVoltageCh2.D16 / 100 % 10);
		TextRender_string24(x+48, y, clNearWhite,  "V");
		
		x = svGridX[5];
		TextRender_string24(x+18, y, clNearWhite,  "--");
		
		x = svGridX[6];
		TextDigitRender2Right24(x, y, clNearWhite,   PsBatData.Body.BatVoltage1.D16 / 1000 % 100);
		TextRender_string24(x+24, y, clNearWhite,  ".");
		TextDigitRender1Right24(x+36, y, clNearWhite,  PsBatData.Body.BatVoltage1.D16 / 100 % 10);
		TextRender_string24(x+48, y, clNearWhite,  "V");
		
		x = svGridX[7];
		TextDigitRender2Right24(x, y, clNearWhite,   PsBatData.Body.BatVoltage2.D16 / 1000 % 100);
		TextRender_string24(x+24, y, clNearWhite,  ".");
		TextDigitRender1Right24(x+36, y, clNearWhite,  PsBatData.Body.BatVoltage2.D16 / 100 % 10);
		TextRender_string24(x+48, y, clNearWhite,  "V");
		
		unsigned int tEng = (PsBatData.Body.BatEnerger1);/// + PsBatData.Body.BatEnerger2);
		x = svGridX[8];
		TextDigitRender3Right24(x, y, clNearWhite,  tEng % 1000);
		TextRender_string24(x+36, y, clNearWhite,   "%");

		Inx++;
	}
	
	if(PowerPrmValue.SubExist[0]){
		y = Lv.Grid.Top[Inx] +3;
		
		x = svGridX[0];
		if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "副电源1");
		else TextRender_string24(x, y, clNearWhite,   "PS-Aux1");
		
		x = svGridX[1] +24;
		if(PowerPrmValue.Sub1.Body.PowerLineFault || PowerPrmValue.Sub1.Body.OfflineFault || PowerPrmValue.Sub1.Body.OutputFault){
			if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "故障");	//主电
			else TextRender_string24(x, y, clNearWhite,   "Fault");	//主电
		}else{
			if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "正常");
			else TextRender_string24(x, y, clNearWhite,   "Work");	
		}

		x = svGridX[2] +24;
		if(PowerPrmValue.Sub1.Body.BatFault || PowerPrmValue.Sub1.Body.Bat1VoltageLow || PowerPrmValue.Sub1.Body.Bat2VoltageLow){
			if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "故障");	//备电
			else TextRender_string24(x, y, clNearWhite,   "Fault");	//备电
		}else{
			if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "正常");
			else TextRender_string24(x, y, clNearWhite,   "Work");
		}
		
		x = svGridX[3];
		TextDigitRender2Right24(x, y, clNearWhite,   PowerPrmValue.Sub1.Body.vChannel1 / 1000 % 100);
		TextRender_string24(x+24, y, clNearWhite,  ".");
		TextDigitRender1Right24(x+36, y, clNearWhite,  (PowerPrmValue.Sub1.Body.vChannel1 % 1000)/100);
		TextRender_string24(x+48, y, clNearWhite,  "V");
		
		x = svGridX[4];
		TextDigitRender2Right24(x, y, clNearWhite,   PowerPrmValue.Sub1.Body.vChannel2 / 1000 % 100);
		TextRender_string24(x+24, y, clNearWhite,  ".");
		TextDigitRender1Right24(x+36, y, clNearWhite,  (PowerPrmValue.Sub1.Body.vChannel2 % 1000)/100);
		TextRender_string24(x+48, y, clNearWhite,  "V");
		
		x = svGridX[5];
		TextRender_string24(x+18, y, clNearWhite,  "--");
		
		x = svGridX[6];
		TextDigitRender2Right24(x, y, clNearWhite,   PowerPrmValue.Sub1.Body.vBat1 / 1000  % 100);
		TextRender_string24(x+24, y, clNearWhite,  ".");
		TextDigitRender1Right24(x+36, y, clNearWhite,  (PowerPrmValue.Sub1.Body.vBat1 % 1000)/100);
		TextRender_string24(x+48, y, clNearWhite,  "V");
		
		x = svGridX[7];
		TextDigitRender2Right24(x, y, clNearWhite,   PowerPrmValue.Sub1.Body.vBat2 / 1000 % 100);
		TextRender_string24(x+24, y, clNearWhite,  ".");
		TextDigitRender1Right24(x+36, y, clNearWhite,  (PowerPrmValue.Sub1.Body.vBat2 % 1000)/100);
		TextRender_string24(x+48, y, clNearWhite,  "V");
		
		unsigned int tEng = (PowerPrmValue.Sub1.Body.PercentBat1 + PowerPrmValue.Sub1.Body.PercentBat2) / 2;
		x = svGridX[8];
		TextDigitRender3Right24(x, y, clNearWhite,  tEng % 1000);
		TextRender_string24(x+36, y, clNearWhite,   "%");

		Inx++;
	}
	if(PowerPrmValue.SubExist[1]){
		y = Lv.Grid.Top[Inx] +3;
		
		x = svGridX[0];
		if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "副电源2");
		else TextRender_string24(x, y, clNearWhite,   "PS-Aux2");
		
		x = svGridX[1] +24;
		if(PowerPrmValue.Sub2.Body.PowerLineFault || PowerPrmValue.Sub2.Body.OfflineFault || PowerPrmValue.Sub2.Body.OutputFault){
			if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "故障");	//主电
			else TextRender_string24(x, y, clNearWhite,   "Fault");
		}else{
			if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "正常");
			else TextRender_string24(x, y, clNearWhite,   "Work");			
		}

		x = svGridX[2] +24;
		if(PowerPrmValue.Sub2.Body.BatFault || PowerPrmValue.Sub2.Body.Bat1VoltageLow || PowerPrmValue.Sub2.Body.Bat2VoltageLow){
			if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "故障");	//备电
			else TextRender_string24(x, y, clNearWhite,   "Fault");
		}else{
			if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "正常");
			else TextRender_string24(x, y, clNearWhite,   "Work");
		}
		
		x = svGridX[3];
		TextDigitRender2Right24(x, y, clNearWhite,   PowerPrmValue.Sub2.Body.vChannel1 / 1000 % 100);
		TextRender_string24(x+24, y, clNearWhite,  ".");
		TextDigitRender1Right24(x+36, y, clNearWhite,  (PowerPrmValue.Sub2.Body.vChannel1 % 1000)/100);
		TextRender_string24(x+48, y, clNearWhite,  "V");
		
		x = svGridX[4];
		TextDigitRender2Right24(x, y, clNearWhite,   PowerPrmValue.Sub2.Body.vChannel2 / 1000 % 100);
		TextRender_string24(x+24, y, clNearWhite,  ".");
		TextDigitRender1Right24(x+36, y, clNearWhite,  (PowerPrmValue.Sub2.Body.vChannel2 % 1000)/100);
		TextRender_string24(x+48, y, clNearWhite,  "V");
		
		x = svGridX[5];
		TextRender_string24(x+18, y, clNearWhite,  "--");
		
		x = svGridX[6];
		TextDigitRender2Right24(x, y, clNearWhite,   PowerPrmValue.Sub2.Body.vBat1 / 1000 % 100);
		TextRender_string24(x+24, y, clNearWhite,  ".");
		TextDigitRender1Right24(x+36, y, clNearWhite,  (PowerPrmValue.Sub2.Body.vBat1 % 1000)/100);
		TextRender_string24(x+48, y, clNearWhite,  "V");
		
		x = svGridX[7];
		TextDigitRender2Right24(x, y, clNearWhite,   PowerPrmValue.Sub2.Body.vBat2 / 1000 % 100);
		TextRender_string24(x+24, y, clNearWhite,  ".");
		TextDigitRender1Right24(x+36, y, clNearWhite,  (PowerPrmValue.Sub2.Body.vBat2 % 1000)/100);
		TextRender_string24(x+48, y, clNearWhite,  "V");
		
		unsigned int tEng = (PowerPrmValue.Sub2.Body.PercentBat1 + PowerPrmValue.Sub2.Body.PercentBat2) / 2;
		x = svGridX[8];
		TextDigitRender3Right24(x, y, clNearWhite,  tEng % 1000);
		TextRender_string24(x+36, y, clNearWhite,   "%");

		Inx++;
	}

	y = Lv.Grid.Top[10] +3;
	x = svGridX[0];
	if(LanguageEnCn==0)TextRender_string24(x, y, clNearWhite,   "设置键更新副电源数量");
	else TextRender_string24(x, y, clNearWhite,   "Key Set To Updata Aux Power Supply");
}

void TPowerSuplyInf::DrawGetSta()
{
	int x,y;
	x = 10; y= Lv.Grid.Top[4]+6;
	if(LanguageEnCn==0){
		if(Result4Asking == 0){
			TextRender_string24(x, y, clNearWhite, ContentColor,  "                        ");
		}else
		if(Result4Asking == 1){
			TextRender_string24(x, y, clNearWhite, ContentColor,  "正在读取副电源数量      ");
		}else
		if(Result4Asking == 2){
			TextRender_string24(x, y, clNearWhite, ContentColor,  "读取副电源数量成功      ");
		}else{
			TextRender_string24(x, y, clNearWhite, ContentColor,  "写入电源配置失败        ");
		}
	}else{
		if(Result4Asking == 0){
			TextRender_string24(x, y, clNearWhite, ContentColor,  "                          ");
		}else
		if(Result4Asking == 1){
			TextRender_string24(x, y, clNearWhite, ContentColor,  "Reading Sub Power Suppy   ");
		}else
		if(Result4Asking == 2){
			TextRender_string24(x, y, clNearWhite, ContentColor,  "Read Sub Power Suppy OK   ");
		}else{
			TextRender_string24(x, y, clNearWhite, ContentColor,  "Wrote Config Error        ");
		}
	}
}

void TPowerSuplyInf::DrawGetSta(unsigned char aSta)
{
	Result4Asking = aSta;
	DrawGetSta();
}

void TPowerSuplyInf::AskPowerCount()
{
	sys_get_power_Count();
}

void TPowerSuplyInf::SavePwrCount(unsigned char aExistMain, unsigned char aExistSub1, unsigned char aEsixtSub2)
{
	if(IsAskingPowerCount){
		IsAskingPowerCount =0;
		Record.WritePowerSuplyCount(aExistSub1, aEsixtSub2);
		Record.ReadPowerSuplyCount();
		if(IsShowing){
			if( (IsPermission == 0) && (IsSelfChecking == 0) && (PmsIntervene.IsShow == 0)  ){
				DrawData();
				if( (PowerPrmValue.SubExist[0] == aExistSub1) && (PowerPrmValue.SubExist[1] == aEsixtSub2) )DrawGetSta(2);
				else DrawGetSta(3);
			}else{
				if( (PowerPrmValue.SubExist[0] == aExistSub1) && (PowerPrmValue.SubExist[1] == aEsixtSub2) )Result4Asking = 2;
				else Result4Asking = 3;
			}
		}
	}
}

int TPowerSuplyInf::ExtRequst(unsigned char Prm)
{
	IsAskingPowerCount =0;
	if(IsShowing){
		IsShowing =0;
	}
	return 0;
}

