#include "PermissionSet.h"
#include "XBox.h"
#include "Runtime.h"


void TPermissionSet::Init(int x1, int y1, int aW, int aH, int OwnerX, int OwnerY, u32 aColor)
{
	#define CH	30	//Caption Height
	unsigned short Left,Right, Top, Bottom, Width, Height, i;
	Bound.SetBySize(OwnerX + x1, OwnerY + y1, aW, aH);
	Left = Bound.Left +3;
	Top = Bound.Top+3;
	Width = aW - 7;
	Height = aH - 7;
	Right = Bound.Right -4;
	Bottom = Bound.Bottom -4;
	BoundCaption.SetBySize(Left,Top,Width, CH);
	Content.Set(Left, Top+CH, Right, Bottom);
	
	BoxBound.Set(Left + 200, Top+70, Right -200, Bottom -70);
	
	
	#undef CH
}

void TPermissionSet::DrawSelf(void)
{
	unsigned int x, y, x2, y2, w, h;
	
	VertLineRender(Bound.Left,		Bound.Top, 			Bound.Height -1,			0xFFFFFFFF);
	VertLineRender(Bound.Right -1,	Bound.Top +1,		Bound.Height -2,			0xFF808080);
	VertLineRender(Bound.Right,		Bound.Top,			Bound.Height,			0xFF404040);
	
	HorizLineRender(Bound.Left, 		Bound.Top, 			Bound.Width - 1, 			0xFFFFFFFF);
	HorizLineRender(Bound.Left +1, 	Bound.Bottom -1,  	Bound.Width - 2, 			0xFF808080);
	HorizLineRender(Bound.Left,		Bound.Bottom,		Bound.Width,			0xFF404040);
	
	x = Bound.Left +2 ;	
	x2 = Bound.Right -2;
	y = Bound.Top +2;
	y2 = Bound.Bottom - 2;
	w = Bound.Width -4;
	h = Bound.Height - 4;
	VertLineRender	(x,		y, 		h-1,	0xFF808080);
	HorizLineRender	(x,		y, 		w-1,	0xFF808080);
		
	VertLineRender	(x+1,		y+1, 		h-2,	0xFF404040);
	HorizLineRender	(x+1,		y+1, 		w-2,	0xFF404040);
	
	VertLineRender	(x2-1,	y+1, 		h-2,	0xFFD4D0C8);
	HorizLineRender	(x+1,		y2-1, 	w-2,	0xFFD4D0C8);

	VertLineRender	(x2,		y, 	  	h,	0xFFFFFFFF);
	HorizLineRender	(x,		y2, 		w,	0xFFFFFFFF);
	
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, clFrmFace);
	
	RectFillRender(BoundCaption.Left, BoundCaption.Top, BoundCaption.Right, BoundCaption.Bottom, clMaroon);
	TextRender_string24(BoundCaption.Left+6, BoundCaption.Top +3, clNearWhite, clMaroon, "II III 级权限允许项设定  按确定键保存并退出"); 
}


void TPermissionSet::Show(void)
{
	DrawSelf();
	DrawBox();
	ReDrawItems();
	DrawWidget();
}

void TPermissionSet::ReDrawItems()
{
	int x,y;
	unsigned int aClr;
	x = BoxBound.Left + 24 + 20;
	y = BoxBound.Top + 36 ;
	
	TextRender_string24(x, y, clNearBlack, clFrmFace, "I 查询信息"); 
	y = y + 36;
	TextRender_string24(x, y, clNearBlack, clFrmFace, "I 消除控制器的声信号");
	y = y + 36;
	TextRender_string24(x, y, clNearBlack, clFrmFace, "I 检查"); 
	y = y + 36;
	TextRender_string24(x, y, clNearBlack, clFrmFace, "I 信息确认");
	y = y + 36; 
	TextRender_string24(x, y, clNearBlack, clFrmFace, "I 联动启动");
	
	if(SystemPermission < 3)aClr = clSilver;
	else if(SystemPermission == 3)aClr = clNearBlack;
	y = y + 36;
	TextRender_string24(x, y, aClr, clFrmFace, "II 屏蔽和解除屏蔽"); 

}

