#include "BroadcastCfg.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

void TBroadcastCfg::Init()
{
	CtlIndex = 0;
	IsShow = 0;
	IsPermission =0;
	Sta =0;
}

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

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

void TBroadcastCfg::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(Lv.Grid.Left[0] + 40, Lv.Grid.Top[0] +40, clNearWhite, "并行模式");
		TextRender_string24(Lv.Grid.Left[0] + 190, Lv.Grid.Top[0] +40, clNearWhite, "交替模式");
	
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "声光输出时间");
	
		TextRender_string24(Lv.Grid.Left[1] + 60, Lv.Grid.Top[1] +40, clNearWhite, "秒");
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite, "广播输出时间");
		TextRender_string24(Lv.Grid.Left[2] + 60, Lv.Grid.Top[2] +40, clNearWhite, "秒");
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Linkage->Broadcast Acoustooptic Alternating");	
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "Mode");
		TextRender_string24(Lv.Grid.Left[0] + 40, Lv.Grid.Top[0] +40, clNearWhite, "Parallel");
		TextRender_string24(Lv.Grid.Left[0] + 190, Lv.Grid.Top[0] +40, clNearWhite, "Alternating");
	
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "Acoustooptic Activated Time");
	
		TextRender_string24(Lv.Grid.Left[1] + 60, Lv.Grid.Top[1] +40, clNearWhite, "Second");
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite, "Broadcast Activated Time");
		TextRender_string24(Lv.Grid.Left[2] + 60, Lv.Grid.Top[2] +40, clNearWhite, "Second");
	}
}

void TBroadcastCfg::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);
	IList->Edit[0].Init(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +36,42,30,0,0,1,clNearWhite);
	IList->Edit[1].Init(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +36,42,30,0,0,1,clNearWhite);
	for(int i=0; i<2;i++){
		IList->Edit[i].Enable = 1;
		IList->Edit[i].Visible = 1;
		IList->Edit[i].SetMaxLen(3);
		IList->Edit[i].SetInputMaskEn();
		IList->Edit[i].Str.FromStr("001");
		IList->Edit[i].SetPstTail();
	}
	DrawFixText();
}

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

void TBroadcastCfg::ShowSta()
{
	if(LanguageEnCn==0){
		if(Sta == 0){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "          ");
		}else
		if(Sta == 1){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "保存成功  ");
		}else
		if(Sta == -1){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "保存失败  ");
		}
	}else{
		if(Sta == 0){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "              ");
		}else
		if(Sta == 1){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "Saved OK      ");
		}else
		if(Sta == -1){
			TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "Failed To Save");
		}
	}
}

void TBroadcastCfg::ShowSta(int aSta)
{
	Sta = aSta;
	ShowSta();
}

void TBroadcastCfg::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
		ShowSta();
	}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);


		ShowSyncMode();
		IList->Edit[0].Show();
		IList->Edit[1].Show();
		DrawFixText();
		Check4Selected();
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}


