#include "RuntimeData.h"
#include "PasswdChange.h"

#define TextTopOffSet	6
#define ListLineCnt	10

#define ipdWidth0  	400
#define ipdWidth1		390

void TPasswdChange::Init()
{
	StaII = 0;
	StaIII = 0;
	
	Tag.Reset();
}

void TPasswdChange::RenderBackGround(void)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);
}

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

void TPasswdChange::InitPanel(void)
{
	aPanel->Init(400,400,10,10,0,0,clFrmFace, bvRaised);
	
	aPanel->Edit[0].Init(Lv.Grid.Left[0] +242, Lv.Grid.Top[0] +10 -4, 210, 32, 0,0, 1, clTeal);
	aPanel->Edit[1].Init(Lv.Grid.Left[0] +242, Lv.Grid.Top[0] +46 -4, 210, 32, 0,0, 1, clTeal);
	aPanel->Edit[2].Init(Lv.Grid.Left[0] +242, Lv.Grid.Top[0] +82 -4, 210, 32, 0,0, 1, clTeal);
	
	aPanel->Edit[3].Init(Lv.Grid.Left[0] +242, Lv.Grid.Top[1] +10 -4, 210, 32, 0,0, 1, clTeal);
	aPanel->Edit[4].Init(Lv.Grid.Left[0] +242, Lv.Grid.Top[1] +46 -4, 210, 32, 0,0, 1, clTeal);
	aPanel->Edit[5].Init(Lv.Grid.Left[0] +242, Lv.Grid.Top[1] +82 -4, 210, 32, 0,0, 1, clTeal);
	
	for(int i=0; i<6; i++){
		aPanel->Edit[i].Str.Clear();
		aPanel->Edit[i].MaxLen = 16;
		aPanel->Edit[i].IsShowCursor = 1;
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].Pst = -1;
		aPanel->Edit[i].SetTextShowMaskByAsterisk();
	}

}

void TPasswdChange::DrawSelf(void)
{
	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(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "本机->修改密码");

		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +10, clNearWhite,  "二级权限");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +10, clNearWhite,  "三级权限");
		
		TextRender_string24(Lv.Grid.Left[0] +142, Lv.Grid.Top[0] +10, clNearWhite,  "原密码");
		TextRender_string24(Lv.Grid.Left[0] +142, Lv.Grid.Top[0] +46, clNearWhite,  "新密码");
		TextRender_string24(Lv.Grid.Left[0] +142, Lv.Grid.Top[0] +82, clNearWhite,  "新密码");
		
		//TRoundRect::sDrawBySize(Lv.Grid.Left[2] +10, Lv.Grid.Top[0] + 166, 100, 40, 7, clDeepBlue, "保存", 14, clNearWhite);
		
		
		TextRender_string24(Lv.Grid.Left[1] +142, Lv.Grid.Top[1] +10, clNearWhite,  "原密码");
		TextRender_string24(Lv.Grid.Left[1] +142, Lv.Grid.Top[1] +46, clNearWhite,  "新密码");
		TextRender_string24(Lv.Grid.Left[1] +142, Lv.Grid.Top[1] +82, clNearWhite, "新密码");
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite,  "Local->Change Password");

		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +10, clNearWhite,  "II  Key ");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +10, clNearWhite,  "III  Key ");
		
		TextRender_string24(Lv.Grid.Left[0] +142, Lv.Grid.Top[0] +10, clNearWhite,  "Original");
		TextRender_string24(Lv.Grid.Left[0] +142, Lv.Grid.Top[0] +46, clNearWhite,  "New");
		TextRender_string24(Lv.Grid.Left[0] +142, Lv.Grid.Top[0] +82, clNearWhite,  "confirm");
		
		//TRoundRect::sDrawBySize(Lv.Grid.Left[2] +10, Lv.Grid.Top[0] + 166, 100, 40, 7, clDeepBlue, "保存", 14, clNearWhite);
		
		
		TextRender_string24(Lv.Grid.Left[1] +142, Lv.Grid.Top[1] +10, clNearWhite,  "Original");
		TextRender_string24(Lv.Grid.Left[1] +142, Lv.Grid.Top[1] +46, clNearWhite,  "New");
		TextRender_string24(Lv.Grid.Left[1] +142, Lv.Grid.Top[1] +82, clNearWhite,  "confirm");
	}
	
	//TRoundRect::sDrawBySize(Lv.Grid.Left[2] +10, Lv.Grid.Top[1] + 166, 100, 40, 7, clDeepBlue, "保存", 14, clNearWhite);
	ShowSave_II();
	ShowSave_III();
	
	Check4Selected();
	aPanel->ReDrawItems();
}

