#include "Gui.h"
#include "DevProperty.h"

#define cmDeviceRuntimeData	207
#define cmDeviceLinkagePowerSuplyMute	208

void TDevProperty::DrawFixTextLinkagePowerSuply()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 4, clNearWhite, ItemColor, "操作");
		TRoundRect::sDrawBySize(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 36, 76, 30, 7, clNearWhite, "消音", 14, clNearBlack);
	}else{
		TextRender_string24(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 4, clNearWhite, ItemColor, "OPER");
		TRoundRect::sDrawBySize(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 36, 76, 30, 7, clNearWhite, "Mute", 14, clNearBlack);
	}
}
	
void TDevProperty::FullRedrawhtLinkagePowerSuply(int Prm)
{
	if(Prm == 0){
		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);
		if(Lv.Panel.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
		ReDrawCommonInf();
		DrawFixTextLinkagePowerSuply();
		if(EdittingDescp){
			CnInput.FullRedraw();
		}else{
			Check4Selected();
		}
	}else{
		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);
		if(Lv.Panel.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
		ReDrawCommonInf();
		DrawFixTextLinkagePowerSuply();
		if(EdittingDescp){
			CnInput.FullRedraw();
		}else{
			Check4Selected();
		}
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}

void TDevProperty::InitHtLinkagePowerSuply()
{
	PanelIsUsingText = 1;
	
	for(int i=2; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	
	DrawFixTextLinkagePowerSuply();
	
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	if(Lv.Panel.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	
	RectFillRender(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top + 6, Lv.PanelSmall.Left + 10 + 20, Lv.PanelSmall.Top + 6 + 20, clGreen);
}

void TDevProperty::EditDescpReturnHtLinkagePowerSuply()
{
	PanelIsUsingText = 1;
	
	for(int i=2; i<4; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	
	DrawFixTextLinkagePowerSuply();
	
	ReDrawPanel();
}

void TDevProperty::Redraw4LinkagePowerSuplyNewLoad()
{
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
}

void TDevProperty::Redraw4LinkagePowerSuplySmallPanelData()
{
	RectFillRender(Lv.PanelSmall.Left + 2, Lv.PanelSmall.Top + 6, Lv.PanelSmall.Left + 2 + 20, Lv.PanelSmall.Top + 6 + 20, clGreen);
	RectFillRender(Lv.PanelSmall.Left + 94, Lv.PanelSmall.Top + 6, Lv.PanelSmall.Left + 94 + 20, Lv.PanelSmall.Top + 6 + 20, clPurple);
	RectFillRender(Lv.PanelSmall.Left + 232, Lv.PanelSmall.Top + 6, Lv.PanelSmall.Left + 232 + 20, Lv.PanelSmall.Top + 6 + 20, clRed);
	RectFillRender(Lv.PanelSmall.Left + 370, Lv.PanelSmall.Top + 6, Lv.PanelSmall.Left + 370 + 20, Lv.PanelSmall.Top + 6 + 20, clBlue);

	TMyString::sClear(TextDraw);
	TMyString::sFrom2Dg(RuntimeData.voltage % 100, TextDraw);
	TMyString::sAddOnChar('.', TextDraw);
	TMyString::sAddOn1Dg(0, TextDraw);
	TMyString::sAddOnChar('V', TextDraw);
	TextRender_string24(Lv.PanelSmall.Left + 24, Lv.PanelSmall.Top + 4, clNearWhite, SmallPanelColor, TextDraw);
	
	TMyString::sClear(TextDraw);
	TMyString::sAddOn2Dg(RuntimeData.OutputVoltage % 1000 /10, TextDraw);
	TMyString::sAddOnChar('.', TextDraw);
	TMyString::sAddOn1Dg(RuntimeData.OutputVoltage %10, TextDraw);
	if(LanguageEnCn==0)TMyString::sAddOnStr("V输出", TextDraw);
	else TMyString::sAddOnStr("V Out", TextDraw);
	TextRender_string24(Lv.PanelSmall.Left + 116, Lv.PanelSmall.Top + 4, clNearWhite, SmallPanelColor, TextDraw);
	
	TMyString::sClear(TextDraw);
	TMyString::sAddOn2Dg(RuntimeData.OutputCurrent % 1000 /10, TextDraw);
	TMyString::sAddOnChar('.', TextDraw);
	TMyString::sAddOn1Dg(RuntimeData.OutputCurrent %10, TextDraw);
	if(LanguageEnCn==0)TMyString::sAddOnStr("A输出", TextDraw);
	else TMyString::sAddOnStr("A Out", TextDraw);
	TextRender_string24(Lv.PanelSmall.Left + 254, Lv.PanelSmall.Top + 4, clNearWhite, SmallPanelColor, TextDraw);
	
	TMyString::sClear(TextDraw);
	TMyString::sAddOn2Dg(RuntimeData.PwrLoaddingPercent % 200, TextDraw);
	if(LanguageEnCn==0)TMyString::sAddOnStr("%负载率", TextDraw);
	else TMyString::sAddOnStr("% Load", TextDraw);
	TextRender_string24(Lv.PanelSmall.Left + 392, Lv.PanelSmall.Top + 4, clNearWhite, SmallPanelColor, TextDraw);
	
	DrawMac();
}

void TDevProperty::ClearSelectedHtLinkagePowerSuply()
{
	char *tText;
	switch(Tag.Inx){
		case 0:
			
			break;
		case 1:
			IList->EditPortNum.Selected = 0;
			IList->EditPortNum.Show();
			break;
		case 2:
			IList->EditAddrNum.Selected = 0;
			IList->EditAddrNum.Show();
			break;
		case 3:
			IList->EditDescpShow.Selected = 0;
			IList->EditDescpShow.Show();
			break;
		case 4:
			IList->EditUserCode.Selected = 0;
			IList->EditUserCode.Show();
			break;
		case 5:
			IList->EditAreaAssign.Selected = 0;
			IList->EditAreaAssign.Show();
			break;
		case 6:
			if(LanguageEnCn==0)tText="消音";else tText="Mute";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 36, 76, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
	}
}
void TDevProperty::Check4SelectedHtLinkagePowerSuply()
{
	char *tText;
	switch(Tag.Inx){
		case 0:
			
			break;
		case 1:
			IList->EditPortNum.Selected = 1;
			IList->EditPortNum.SetPstTail();
			IList->EditPortNum.Show();
			IList->EditPortNum.ShowCursor();
			break;
		case 2:
			IList->EditAddrNum.Selected = 1;
			IList->EditAddrNum.SetPstTail();
			IList->EditAddrNum.Show();
			IList->EditAddrNum.ShowCursor();
			break;
		case 3:
			IList->EditDescpShow.Selected = 1;
			IList->EditDescpShow.Show();
			break;
		case 4:
			IList->EditUserCode.Selected = 1;
			IList->EditUserCode.SetPstTail();
			IList->EditUserCode.Show();
			IList->EditUserCode.ShowCursor();
			break;
		case 5:
			IList->EditAreaAssign.Selected = 1;
			IList->EditAreaAssign.SetPstTail();
			IList->EditAreaAssign.Show();
			IList->EditAreaAssign.ShowCursor();
			break;
		case 6:
			if(LanguageEnCn==0)tText="消音";else tText="Mute";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 36, 76, 30, 7, clDeepBlue, tText, 14, clNearWhite);
			break;
	}	
}

TGuiMsgReturn TDevProperty::KeyInHtLinkagePowerSuply(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	switch(aKey){
		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_DELETE:
		case VK_LEFT:
		case VK_RIGHT:
			if(Tag.Inx < 6){
				KeyInCommonEdit(aKey);
			}else{
				if(Tag.Inx == 6){
					//Light Up
					if(aKey == VK_RIGHT){
						ClearSelectedHtLinkagePowerSuply();
						Tag.Inx = 7;
						Check4SelectedHtLinkagePowerSuply();
					}
				}
			}
			break;
		case VK_UP:
			if(PortNumValid && AddrNumValid){
				ClearSelectedHtLinkagePowerSuply();
				Tag.Inx > Tag.InxMin ? Tag.Inx-- : Tag.Inx = 6;
				Check4SelectedHtLinkagePowerSuply();
			}
			break;
		case VK_DOWN:
			if(PortNumValid && AddrNumValid){
				ClearSelectedHtLinkagePowerSuply();
				Tag.Inx < 6 ? Tag.Inx++ : Tag.Inx = Tag.InxMin;
				Check4SelectedHtLinkagePowerSuply();
			}
			break;
		case VK_TAB:
			//PanelIsUsingText = PanelIsUsingText ? 0 : 1;
			break;
		case dfKEY_SETUP:
			break;
		case dfKEY_QUERY:
			PanelDrawData.DesertTextHex = PanelDrawData.DesertTextHex ? 0: 1;
			break;
		case dfKEY_ENCN:
			SendReqMac();
			break;
		case VK_EXECUTE:
			if(PortNumValid && AddrNumValid){
				if(Tag.Inx <6){
					KeyInCommonEnter(aKey);
				}else{
					if(Tag.Inx == 6){
						SendLinkagePowerSuplyMute();
					}
				}
			}
			break;
		case VK_RETURN:
			IsShow = 0;
			RealTimeSendCmdAbort();
			aMsg = guiMsgReturn;
			break;
	}
	
	
	return aMsg;
}

void TDevProperty::PushLinkagePowerSuplyCanData(unsigned char aCmd, unsigned char *pData)
{
	int i;
	if(aCmd == cmDeviceRuntimeData){
		for(i=0; i<8; i++)RecData[i] = pData[i];
		if(   (1 == RecData[0]) &&
			(MainCtl.fData.Split.MyNum == RecData[1]) &&
			(Path0 == RecData[2]) &&
			(Path1 == RecData[3]) ) {
				RuntimeData.OutputVoltage = RecData[4];
				RuntimeData.OutputCurrent = RecData[5];
				RuntimeData.PwrLoaddingPercent = RecData[6];
				//RuntimeData.OutputCurrent = RecData[7];
		}
	}
}

void TDevProperty::SendLinkagePowerSuplyMute()
{
	SendCmdMute();
}

void TDevProperty::Task1000MsLinkagePowerSuply()
{
	Redraw4LinkagePowerSuplySmallPanelData();
}





