#include "CanNetworkInf.h"
#include "Runtime.h"

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

//#define dFlash4AreaChip			(0)
//#define dFlash4Area			(0x00BB'0000)

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

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

void TCanNetworkInf::Init()
{

}

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

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

void TCanNetworkInf::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, "本机号");
		if(MainCtl.fData.Split.NetworkMode == 1){
			TextRender_string24(Lv.Grid.Left[0] + 116, Lv.Grid.Top[0] +6, clNearWhite, "集中机");
		}else
		if(MainCtl.fData.Split.NetworkMode == 2){
			TextRender_string24(Lv.Grid.Left[0] + 116, Lv.Grid.Top[0] +6, clNearWhite, "区域机");
			
			TextRender_string24(Lv.Grid.Left[0] + 400, Lv.Grid.Top[0] +40, clNearWhite, "集中机号:");
			TextDigitRender2_24(Lv.Grid.Left[0] + 510, Lv.Grid.Top[0] +40, clNearWhite, ProtoC2C.MasterNum);
		}else{
			TextRender_string24(Lv.Grid.Left[0] + 116, Lv.Grid.Top[0] +6, clNearWhite, "单机  ");
		}
		TextRender_string24(Lv.Grid.Left[0] + 460, Lv.Grid.Top[0] +6, clNearWhite, "     屏蔽的控制器号:");
		
		TextRender_string24(Lv.Grid.Left[0] + 10 +   0, Lv.Grid.Top[0] +40, clNearWhite, "区域机到集中机连接数:");
		TextDigitRender2_24(Lv.Grid.Left[0] + 10 + 252, Lv.Grid.Top[0] +40, clNearWhite, ProtoC2C.SlaveCount +1);
		TextRender_string24(Lv.Grid.Left[0] + 10 + 276, Lv.Grid.Top[0] +40, clNearWhite, "台");
		
		TextRender_string24(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top +4, clNearWhite, "信息互联的集中机号:");
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Local Network->Informatiom");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "CtlID");
		if(MainCtl.fData.Split.NetworkMode == 1){
			TextRender_string24(Lv.Grid.Left[0] + 116, Lv.Grid.Top[0] +6, clNearWhite, "Central      ");	
		}else
		if(MainCtl.fData.Split.NetworkMode == 2){
			TextRender_string24(Lv.Grid.Left[0] + 116, Lv.Grid.Top[0] +6, clNearWhite, "Regional     ");
			TextRender_string24(Lv.Grid.Left[0] + 400, Lv.Grid.Top[0] +40, clNearWhite, "CentralID");
			TextDigitRender2_24(Lv.Grid.Left[0] + 510, Lv.Grid.Top[0] +40, clNearWhite, ProtoC2C.MasterNum);
		}else{
			TextRender_string24(Lv.Grid.Left[0] + 116, Lv.Grid.Top[0] +6, clNearWhite, "Stand Alone  ");
		}
		TextRender_string24(Lv.Grid.Left[0] + 460, Lv.Grid.Top[0] +6, clNearWhite, "Blocking Ctrl ID(s):");
		
		TextRender_string24(Lv.Grid.Left[0] + 10 +   0, Lv.Grid.Top[0] +40, clNearWhite, "Slaver-Master Interlinked Items:");
		TextDigitRender2_24(Lv.Grid.Left[0] + 10 + 384, Lv.Grid.Top[0] +40, clNearWhite, ProtoC2C.SlaveCount +1);
		TextRender_string24(Lv.Grid.Left[0] + 10 + 408, Lv.Grid.Top[0] +40, clNearWhite, "Pcs");
		
		TextRender_string24(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top +4, clNearWhite, "Shared Info Masters List:");
	}
}

void TCanNetworkInf::DrawSelf(void)
{
	int xp;
	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);
	if(Lv.PanelSmall.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, PanelColor);

	DrawFixText();
	TextDigitRender2_24(Lv.Grid.Left[0] + 86, Lv.Grid.Top[0] +6, clNearWhite, MainCtl.fData.Split.MyNum);
	
	DrawHadMask();
	DrawMasterList();
	DrawSlaverList();
}

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