void TPasswdChange::ShowSave_II()
{
	char *tText;
	if(LanguageEnCn==0)tText="保存"; else tText = "Save";
	if( (Tag.Group == 0) && (Tag.Inx0 == 3) ){
		TRoundRect::sDrawBySize(Lv.Grid.Left[0] +242, Lv.Grid.Top[0] + 126, 76, 36, 7, clDeepBlue, tText, 14, clNearWhite);
	}else{
		TRoundRect::sDrawBySize(Lv.Grid.Left[0] +242, Lv.Grid.Top[0] + 126, 76, 36, 7, clNearWhite, tText, 14, clNearBlack);
	}
}

void TPasswdChange::ShowSave_III()
{
	char *tText;
	if(LanguageEnCn==0)tText="保存"; else tText = "Save";
	if( (Tag.Group == 1) && (Tag.Inx1 == 3)  ){
		TRoundRect::sDrawBySize(Lv.Grid.Left[1] +242, Lv.Grid.Top[1] + 126, 76, 36, 7, clDeepBlue, tText, 14, clNearWhite);
	}else{
		TRoundRect::sDrawBySize(Lv.Grid.Left[1] +242, Lv.Grid.Top[1] + 126, 76, 36, 7, clNearWhite, tText, 14, clNearBlack);
	}
}

void TPasswdChange::Show(void)
{	
	RenderBackGround();
	DrawSelf();
}

void TPasswdChange::ShowStaII()
{
	if(LanguageEnCn==0){
		if(StaII == 0){
			TMyString::sFromStr("              ", 	Str.Text);
		}else
		if(StaII == 1){
			TMyString::sFromStr("更改成功\0", 		Str.Text);
		}else
		if(StaII == -1){
			TMyString::sFromStr("密码至少6位\0", 	Str.Text);
		}else
		if(StaII == -2){
			TMyString::sFromStr("密码至多12位\0", 	Str.Text);
		}else
		if(StaII == -3){
			TMyString::sFromStr("密码不等\0", 		Str.Text);
		}else
		if(StaII == -4){
			TMyString::sFromStr("记忆出错\0", 		Str.Text);
		}else
		if(StaII == -5){
			TMyString::sFromStr("输入内容出错\0",	Str.Text);
		}else
		if(StaII == -6){
			TMyString::sFromStr("原密码不符", 	Str.Text);
		}
	}else{
		if(StaII == 0){
			TMyString::sFromStr("              ", 	Str.Text);
		}else
		if(StaII == 1){
			TMyString::sFromStr("Changed OK    ", 	Str.Text);
		}else
		if(StaII == -1){
			TMyString::sFromStr("Error less 6  ", 	Str.Text);
		}else
		if(StaII == -2){
			TMyString::sFromStr("Error More 12 ", 	Str.Text);
		}else
		if(StaII == -3){
			TMyString::sFromStr("Error confirm ", 	Str.Text);
		}else
		if(StaII == -4){
			TMyString::sFromStr("Error Memory  ", 	Str.Text);
		}else
		if(StaII == -5){
			TMyString::sFromStr("Error Enter   ",	Str.Text);
		}else
		if(StaII == -6){
			TMyString::sFromStr("Error Original", 	Str.Text);
		}
	}		
	TextRender_string24(ResultTipPoint_II.x, ResultTipPoint_II.y, clNearWhite, ItemColor, Str.Text);
}

