#include "PmsIntervene.h"
#include "Runtime.h"
#include "server.h"

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


static const unsigned char SavePassWord[32] = "9910232250266023\0";


void TPmsIntervene::Init(int aX, int aY, int aW, int aH, int OwnerX, int OwnerY, u32 aColor, TBvStyle aBevelOuter){
	unsigned short Left,Right,Top,Bottom,i;
	Bound.SetBySize(aX+OwnerX, aY+OwnerY, aW, aH);
	Color = aColor;
	BevelOuter = aBevelOuter;
	if(BevelOuter == bvRaised){	
		Left 		= Bound.Left + 1;
		Right 	= Bound.Right - 2;
		Top 		= Bound.Top + 1;
		Bottom 	= Bound.Bottom - 2;
	}else{
		Left 		= Bound.Left + 2;
		Right 	= Bound.Right - 2;
		Top 		= Bound.Top + 2;
		Bottom 	= Bound.Bottom - 2;
	}
	
	Caption.Init(Left, Top, Right-Left+1, 32, 0, 0, clNearWhite, clMedBlue);
	Content.Set(Left,Top+32,Right,Bottom);

	TextColor = clNearBlack;
	SelectedColor = clBlue;
	SelectedTextColor = clNearWhite;
	
	Caption.Visible = 0;

	
	Edit.Visible = 0;
	Edit.SetMaxLen(16);
	Edit.SetTextShowMaskByAsterisk();
	
	STextDown.Visible = 1;
	
	CountDownTick =0;
}

void TPmsIntervene::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 TPmsIntervene::DrawSelf(void)
{
	unsigned int i;
	if (BevelOuter == bvLowered){
		VertLineRender(Bound.Left,	Bound.Top, 	Bound.Height-1,	0xFF808080);
		HorizLineRender(Bound.Left,	Bound.Top, 	Bound.Width-1,	0xFF808080);
		
		VertLineRender(Bound.Left+1,	Bound.Top+1, 	Bound.Height-2,	0xFF404040);
		HorizLineRender(Bound.Left+1,	Bound.Top+1, 	Bound.Width-2,	0xFF404040);
	
		VertLineRender(Bound.Right-1,	Bound.Top+1, 	Bound.Height-2,	0xFFD4D0C8);
		HorizLineRender(Bound.Left+1,	Bound.Bottom-1, 	Bound.Width-2,	0xFFD4D0C8);

		VertLineRender(Bound.Right,	Bound.Top, 	  Bound.Height,	0xFFFFFFFF);
		HorizLineRender(Bound.Left,	Bound.Bottom, Bound.Width,	0xFFFFFFFF);	
	}else
	if(BevelOuter == bvRaised){
		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);
	}
	
	//Fill Rect Box
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);
	
	RectFillRender(Bound.Left-6, Bound.Top-8, Bound.Left-1,  Bound.Bottom+6, clGray);
	RectFillRender(Bound.Left-6, Bound.Top-8, Bound.Right+6,  Bound.Top-1, clGray);
	RectFillRender(Bound.Right+1, Bound.Top-8, Bound.Right+6,  Bound.Bottom+6, clGray);
	RectFillRender(Bound.Left-6, Bound.Bottom+1, Bound.Right+6,  Bound.Bottom+6, clGray);
	
	//if(Caption.Visible)Caption.Show();
}

void TPmsIntervene::Show(void)
{
	DrawSelf();
}

void TPmsIntervene::Show4Reset()
{
	Init(180, 110, 440, 120, 0, 0, clFrmFace, bvRaised);
	Caption.Visible = 1;
	if(LanguageEnCn==0)Caption.SetText("执行复位,请输入II级或III级密码", 24);
	else Caption.SetText("Reset,Level 2 or 3 Permission", 24);
	STextDown.Init(Content.Left+ 20, Content.Top + 50,  Content.Width - 40, 30, 0,0,2,clFrmFace);
	if(LanguageEnCn==0)STextDown.SetText("输入密码按确认键执行,或返回", 24);
	else STextDown.SetText("Input Password", 24);
	Caption.TextColor = clNearWhite;
	//BackScreen();
	Edit.Init(Content.Left+ 20, Content.Top + 10, Content.Width - 40, 32, 0, 0, 2 , clFrmFace);
	Edit.Str.Clear();
	Edit.SetMaxLen(16);
	Edit.SetTextShowMaskByAsterisk();
	Edit.Pst = -1;
	Edit.SetSelect();
	
	DrawSelf();	
	Caption.Show();
	Edit.Show();
	STextDown.Show();
	IsShow = 1;
	GoWhat = 1;
	CountDownTick =15;
	
	IsHotKeyCheckingPermission =1;
}