void TCanNetworkInf::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<1; 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);
		TextDigitRender2_24(Lv.Grid.Left[0] + 86, Lv.Grid.Top[0] +6, clNearWhite, MainCtl.fData.Split.MyNum);
		DrawFixText();
		DrawMasterList();
		DrawSlaverList();
		
		//if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}

void TCanNetworkInf::DrawSlaverList()
{	
	int x,y, c, xp;
	x = Lv.Panel.Left +6;
	y = Lv.Panel.Top + 4;
	c =0;
	for(int i=0; (i<dNETWORK_MACHINE_COUNT) and (i<ProtoC2C.SlaveCount); i++){
		if( (ProtoC2C.SlaveList[i] >0) && (ProtoC2C.SlaveList[i] <=dNETWORK_MACHINE_COUNT) ){
			xp = x + (c *262); 
			TextDigitRender2_24(xp, y, clNearWhite, ProtoC2C.SlaveList[i]);
			if(LanguageEnCn==0)TextRender_string24(xp + 30, y, clNearWhite, 	"火灾报警控制器");
			else TextRender_string24(xp + 30, y, clNearWhite, 			"FireAlram Ctrl");
			c++;
			if(c > 2){
				c=0; 
				y+= 28;
			}
		}
	}
}

//_____________________________________________________________________________________________________________________________


void TCanNetworkInf::DrawHadMask()
{
	if( (ProtoC2C.CtlMask.StartNum) && (ProtoC2C.CtlMask.EndNum) ){
		//TextRender_string24(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top +6, clNearWhite, PanelColor, "已屏蔽号段:");
		TextDigitRender2_24(Lv.Grid.Left[0] + 460 + 240 +  0, Lv.Grid.Top[0] +6, clNearWhite, ItemColor, ProtoC2C.CtlMask.StartNum);
		TextRender_string24(Lv.Grid.Left[0] + 460 + 240 + 28, Lv.Grid.Top[0] +6, clNearWhite, ItemColor, "-");
		TextDigitRender2_24(Lv.Grid.Left[0] + 460 + 240 + 46, Lv.Grid.Top[0] +6, clNearWhite, ItemColor, ProtoC2C.CtlMask.EndNum);
	}else{
		//TextRender_string24(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top +6, clNearWhite, PanelColor, "已屏蔽号段:");
		TextRender_string24(Lv.Grid.Left[0] + 460 + 240 +  0, Lv.Grid.Top[0] +6, clNearWhite, ItemColor, "--");
		TextRender_string24(Lv.Grid.Left[0] + 460 + 240 + 28, Lv.Grid.Top[0] +6, clNearWhite, ItemColor, "-");
		TextRender_string24(Lv.Grid.Left[0] + 460 + 240 + 46, Lv.Grid.Top[0] +6, clNearWhite, ItemColor, "--");
	}
}

void TCanNetworkInf::DrawMasterList()
{
	int x, y, i;
	x = Lv.PanelSmall.Left + 10; 
	y = Lv.PanelSmall.Top + 32;
	for(i=0; (i<dNETWORK_MACHINE_MASTER_COUNT) and (i<ProtoC2C.M2MList.Cnt); i++){
		TextDigitRender2_24(x, y, clNearWhite, ProtoC2C.M2MList.Items[i]);
		x += 40;
	}
	for(i=0; i<dNETWORK_MACHINE_MASTER_COUNT; i++){
		TextRender_string24(x, y, clNearWhite, PanelColor, "  ");
		x += 40;
	}
}


TGuiMsgReturn TCanNetworkInf::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg = guiMsgNone;
	int iVal;
	int HadPrc;

	if(0){
		return aMsg;
	}else{
		switch(aKey){
			case VK_LEFT:
			case VK_RIGHT:
			case VK_DELETE:

				break;
			case dfKEY_ASTERISK:

				break;
			case VK_UP:

				break;
			case VK_DOWN:

				break;

			case VK_TAB:

				break;				
			case dfKEY_SETUP:		//0x70		//  Key value for SETUP

				break;
			case VK_EXECUTE:

				break;
			case VK_RETURN:
				aMsg = guiMsgReturn;
				break;
			default :
				break;
		}
	}
	
	return aMsg;	
}