void TPasswdChange::ShowStaII(int aSta)
{
	StaII = aSta;
	ShowStaII();
}

void TPasswdChange::ShowStaIII()
{
	if(LanguageEnCn==0){
		if(StaIII == 0){
			TMyString::sFromStr("              ", 	Str.Text);
		}else
		if(StaIII == 1){
			TMyString::sFromStr("更改成功", 		Str.Text);
		}else
		if(StaIII == -1){
			TMyString::sFromStr("密码至少6位", 		Str.Text);
		}else
		if(StaIII == -2){
			TMyString::sFromStr("密码至多12位",		Str.Text);
		}else
		if(StaIII == -3){
			TMyString::sFromStr("密码不等", 		Str.Text);
		}else
		if(StaIII == -4){
			TMyString::sFromStr("记忆出错", 		Str.Text);
		}else
		if(StaIII == -5){
			TMyString::sFromStr("输入内容出错", 	Str.Text);
		}else
		if(StaIII == -6){
			TMyString::sFromStr("原密码不符", 	Str.Text);
		}
	}else{
		if(StaIII == 0){
			TMyString::sFromStr("              ", 	Str.Text);
		}else
		if(StaIII == 1){
			TMyString::sFromStr("Changed OK    ", 	Str.Text);
		}else
		if(StaIII == -1){
			TMyString::sFromStr("Error less 6  ", 	Str.Text);
		}else
		if(StaIII == -2){
			TMyString::sFromStr("Error More 12 ", 	Str.Text);
		}else
		if(StaIII == -3){
			TMyString::sFromStr("Error confirm ", 	Str.Text);
		}else
		if(StaIII == -4){
			TMyString::sFromStr("Error Memory  ", 	Str.Text);
		}else
		if(StaIII == -5){
			TMyString::sFromStr("Error Enter   ",	Str.Text);
		}else
		if(StaIII == -6){
			TMyString::sFromStr("Error Original", 	Str.Text);
		}
	}		
	TextRender_string24(ResultTipPoint_III.x, ResultTipPoint_III.y, clNearWhite, ItemColor, Str.Text);
}

void TPasswdChange::ShowStaIII(int aSta)
{
	StaIII = aSta;
	ShowStaIII();
}

void TPasswdChange::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);
		RenderBackGround();
		DrawSelf();
	}
}

//______________________________________________________________________________________________________________________________

void TPasswdChange::Check4Selected(void)
{
	for(int i=0; i<6; i++)
		aPanel->Edit[i].Selected = 0;

	if(Tag.Group == 0){
		switch(Tag.Inx0){
			case 0:
				aPanel->Edit[0].Selected = 1;
				break;
			case 1:
				aPanel->Edit[1].Selected = 1;
				break;
			case 2:
				aPanel->Edit[2].Selected = 1;
				break;
		}		
	}else
	if(Tag.Group == 1){
		switch(Tag.Inx1){
			case 0:
				aPanel->Edit[3].Selected = 1;
				break;
			case 1:
				aPanel->Edit[4].Selected = 1;
				break;
			case 2:
				aPanel->Edit[5].Selected = 1;
				break;
		}
	}
}

int TPasswdChange::CheckPrvPasswdOk_II()
{
	unsigned int Len,i, Cok, Cing;
	Len = aPanel->Edit[0].Str.GetLength();
	if(Len <6){ ShowStaII(-6); return -1;}
	if(Len >12){ ShowStaII(-6); return -1;}
	Cok = 0;
	Cing = 1;
	if(Len == CtlCenter.PassWordII.Len){
		for(i=0; i<Len; i++){
			if(aPanel->Edit[0].Str.Text[i] != CtlCenter.PassWordII.Content[i]){
				Cing = 0;
			}
		}
		if(Cing){
			Cok = 1;
			SystemPermission = 2;
		}
	}
	
	if(Cok)return 0;
	ShowStaII(-6); return -1;
}