void TPmsIntervene::Show4Alram()
{
	DelayMs(50);
	
	Init(180, 110, 440, 120, 0, 0, clFrmFace, bvRaised);
	Caption.Visible = 1;
	if(LanguageEnCn==0)Caption.SetText("启停声光警报,请输入II级或III级密码", 24);
	else Caption.SetText("Acoustooptic,Level 2 or 3 Permission", 24);
	STextDown.Init(Content.Left+ 20, Content.Top + 50,  Content.Width - 40, 30, 0,0,2,clFrmFace);
	if(LanguageEnCn==0)STextDown.SetText("输入密码按确认键执行,或返回", 24);
	else STextDown.SetText("Input Password", 24);
	Caption.TextColor = clNearWhite;
	//BackScreen();
	Edit.Init(Content.Left+ 20, Content.Top + 10, Content.Width - 40, 32, 0, 0, 2 , clFrmFace);
	Edit.Str.Clear();
	Edit.SetMaxLen(16);
	Edit.SetTextShowMaskByAsterisk();
	Edit.Pst = -1;
	Edit.SetSelect();
	
	DrawSelf();	
	Caption.Show();
	Edit.Show();
	STextDown.Show();
	IsShow =1;
	GoWhat = 2;
	CountDownTick =15;
	
	IsHotKeyCheckingPermission = 1;
}

void TPmsIntervene::Show4SelfCheck()
{
	Init(180, 110, 440, 120, 0, 0, clFrmFace, bvRaised);
	Caption.Visible = 1;
	if(LanguageEnCn==0)Caption.SetText("自检,请输入II级或III级密码", 24);
	else Caption.SetText("Self-Check,Level 2 or 3 Permission", 24);
	STextDown.Init(Content.Left+ 20, Content.Top + 50,  Content.Width - 40, 30, 0,0,2,clFrmFace);
	if(LanguageEnCn==0)STextDown.SetText("输入密码按确认键执行,或返回", 24);
	else STextDown.SetText("Input Password", 24);
	Caption.TextColor = clNearWhite;
	//BackScreen();
	Edit.Init(Content.Left+ 20, Content.Top + 10, Content.Width - 40, 32, 0, 0, 2 , clFrmFace);
	Edit.Str.Clear();
	Edit.SetMaxLen(16);
	Edit.SetTextShowMaskByAsterisk();
	Edit.Pst = -1;
	Edit.SetSelect();
	
	DrawSelf();	
	Caption.Show();
	Edit.Show();
	STextDown.Show();
	
	IsShow =1;
	GoWhat = 3;
	CountDownTick =15;
	
	IsHotKeyCheckingPermission = 1;
}

void TPmsIntervene::Show4Gui(int aX, int aY, int aW, int aH, int aReqLevel)
{
	DelayMs(50);
	
	Init(aX, aY, aW, aH, 0, 0, clFrmFace, bvRaised);
	Caption.Visible = 1;
	if(aReqLevel == 2){
		if(LanguageEnCn==0)Caption.SetText("请输入II级或III级密码", 24);
		else Caption.SetText("Level 2 or 3 Permission", 24);
	}else{
		if(LanguageEnCn==0)Caption.SetText("请输入III级密码      ", 24);
		else Caption.SetText("Level 3 Permission      ", 24);
	}
	STextDown.Init(Content.Left+ 20, Content.Top + 50,  Content.Width - 40, 30, 0,0,2,clFrmFace);
	if(LanguageEnCn==0)STextDown.SetText("输入密码按确认键执行,或返回", 24);
	else STextDown.SetText("Input Password", 24);
	Caption.TextColor = clNearWhite;
	//BackScreen();
	Edit.Init(Content.Left+ 20, Content.Top + 10, Content.Width - 80, 32, 0, 0, 2 , clFrmFace);
	Edit.Str.Clear();
	Edit.SetMaxLen(16);
	Edit.SetTextShowMaskByAsterisk();
	Edit.Pst = -1;
	Edit.SetSelect();
	
	DrawSelf();	
	Caption.Show();
	Edit.Show();
	STextDown.Show();
	IsShow =1;
	GoWhat = 4;
	ReqLevel = aReqLevel;
	CountDownTick =15;
}

