#include "DialogStartup.h"
#include "Runtime.h"

extern "C"{
	#include "HW_Config.h"
}

unsigned int startUpPwr;

void TDialogStartup::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);
	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);
	
	Caption.Visible = 0;
	for(i=0;i<1;i++){
		Edit[i].Enable = 0;
		Edit[i].Visible = 0;
		Edit[i].Selected = 0;
	}
	
	GoSkip = 0;
}


void TDialogStartup::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, clFrmFace);
	
	if(Caption.Visible)Caption.Show();
	for(i=0; i<1; i++)if(Edit[i].Visible)Edit[i].Show();
}

void TDialogStartup::Show(void)
{
	Init(150, 300, 500, 150, 0, 0, clFrmFace, bvRaised);
	Caption.Visible = 1;
	if(LanguageEnCn==0)Caption.SetText("ÇëÊäÈë¿ª»úÃÜÂë", 24);
	else Caption.SetText("Enter Start Up Password", 24);
	Caption.TextColor = clNearWhite;
	Edit[0].Init(30, 70, 446, 32, 150, 300, 2 , clFrmFace);
	DrawSelf();
	Edit[0].TextClear();
	Edit[0].Enable = 1;
	Edit[0].Selected = 1;
	Edit[0].SetMaxLen(16);
	Edit[0].SetTextShowMaskByAsterisk();
	Edit[0].Show();
}

void TDialogStartup::ShowSeries(char *pData)
{
	//MaxLen 24
	if(LanguageEnCn==0){
		TextRender_string24(Edit[0].Bound.Left+6, 	Edit[0].Bound.Top -28, clNearBlack, clFrmFace, 	"ÐòÁÐºÅ:");
		TextRender_string24(Edit[0].Bound.Left+6+84, 	Edit[0].Bound.Top -28, clNearBlack, clFrmFace, pData);
	}else{
		TextRender_string24(Edit[0].Bound.Left+6, 	Edit[0].Bound.Top -28, clNearBlack, clFrmFace, 	"Serial Num:");
		TextRender_string24(Edit[0].Bound.Left+6+132, 	Edit[0].Bound.Top -28, clNearBlack, clFrmFace, pData);
	}
}

int TDialogStartup::CheckToShow()
{
	//Check Memory if OK, return 0 
	//Fail, return 1 to Show 
	if(check_open_password_isenable()){
		return 1;
	}else{
		return 0;
	}
}

int TDialogStartup::CheckSkip()
{
	if(GoSkip)return 1;
	return 0;
}
int TDialogStartup::CheckPasswd()
{
	if(6 == Edit[0].Str.GetLength()){
		startUpPwr = Edit[0].Str.ToInteger();
		if(check_open_password(startUpPwr)){
			set_open_password_isover();
			return 1;//1 is succed
		}else{
			return 0;
		}
	}
	return 0;
}

void TDialogStartup::PasswdErrShow()
{
	if(LanguageEnCn==0)Caption.SetText("ÃÜÂë´íÎó !", 24);
	else Caption.SetText("Incorrect Password", 24);
	Caption.TextColor = clRed;
	Caption.Show();
}

TGuiMsgReturn TDialogStartup::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aGMR = guiMsgNone;
	Edit[0].KeyIn(aKey);
	Edit[0].Show();
	if(aKey == VK_EXECUTE){
		if(CheckPasswd()){
			GoSkip = 1;
		}else{
			PasswdErrShow();
		}
	}
	if(aKey == VK_RETURN)aGMR = guiMsgReturn;
	return aGMR;
}