void TPermissionSet::DrawWidget()
{
	int x,y,R;
	x = BoxBound.Left + 24;
	y = BoxBound.Top + 36 +12;
	R = 10;
	
	#define CLR_SEL	clBlue
	#define CLR_ACT	clGreen
	
	if(FocusIndex == 0){
		TCircle::sRender(x, y, R+2, CLR_SEL, 0);
		TCircle::sRender(x, y, R+3, CLR_SEL, 0);
		TCircle::sRender(x, y, R+4, CLR_SEL, 0);
	}else{
		TCircle::sRender(x, y, R+2, clFrmFace, 0);
		TCircle::sRender(x, y, R+3, clFrmFace, 0);
		TCircle::sRender(x, y, R+4, clFrmFace, 0);
	}
	TCircle::sRender(x, y, R, clGray, 0);
	if(eValue[0]){
		TCircle::sRender(x, y, R-1, CLR_ACT, 1);
	}else{
		TCircle::sRender(x, y, R-1, clFrmFace, 1);
	}
	
	y = y+36;
	if(FocusIndex == 1){
		TCircle::sRender(x, y, R+2, CLR_SEL, 0);
		TCircle::sRender(x, y, R+3, CLR_SEL, 0);
		TCircle::sRender(x, y, R+4, CLR_SEL, 0);
	}else{
		TCircle::sRender(x, y, R+2, clFrmFace, 0);
		TCircle::sRender(x, y, R+3, clFrmFace, 0);
		TCircle::sRender(x, y, R+4, clFrmFace, 0);
	}
	TCircle::sRender(x, y, R, clGray, 0);
	if(eValue[1]){
		TCircle::sRender(x, y, R-1, CLR_ACT, 1);
	}else{
		TCircle::sRender(x, y, R-1, clFrmFace, 1);
	}
	
	y = y+36;
	if(FocusIndex == 2){
		TCircle::sRender(x, y, R+2, CLR_SEL, 0);
		TCircle::sRender(x, y, R+3, CLR_SEL, 0);
		TCircle::sRender(x, y, R+4, CLR_SEL, 0);
	}else{
		TCircle::sRender(x, y, R+2, clFrmFace, 0);
		TCircle::sRender(x, y, R+3, clFrmFace, 0);
		TCircle::sRender(x, y, R+4, clFrmFace, 0);
	}
	TCircle::sRender(x, y, R, clGray, 0);
	if(eValue[2]){
		TCircle::sRender(x, y, R-1, CLR_ACT, 1);
	}else{
		TCircle::sRender(x, y, R-1, clFrmFace, 1);
	}
	
	y = y+36;
	if(FocusIndex == 3){
		TCircle::sRender(x, y, R+2, CLR_SEL, 0);
		TCircle::sRender(x, y, R+3, CLR_SEL, 0);
		TCircle::sRender(x, y, R+4, CLR_SEL, 0);
	}else{
		TCircle::sRender(x, y, R+2, clFrmFace, 0);
		TCircle::sRender(x, y, R+3, clFrmFace, 0);
		TCircle::sRender(x, y, R+4, clFrmFace, 0);
	}
	TCircle::sRender(x, y, R, clGray, 0);
	if(eValue[3]){
		TCircle::sRender(x, y, R-1, CLR_ACT, 1);
	}else{
		TCircle::sRender(x, y, R-1, clFrmFace, 1);
	}
	
	y = y+36;
	if(FocusIndex == 4){
		TCircle::sRender(x, y, R+2, CLR_SEL, 0);
		TCircle::sRender(x, y, R+3, CLR_SEL, 0);
		TCircle::sRender(x, y, R+4, CLR_SEL, 0);
	}else{
		TCircle::sRender(x, y, R+2, clFrmFace, 0);
		TCircle::sRender(x, y, R+3, clFrmFace, 0);
		TCircle::sRender(x, y, R+4, clFrmFace, 0);
	}
	TCircle::sRender(x, y, R, clGray, 0);
	if(eValue[4]){
		TCircle::sRender(x, y, R-1, CLR_ACT, 1);
	}else{
		TCircle::sRender(x, y, R-1, clFrmFace, 1);
	}
	
	y = y+36;
	if(FocusIndex == 5){
		TCircle::sRender(x, y, R+2, CLR_SEL, 0);
		TCircle::sRender(x, y, R+3, CLR_SEL, 0);
		TCircle::sRender(x, y, R+4, CLR_SEL, 0);
	}else{
		TCircle::sRender(x, y, R+2, clFrmFace, 0);
		TCircle::sRender(x, y, R+3, clFrmFace, 0);
		TCircle::sRender(x, y, R+4, clFrmFace, 0);
	}
	TCircle::sRender(x, y, R, clGray, 0);
	if(eValue[5]){
		TCircle::sRender(x, y, R-1, CLR_ACT, 1);
	}else{
		TCircle::sRender(x, y, R-1, clFrmFace, 1);
	}
}

