//---------------------------------------------------------------------------

#include <vcl.h>
#include <pcre.h>
#include <stdio.h>
#include "GlobalUnit.h"
#pragma hdrstop

#include "RegisterFormUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "GeneralDialogPanel"
#pragma link "LineEdit"
#pragma link "TextButton"
#pragma link "CKBox"
#pragma resource "*.dfm"

TRegisterForm *RegisterForm;

const char *g_pPattern[4] = {"^[_a-zA-Z0-9\\xA1-\\xFE\\.-]{3,16}$",
				"^[_a-zA-Z0-9\\.-]+@([_a-zA-Z0-9]+\\.)+[a-zA-Z0-9]{2,3}$",
				"^[\\x21-\\x7E]{3,16}$",
				"^[\\x21-\\x7E]{6,16}$"
			};
//---------------------------------------------------------------------------

__fastcall TRegisterForm::TRegisterForm(TComponent* Owner)
	: TForm(Owner)
{
	m_PasswordEdit->PasswordChar = L'*';
	m_ConfirmEdit->PasswordChar = L'*';

	for( int i=0; i<ComponentCount; i++ ){
		if(Components[i]->ClassNameIs( TLabel::ClassName() ) )
			((TLabel*)Components[i])->ControlStyle>>csOpaque;
	}
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::FormCreate(TObject *Sender)
{
	m_DialogPanel->Font->Size = g_szFont9;
	m_UserEdit->SetTextFont(g_strTxtFontName, g_szFont9);
	m_PasswordEdit->SetTextFont(g_strTxtFontName, g_szFont9);
	m_ConfirmEdit->SetTextFont(g_strTxtFontName, g_szFont9);
	m_ckAgree->Font->Size = g_szFont9;
	m_OkButton->FontSize = g_szFont11;

//	FUserName = String(g_infoUser.strUserName);
//	m_UserEdit->Text = FUserName;
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::FormShow(TObject *Sender)
{
	Layout();
}
//---------------------------------------------------------------------------

bool __fastcall TRegisterForm::CheckInput(const char * pText, int type)
{
    bool bRet = false;
	pcre *pPcre = NULL;
	int nOffset = -1;
	const char *pPattern = g_pPattern[type];
	const char *pErrMsg = NULL;

	if( NULL == (pPcre = pcre_compile(pPattern, 0, &pErrMsg, &nOffset, NULL)) ){
		printf("pcre_compile ErrMsg=%s\n", pErrMsg);
	}
	else{
		if( pcre_exec(pPcre, NULL, pText, strlen(pText), 0, NULL, 0) < 0 ){
			printf("%s doesn't match %s\n", pText, pPattern);
		}
		else{
			printf("%s matches %s\n", pText, pPattern);
			bRet = true;
		}

		pcre_free(pPcre);
	}

	return bRet;
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::DoCommitInfo(TObject *Sender)
{
	m_UserEdit->Text = m_UserEdit->Text.Trim();
	m_PasswordEdit->Text = m_PasswordEdit->Text.Trim();
	m_ConfirmEdit->Text = m_ConfirmEdit->Text.Trim();
	m_VerifyEdit->Text = m_VerifyEdit->Text.Trim();

	if(m_UserEdit->Text.IsEmpty()){
		m_UserEdit->SetFocus();
	}
	else if(m_PasswordEdit->Text.IsEmpty()){
		m_PasswordEdit->SetFocus();
	}
	else if(m_ConfirmEdit->Text.IsEmpty()){
		m_ConfirmEdit->SetFocus();
	}
	else{
		bool bCheckOk = true;
		AnsiString astrUN = m_UserEdit->Text;
		if( !CheckInput(astrUN.c_str(), 0) && !CheckInput(astrUN.c_str(), 1 )){
			m_UserEdit->Text = "";
			m_UserEdit->SetFocus();
			bCheckOk = false;
		}
		else{
			AnsiString astrPW = m_PasswordEdit->Text;
			if(!CheckInput(astrPW.c_str(), 3)){
				m_PasswordEdit->Text = "";
				m_ConfirmEdit->Text = "";
				m_PasswordEdit->SetFocus();
				bCheckOk = false;
			}
		}

		if(m_PasswordEdit->Text.Compare(m_ConfirmEdit->Text)){
			m_ConfirmEdit->Text = "";
			m_ConfirmEdit->SetFocus();
			bCheckOk = false;
		}

		if(bCheckOk){
			FUserName = m_UserEdit->Text;
			FPassword = m_PasswordEdit->Text;
			ModalResult = mrOk;

			string strUN = AnsiString(FUserName).c_str();
			string strPW = AnsiString(FPassword).c_str();
			string strVF = AnsiString(FVerify).c_str();
		}
	}
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::DoReturnLableClick(TObject *Sender)
{
//	Layout();
    ModalResult = mrCancel;
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::Layout()
{
//	if( FIsLogin ){
//		m_DialogPanel->Caption = L"登录";
//		m_OkButton->Text = L"登录";
//		m_ReturnLabel->Caption = L"注册新用户 ";
//		m_ReturnLabel->Hint = L"点击注册新用户";
//	}
//	else{
//		m_DialogPanel->Caption = L"注册";
//		m_OkButton->Text = L"注册";
//		m_ReturnLabel->Caption = L"已有账户 ";
//		m_ReturnLabel->Hint = L"点击使用已有账户登录";
//	}

//	m_ckAgree->Checked = true;
//	m_ConfirmEdit->Text = "";
//	m_ReturnLabel->Left = m_OkButton->Left + m_OkButton->Width - m_ReturnLabel->Width;
//
//	m_ConfirmLabel->Visible = !FIsLogin;
//	m_ConfirmEdit->Visible = !FIsLogin;
//	m_IsAutoLogin->Visible = FIsLogin;
//	m_ckAgree->Visible = !FIsLogin;
//	m_lblAgreement->Visible = !FIsLogin;

	if( m_UserEdit->Text.IsEmpty() ){
		m_UserEdit->SetFocus();
	}
	else if( m_VerifyEdit->Text.IsEmpty() ){
		m_ConfirmEdit->SetFocus();
	}
	else if( m_PasswordEdit->Text.IsEmpty() ){
		m_PasswordEdit->SetFocus();
	}
	else if( m_ConfirmEdit->Text.IsEmpty() ){
		m_ConfirmEdit->SetFocus();
	}
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::SetUserName(String Value)
{
	if(FUserName != Value){
        FUserName = Value;
        m_UserEdit->Text = Value;
    }
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::SetPassWord(String Value)
{
	if(FPassword != Value){
        FPassword = Value;
        m_PasswordEdit->Text = Value;
    }
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::SetConfirm(String Value)
{
	if(FConfirm != Value){
		FConfirm = Value;
		m_ConfirmEdit->Text = Value;
	}
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::SetVerify(String Value)
{
	if(FVerify != Value){
		FVerify = Value;
		m_VerifyEdit->Text = Value;
	}
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	switch( Key ){
	case VK_RETURN:
		DoCommitInfo(NULL);
		break;

	case VK_ESCAPE:
		ModalResult = mrCancel;
		break;
	}
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::AgreementMouseEnter(TObject *Sender)
{
	m_lblAgreement->Font->Color = RGB(0, 255, 255);
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::AgreementMouseLeave(TObject *Sender)
{
	m_lblAgreement->Font->Color = RGB(0, 200, 200);
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::AgreementClick(TObject *Sender)
{
	char strURL[256];
	f_CS_GetWebPageURL( URL_USERAGREEMENT, strURL );
	ShellExecute(Handle, NULL, ((String)strURL).c_str(), NULL, NULL, SW_SHOWNORMAL);
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::ReturnLabelMouseEnter(TObject *Sender)
{
	m_ReturnLabel->Font->Color = RGB(225, 235, 245);
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::ReturnLabelMouseLeave(TObject *Sender)
{
	m_ReturnLabel->Font->Color = RGB(191,203,218);
}
//---------------------------------------------------------------------------

void __fastcall TRegisterForm::AgreeChange(TObject *Sender)
{
	m_OkButton->Enabled = m_ckAgree->Checked;
}
//---------------------------------------------------------------------------