int TPasswdChange::CheckPrvPasswdOk_III()
{
	unsigned int Len,i, Cok, Cing;
	Len = aPanel->Edit[3].Str.GetLength();
	if(Len <6){ ShowStaIII(-6); return -1;}
	if(Len >12){ ShowStaIII(-6); return -1;}
	Cok = 0;
	Cing = 1;
	if(Len == CtlCenter.PassWordIII.Len){
		for(i=0; i<Len; i++){
			if(aPanel->Edit[3].Str.Text[i] != CtlCenter.PassWordIII.Content[i]){
				Cing = 0;
			}
		}
		if(Cing){
			Cok = 1;
			SystemPermission = 3;
			SystemPermissionTick = 0;
		}
	}
	if(Cok)return 0;
	ShowStaIII(-6); return -1;
}

void TPasswdChange::CheckPasswordII()
{
	int Len0, Len1, i, IsEq, Wok, Rok;
	Len0 = aPanel->Edit[1].Str.GetLength();
	Len1 = aPanel->Edit[2].Str.GetLength();
	
	Wok = 0;
	Rok = 0;
	if(Len0 == Len1){
		if(Len0 <6){
			ShowStaII(-1);
			return;
		}else
		if(Len0 > 12){
			ShowStaII(-2);
			return;
		}
		IsEq = 1;
		for(i=0; i<Len0; i++){
			if(aPanel->Edit[1].Str.Text[i] != aPanel->Edit[2].Str.Text[i]){
				IsEq = 0;
			}
		}
		if(IsEq){
			for(i=0; i<16; i++)PassWord.Content[i] = '\0';
			for(i=0; i<Len0; i++){
				PassWord.Content[i] = aPanel->Edit[1].Str.Text[i] ;
			}
			Wok = CtlCenter.PassWordII_ReadWrite(0, &PassWord);
			if(Wok == 1){
				Rok = CtlCenter.PassWordII_ReadWrite(1, nullptr); 
				if(Rok){
					ShowStaII(1);
					Record.OperateAdd_PassWdModify(2);
				}else{
					ShowStaII(-4);
					Record.OperateAdd_PassWdModify(12);
				}
			}else
			if(Wok == -1){
				ShowStaII(-4);
				Record.OperateAdd_PassWdModify(12);
			}else{
				ShowStaII(-5);
				Record.OperateAdd_PassWdModify(12);
			}
		}else{
			ShowStaII(-3);
		}
	}else{
		ShowStaII(-3);
	}
}

void TPasswdChange::CheckPasswordIII()
{
	int Len0, Len1, i, IsEq, Wok, Rok;
	Len0 = aPanel->Edit[4].Str.GetLength();
	Len1 = aPanel->Edit[5].Str.GetLength();
	
	Wok = 0;
	Rok = 0;
	if(Len0 == Len1){
		if(Len0 <6){
			ShowStaIII(-1);
			return;
		}else
		if(Len0 > 12){
			ShowStaIII(-2);
			return;
		}
		IsEq = 1;
		for(i=0; i<Len0; i++){
			if(aPanel->Edit[4].Str.Text[i] != aPanel->Edit[5].Str.Text[i]){
				IsEq = 0;
			}
		}
		if(IsEq){
			for(i=0; i<16; i++)PassWord.Content[i] = '\0';
			for(i=0; i<Len0; i++){
				PassWord.Content[i] = aPanel->Edit[4].Str.Text[i] ;
			}
			Wok = CtlCenter.PassWordIII_ReadWrite(0, &PassWord);
			if(Wok == 1){
				Rok = CtlCenter.PassWordIII_ReadWrite(1, nullptr);
				if(Rok == 1){
					ShowStaIII(1);
					Record.OperateAdd_PassWdModify(3);
				}else{
					ShowStaIII(-4);
					Record.OperateAdd_PassWdModify(13);
				}
			}else
			if(Wok == -1){
				ShowStaIII(-4);
				Record.OperateAdd_PassWdModify(13);
			}else{
				ShowStaIII(-5);
				Record.OperateAdd_PassWdModify(13);
			}
		}else{
			ShowStaIII(-3);
		}
	}else{
		ShowStaIII(-3);
	}	
}