void TPermissionSet::DrawBox(void)
{
	int aLen, aW;
	aLen = 10;
	aW = aLen * 12 +12;
	
	RectFillRender(BoxBound.Left, 	BoxBound.Top, BoxBound.Right, BoxBound.Bottom, clFrmFace);
	
	VertLineRender(BoxBound.Left,		BoxBound.Top+12,		BoxBound.Height-12,	clGray);
	VertLineRender(BoxBound.Left+1,	BoxBound.Top+12,		BoxBound.Height-12,	clGray);
	VertLineRender(BoxBound.Right,	BoxBound.Top+12,		BoxBound.Height-12,	clGray);
	VertLineRender(BoxBound.Right-1,	BoxBound.Top+12,		BoxBound.Height-12,	clGray);
	
	HorizLineRender(BoxBound.Left, 	BoxBound.Top+12, 		6, 	clGray);
	HorizLineRender(BoxBound.Left, 	BoxBound.Top+12+1, 	6, 	clGray);
	HorizLineRender(BoxBound.Left, 	BoxBound.Bottom, 		BoxBound.Width, 	clGray);
	HorizLineRender(BoxBound.Left, 	BoxBound.Bottom -1, 	BoxBound.Width, 	clGray);
	
	HorizLineRender(BoxBound.Left + aW +6, 	BoxBound.Top+12, 		BoxBound.Width -6 -aW, 	clGray);
	HorizLineRender(BoxBound.Left + aW +6, 	BoxBound.Top+12+1, 	BoxBound.Width -6 -aW, 	clGray);
	
	TextRender_string24(BoxBound.Left + 8, BoxBound.Top, clNearBlack, clFrmFace, "允许项设定");
	
	ReDrawItems();	
}

void TPermissionSet::ChangeWg(void)
{
	switch(FocusIndex){
		case 0: 	if(eValue[0]){
					eValue[0] = 0;
				}else{
					eValue[0] = 0xA5;
				}
				break;
		case 1: 	if(eValue[1]){
					eValue[1] = 0;
				}else{
					eValue[1] = 0xA5;
				}
				break;
		case 2: 	if(eValue[2]){
					eValue[2] = 0;
				}else{
					eValue[2] = 0xA5;
				}
				break;
		case 3: 	if(eValue[3]){
					eValue[3] = 0;
				}else{
					eValue[3] = 0xA5;
				}
				break;
		case 4: 	if(eValue[4]){
					eValue[4] = 0;
				}else{
					eValue[4] = 0xA5;
				}
				break;
		case 5: 	if(eValue[5]){
					eValue[5]= 0;
				}else{
					eValue[5] = 0xA5;
				}
				break;
	}					
}

void TPermissionSet::LoadSysCfg()
{
	eValue[0] = PermissionDt.QueryInf;
	eValue[1] = PermissionDt.ClearCtlVoice;
	eValue[2] = PermissionDt.Check;
	eValue[3] = PermissionDt.InfReadConfirm;
	eValue[4] = PermissionDt.LinkageActive;
	eValue[5] = PermissionDt.MaskAndUnMask;
}

TGuiMsgReturn TPermissionSet::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aPa;

	if(aKey == VK_LEFT){
		ChangeWg();
		DrawWidget();
	}else
	if(aKey == VK_RIGHT){
		ChangeWg();
		DrawWidget();
	}else
	if(aKey == VK_UP){
		if(FocusIndex >0){
			FocusIndex--;
			DrawWidget();
		}
	}else
	if(aKey == VK_DOWN){
		if(SystemPermission == 2){
			if(FocusIndex <4){
				FocusIndex++;
				DrawWidget();
			}
		}else
		if(SystemPermission == 3){
			if(FocusIndex <5){
				FocusIndex++;
				DrawWidget();
			}
		}
	}else
	if(aKey == VK_EXECUTE){
		PermissionDt.QueryInf 		= eValue[0];
		PermissionDt.ClearCtlVoice	= eValue[1];
		PermissionDt.Check		= eValue[2];
		PermissionDt.InfReadConfirm	= eValue[3];
		PermissionDt.LinkageActive	= eValue[4];
		PermissionDt.MaskAndUnMask	= eValue[5];
		
		aPa = 0;
		if(eValue[0])aPa |= 0x01;
		if(eValue[1])aPa |= 0x02;
		if(eValue[2])aPa |= 0x04;
		if(eValue[3])aPa |= 0x08;
		if(eValue[4])aPa |= 0x10;
		if(eValue[5])aPa |= 0x20;
		Record.OperateAdd_CfgPermissionAllow(aPa);
		
		aMsg = guiMsgReturn;
	}else
	if(aKey == VK_RETURN){
		aMsg = guiMsgReturn;
	}
	return aMsg;
}