void TPmsIntervene::ReDraw4GuiRecall()
{
	DelayMs(50);

	DrawSelf();	
	Caption.Show();
	Edit.Show();
	STextDown.Show();
	CountDownTick =15;
}

void TPmsIntervene::CheckEnterLvl()
{
	unsigned int Len,i, Cok, Cing;
	Len = Edit.Str.GetLength();
	if(Len <6)return;
	if(Len >16)return;
	Cok = 0;
	Cing = 1;
	if(Len == CtlCenter.PassWordII.Len){
		for(i=0; i<Len; i++){
			if(Edit.Str.Text[i] != CtlCenter.PassWordII.Content[i]){
				Cing = 0;
			}
		}
		if(Cing){
			Cok = 1;
			SystemPermission = 2;
			SystemPermissionTick = 0;
		}
	}
	
	if(Cok)return;
	Cing = 1;
	if(Len == CtlCenter.PassWordIII.Len){
		for(i=0; i<Len; i++){
			if(Edit.Str.Text[i] != CtlCenter.PassWordIII.Content[i]){
				Cing = 0;
			}
		}
		if(Cing){
			Cok = 1;
			SystemPermission = 3;
			SystemPermissionTick = 0;
		}
	}
	
	if(Cok)return;
	if(Len == 16){
		Cok =0;
		for(i=0; i<16; i++){
			if(SavePassWord[i] == Edit.Str.Text[i])Cok++;
		}
		if(Cok == 16){
			SystemPermission = 3;
			SystemPermissionTick = 0;
		}
		return;
	}
}

void TPmsIntervene::CheckEnterLv2()
{
	unsigned int Len,i, Cok, Cing;
	Len = Edit.Str.GetLength();
	if(Len <6)return;
	if(Len >16)return;
	Cok = 0;
	Cing = 1;
	if(Len == CtlCenter.PassWordIII.Len){
		for(i=0; i<Len; i++){
			if(Edit.Str.Text[i] != CtlCenter.PassWordIII.Content[i]){
				Cing = 0;
			}
		}
		if(Cing){
			Cok = 1;
			SystemPermission = 3;
			SystemPermissionTick = 0;
		}
	}
	
	if(Cok)return;
	if(Len == 16){
		Cok =0;
		for(i=0; i<16; i++){
			if(SavePassWord[i] == Edit.Str.Text[i])Cok++;
		}
		if(Cok == 16){
			SystemPermission = 3;
			SystemPermissionTick = 0;
		}
		return;
	}
}