void TPasswdChange::sRecoverSetDefault()
{
	TPassWord tPw;
	int Wok, RetryTime, i;
	for(i=0; i<16; i++)tPw.Content[i] = '\0';
	for(i=0; i<6; i++){
		tPw.Content[i] = '1' + i;
	}
	Wok = CtlCenter.PassWordII_ReadWrite(0, &tPw);
	for(RetryTime =0; RetryTime <6; RetryTime++){
		if(Wok != 1){
			Wok = CtlCenter.PassWordII_ReadWrite(0, &tPw);
		}
	}
	
	for(i=0; i<16; i++)tPw.Content[i] = '\0';
	for(i=0; i<6; i++){
		tPw.Content[i] = '1' + i;
	}
	Wok = CtlCenter.PassWordIII_ReadWrite(0, &tPw);
	for(RetryTime =0; RetryTime <6; RetryTime++){
		if(Wok != 1){
			Wok = CtlCenter.PassWordII_ReadWrite(0, &tPw);
		}
	}
	
	CtlCenter.PassWordII_ReadWrite(1,nullptr);
	CtlCenter.PassWordIII_ReadWrite(1,nullptr);
}


//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TPasswdChange::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	Updata = 0;
	
	if( 	(aKey == VK_DELETE) || 
		(aKey == VK_0) ||	
		(aKey == VK_1) ||
		(aKey == VK_2) ||
		(aKey == VK_3) ||
		(aKey == VK_4) ||
		(aKey == VK_5) ||
		(aKey == VK_6) ||	
		(aKey == VK_7) ||
		(aKey == VK_8) ||
		(aKey == VK_9)
					){
		for(int i=0; i<6; i++){
			if(aPanel->Edit[i].Selected){
				aPanel->Edit[i].KeyIn(aKey);
				aPanel->Edit[i].ReDraw();
			}
		}		
	}
	
	if(aKey == VK_UP){
		if(Tag.Group == 0){
			if(Tag.Inx0 >0){
				Tag.Inx0--;
				Check4Selected();
				aPanel->ReDrawItems();
				ShowSave_II();
			}
		}else
		if(Tag.Group == 1){
			if(Tag.Inx1 >0){
				Tag.Inx1--;
				Check4Selected();
				aPanel->ReDrawItems();
				ShowSave_III();
			}else{
				Tag.Group = 0;
				Tag.Inx0 = 3;
				Tag.Inx1--;
				Check4Selected();
				aPanel->ReDrawItems();
				ShowSave_II();
			}
		}
	}else
	if(aKey == VK_DOWN){
		if(Tag.Group == 0){
			if(Tag.Inx0 <3){
				Tag.Inx0++;
				Check4Selected();
				aPanel->ReDrawItems();
				ShowSave_II();
			}else{
				Tag.Group = 1;
				Tag.Inx1 = 0;
				Check4Selected();
				aPanel->ReDrawItems();
				ShowSave_II();
			}
		}else
		if(Tag.Group == 1){
			if(Tag.Inx1 <3){
				Tag.Inx1++;
				Check4Selected();
				aPanel->ReDrawItems();
				ShowSave_III();
			}
		}
	}else
	if(aKey == VK_EXECUTE){
		//Save Data Here
		if(Tag.Group == 0){
			if(Tag.Inx0 == 3){
				//if(SystemPermission > 1){
				//	CheckPasswordII();
				//}else{
					if(CheckPrvPasswdOk_II() == 0)CheckPasswordII();
				//}
			}
		}else
		if(Tag.Group == 1){
			if(Tag.Inx1 == 3){
				//if(SystemPermission >2){
				//	CheckPasswordIII();
				//}else{
					if(CheckPrvPasswdOk_III() == 0)CheckPasswordIII();
				//}
			}
		}
	}else
	if(aKey == VK_RETURN){
		aMsg = guiMsgReturn;
	}
	
	if( (aKey != VK_EXECUTE) && (aKey != VK_EXECUTE) ){
		ShowStaII(0);
		ShowStaIII(0);
	}
	
	return aMsg;	
}