//______________________________________________________________________________________________________________________________
void TBroadcastCfg::ClearSelected(void)
{
	switch(CtlIndex){
		case 0:
			TRoundSquare::sSelected(Lv.Grid.Left[0] +10, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 1:
			TRoundSquare::sSelected(Lv.Grid.Left[0] +160, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 2:
			IList->Edit[0].Selected = 0;
			IList->Edit[0].Show();
			break;
		case 3:
			IList->Edit[1].Selected = 0;
			IList->Edit[1].Show();
			break;
		default:

			break;
	}	
}

void TBroadcastCfg::Check4Selected(void)
{
	switch(CtlIndex){
		case 0:
			TRoundSquare::sSelected(Lv.Grid.Left[0] +10, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 1:
			TRoundSquare::sSelected(Lv.Grid.Left[0] +160, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 2:
			IList->Edit[0].Selected = 1;
			IList->Edit[0].SetPstTail();
			IList->Edit[0].Show();
			break;
		case 3:
			IList->Edit[1].Selected = 1;
			IList->Edit[1].SetPstTail();
			IList->Edit[1].Show();
			break;
		default:

			break;
	}	
}

void TBroadcastCfg::ShowSyncMode()
{
	if(aBroadcast.SyncMode == 0){
		//并行
		TRoundSquare::sDrawBySize(Lv.Grid.Left[0] +10, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[0] +160, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}else{
		//交替模式
		TRoundSquare::sDrawBySize(Lv.Grid.Left[0] +10, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, dRoundR,  dWdDisabelClr, 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[0] +160, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, dRoundR,  dWdClr, 1);
	}
}

void TBroadcastCfg::PermissionCmdGO()
{
	if(PermissionCmd == 1){
		RestoreData();
		if(Broadcast.SyncMode == 0)InnerCan.SendCmdBroadcastFast(2);
		WriteSetting();
		ReadSetting();
		LoadData();
		
		if(	(aBroadcast.PadAddr 		== Broadcast.PadAddr) 		&&
			(aBroadcast.Addr 			== Broadcast.Addr)		&&
			(aBroadcast.AreaNum 		== Broadcast.AreaNum)		&&
			(aBroadcast.SyncMode 		== Broadcast.SyncMode)		&&
	
			(aBroadcast.BroadcastTime 	== Broadcast.BroadcastTime)	&&
			(aBroadcast.VaTime 		== Broadcast.VaTime) ) {
				ShowSta(1);		
				Record.OperateAdd_BroadcastVa(Broadcast.PadAddr, Broadcast.Addr, Broadcast.SyncMode);
		}else{
				ShowSta(-1);	
		}
	}
}

void TBroadcastCfg::PermissionCmdReturnDraw()
{
	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);
	DrawFixText();
	
	
	ShowSyncMode();
	IList->Edit[0].Show();
	IList->Edit[1].Show();
	Check4Selected();
}

//_____________________________________________________________________________________________________________________________

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

	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(CtlIndex == 0){
					if(aKey == VK_RIGHT){
						ClearSelected();
						CtlIndex = 1;
						Check4Selected();
					}
				}else
				if(CtlIndex == 1){
					if(aKey == VK_LEFT){
						ClearSelected();
						CtlIndex = 0;
						Check4Selected();
					}
				}else
				if(CtlIndex == 2){
					IList->Edit[0].KeyIn(aKey);
					IList->Edit[0].Show();
				}else
				if(CtlIndex == 3){
					IList->Edit[1].KeyIn(aKey);
					IList->Edit[1].Show();
				}
				break;
			case VK_UP:
				if(CtlIndex >0){
					ClearSelected();
					CtlIndex--;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(CtlIndex <3){
					ClearSelected();
					CtlIndex++;
					Check4Selected();
				}
				break;	
			case dfKEY_SETUP:		//0x70		//  Key value for SETUP
				if( (CtlIndex == 0) || (CtlIndex == 1) ){
					aBroadcast.SyncMode = aBroadcast.SyncMode ? 0 : 0xA5;
					ShowSyncMode();
				}	
				break;
			case VK_EXECUTE:
				//Save Data Here
				PermissionCmd = 1;
				if(SystemPermission > 1){
					PermissionCmdGO();
				}else{
					IsPermission = 1;
					ClearSelected();
					PmsIntervene4Gui.Show4Gui(Lv.Grid.Left[2] + 200, Lv.Grid.Top[2] + 16, 400, 140, 2);
				}
				break;
			case VK_RETURN:
				aMsg = guiMsgReturn;
				break;
			default :
				break;
		}
		
		if(VK_EXECUTE != aKey){
			ShowSta(0);
		}
	}
	
	return aMsg;	
}

void TBroadcastCfg::InitPanel(void)
{
	LoadData();
	Check4Selected();
	ShowSyncMode();
}

void TBroadcastCfg::LoadData()
{
	aBroadcast.PadAddr 		= 0;//Broadcast.PadAddr;
	aBroadcast.Addr 			= 0;//Broadcast.Addr;
	aBroadcast.AreaNum 		= 0;//Broadcast.AreaNum;
	aBroadcast.SyncMode 		= Broadcast.SyncMode;
	
	aBroadcast.BroadcastTime 	= Broadcast.BroadcastTime;
	aBroadcast.VaTime 		= Broadcast.VaTime;
	
	IList->Edit[0].Str.FromUInt3Dg(aBroadcast.VaTime);
	IList->Edit[1].Str.FromUInt3Dg(aBroadcast.BroadcastTime);
	
	IList->Edit[0].SetPstTail();
	IList->Edit[1].SetPstTail();
	
	
	IList->Edit[0].Show();
	IList->Edit[1].Show();
}

void TBroadcastCfg::RestoreData()
{
	int iVal;
	Broadcast.PadAddr 	=aBroadcast.PadAddr;
	Broadcast.Addr 		=aBroadcast.Addr;
	Broadcast.AreaNum 	=aBroadcast.AreaNum ;
	
	Broadcast.SyncMode 	=aBroadcast.SyncMode;
	
	iVal = IList->Edit[0].Str.ToInteger();
	if(iVal <0) iVal = 1;
	if(iVal > 999) iVal = 999;
	aBroadcast.VaTime = iVal;
	
	iVal = IList->Edit[1].Str.ToInteger();
	if(iVal <0) iVal = 1;
	if(iVal > 999) iVal = 999;
	aBroadcast.BroadcastTime = iVal;
	
	Broadcast.BroadcastTime =aBroadcast.BroadcastTime;
	Broadcast.VaTime 		=aBroadcast.VaTime;	
}

void TBroadcastCfg::ReadSetting()
{
	unsigned int tAddr;
	unsigned char tData[8];
	//Read ExistTable 
	tAddr = dEepAddrBroadcastSetting +0;
	sDelayMs(10);
	eeprom_read_byte(0, tAddr, tData, 8);
	
	Broadcast.PadAddr = tData[0];
	Broadcast.Addr	= tData[1];
	Broadcast.AreaNum =  tData[2];
	Broadcast.SyncMode =  tData[3];
	if(Broadcast.SyncMode != 0xA5)Broadcast.SyncMode =0;
	Broadcast.BroadcastTime = tData[5];
	Broadcast.BroadcastTime <<= 8;
	Broadcast.BroadcastTime |= tData[4];
	Broadcast.VaTime = tData[7];
	Broadcast.VaTime <<= 8;
	Broadcast.VaTime |= tData[6];
}

void TBroadcastCfg::WriteSetting()
{
	unsigned int tAddr;
	unsigned char tData[8];
	
	tData[0] = Broadcast.PadAddr;
	tData[1] = Broadcast.Addr;
	tData[2] = Broadcast.AreaNum;
	tData[3] = Broadcast.SyncMode ;
	tData[4] = static_cast<unsigned char>(Broadcast.BroadcastTime & 0x00FF);
	tData[5] = static_cast<unsigned char>((Broadcast.BroadcastTime >> 8) & 0x00FF);
	tData[6] = static_cast<unsigned char>(Broadcast.VaTime & 0x00FF);
	tData[7] = static_cast<unsigned char>((Broadcast.VaTime >> 8) & 0x00FF);
	
	tAddr = dEepAddrBroadcastSetting + 0;
	sDelayMs(10);
	eeprom_write_byte_via_page(0, tAddr, tData, 8);
	sDelayMs(10);	
}

void TBroadcastCfg::sRecoverSetDefault()
{
	unsigned int tAddr;
	unsigned char tData[8];
	
	tData[0] = Broadcast.PadAddr;
	tData[1] = Broadcast.Addr;
	tData[2] = Broadcast.AreaNum;
	tData[3] = Broadcast.SyncMode ;
	tData[4] = 30;	//static_cast<unsigned char>(Broadcast.BroadcastTime & 0x00FF);
	tData[5] = 0;	//static_cast<unsigned char>((Broadcast.BroadcastTime >> 8) & 0x00FF);
	tData[6] = 30;	//static_cast<unsigned char>(Broadcast.VaTime & 0x00FF);
	tData[7] = 0;	//static_cast<unsigned char>((Broadcast.VaTime >> 8) & 0x00FF);
	
	tAddr = dEepAddrBroadcastSetting + 0;
	sDelayMs(10);
	eeprom_write_byte_via_page(0, tAddr, tData, 8);
	sDelayMs(10);

	ReadSetting	();
}