TGuiMsgReturn TPmsIntervene::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	int uKey, Len;
	unsigned char aIndex;
	char LastChr;
	const char *p;
	int i;
	
	switch(aKey){
		case VK_DELETE:
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':		
		case VK_LEFT:
		case VK_RIGHT:
			Edit.KeyIn(aKey);
			Edit.CorrectPst();
			Edit.Show();
			Edit.ShowCursor();
			CountDownTick = 15;
			break;
	}
	if(aKey == VK_EXECUTE){
		if(GoWhat == 1){
			CheckEnterLvl();
			if(SystemPermission >1){
				HotKeyResetCallBack();
				IsShow = 0;
				aMsg = guiMsgReturn;
			}else{
				if(LanguageEnCn==0)STextDown.SetText("密码错误 !", 24);
				else STextDown.SetText("Incorrect Password", 24);
				STextDown.TextColor = clRed;
				STextDown.Show();
				CountDownTick = 15;
			}
		}else
		if(GoWhat == 2){
			CheckEnterLvl();
			if(SystemPermission >1){
				RedrawScreen();
				HotKeyAlramCallBack();
				IsShow = 0;
				aMsg = guiMsgReturn;
			}else{
				if(LanguageEnCn==0)STextDown.SetText("密码错误 !", 24);
				else STextDown.SetText("Incorrect Password", 24);
				STextDown.TextColor = clRed;
				STextDown.Show();
				CountDownTick = 15;
			}
		}else
		if(GoWhat == 3){
			CheckEnterLvl();
			if(SystemPermission >1){
				HotKeySelfCheckCallBack();
				IsShow = 0;
				aMsg = guiMsgReturn;
			}else{
				if(LanguageEnCn==0)STextDown.SetText("密码错误 !", 24);
				else STextDown.SetText("Incorrect Password", 24);
				STextDown.TextColor = clRed;
				STextDown.Show();
				CountDownTick = 15;
			}
		}else
		if(GoWhat == 4){
			if(ReqLevel == 2){
				CheckEnterLvl();
				if(SystemPermission >1){
					IsShow = 0;
					aMsg = guiMsgOkAndReturn;
				}else{
					if(LanguageEnCn==0)STextDown.SetText("密码错误 !", 24);
					else STextDown.SetText("Incorrect Password", 24);
					STextDown.TextColor = clRed;
					STextDown.Show();
					CountDownTick = 15;
				}
			}else
			if(ReqLevel == 3){
				CheckEnterLv2();
				if(SystemPermission >2){
					IsShow = 0;
					aMsg = guiMsgOkAndReturn;
				}else{
					if(LanguageEnCn==0)STextDown.SetText("密码错误 !", 24);
					else STextDown.SetText("Incorrect Password", 24);
					STextDown.TextColor = clRed;
					STextDown.Show();
					CountDownTick = 15;
				}
			}
		}
	}else
	if(aKey == VK_RETURN){
		IsShow = 0;
		if(GoWhat < 4)
			RedrawScreen();
		aMsg = guiMsgReturn;
	}
	if( (aMsg == guiMsgReturn) || (aMsg == guiMsgOkAndReturn) ){
		IsHotKeyCheckingPermission = 0;
	}
	return aMsg;		
}

void TPmsIntervene::BackScreen()
{
	volatile unsigned int aAddrs, aAddrt;
	int x,y;
	aAddrs = LcdBaseAddr;
	aAddrt = dAddrSdRam_BackScreen;
	for(y=100; y<260; y++){
		for(x=170; x<630; x++){
			PxData[x-170] = *(volatile unsigned int *)(aAddrs + ((y*800 + x) *4));
		}
		for(x=170; x<630; x++){
			*(volatile unsigned int *)aAddrt = PxData[x-170];
			aAddrt+=4;
		}
	}
}

void TPmsIntervene::RedrawScreen()
{
	volatile unsigned int aAddr;
	int x,y;
	
	GuiRedraw(1);
	return;
	
	aAddr = dAddrSdRam_BackScreen;
	for(y=100; y<260; y++){
		for(x=170; x<630; x++){
			 PxData[x-170] = *(volatile unsigned int *)aAddr;
			 aAddr+=4;
		}
		for(x=170; x<630; x++){
			*(volatile unsigned int *)(LcdBaseAddr + ((y*800 + x) *4)) = PxData[x-170];
		}
	}
}

void TPmsIntervene::DrawTip()
{
	unsigned int aClr;
	if(CountDownTick & 0x01){
		aClr = clNearWhite;
	}else{
		aClr = clNearBlack;
	}
	RectFillRender(Bound.Left-6, Bound.Top-8, Bound.Left-1,  Bound.Bottom+6, aClr);
	RectFillRender(Bound.Left-6, Bound.Top-8, Bound.Right+6,  Bound.Top-1, aClr);
	RectFillRender(Bound.Right+1, Bound.Top-8, Bound.Right+6,  Bound.Bottom+6, aClr);
	RectFillRender(Bound.Left-6, Bound.Bottom+1, Bound.Right+6,  Bound.Bottom+6, aClr);
	if(CountDownTick < 10)
		TextDigitRender2_24(STextDown.Bound.Left+ 360, STextDown.Bound.Top+2, clNearBlack, clFrmFace, CountDownTick);
	else
		TextRender_string24(STextDown.Bound.Left+ 360, STextDown.Bound.Top+2, clNearBlack, clFrmFace, "  ");
}

void TPmsIntervene::Task1000Ms()
{
	if(IsShow == 0)return;
	if(CountDownTick){
		CountDownTick--;
		if(CountDownTick){
			DrawTip();
		}else{
			IsShow = 0;
			GoWhat = 0;
			RedrawScreen();
		}
	}
}

int TPmsIntervene::ExtRequst(unsigned char Prm)
{
	IsShow = 0;
	IsHotKeyCheckingPermission =0;
	return 0;
}











