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

#include <vcl.h>
#include <Vcl.Imaging.pngimage.hpp>
#include <System.NetEncoding.hpp>
#include "GlobalUnit.h"
#pragma hdrstop

#include "VerifyCodeFormUnit.h"
#include "MessageFormUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "GeneralDialogPanel"
#pragma link "LineEdit"
#pragma link "TextButton"
#pragma resource "*.dfm"
TVerifyCodeForm *VerifyCodeForm;
//---------------------------------------------------------------------------
__fastcall TVerifyCodeForm::TVerifyCodeForm(TComponent* Owner, String baseUrl)
	: TForm(Owner)
{
	for( int i=0; i<ComponentCount; i++ ){
		if(Components[i]->ClassNameIs( TLabel::ClassName() ) )
			((TLabel*)Components[i])->ControlStyle>>csOpaque;
	}

	RESTClient->BaseURL = String(baseUrl);
}
//---------------------------------------------------------------------------
void __fastcall TVerifyCodeForm::FormCreate(TObject *Sender)
{
    m_editCode->SetTextFont(g_strTxtFontName, g_szFont9);
    m_btnOk->FontSize = g_szFont9;

	RefreshCode();
}
//---------------------------------------------------------------------------

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

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

void __fastcall TVerifyCodeForm::RefreshCode()
{
	this->Enabled = false;
	Screen->Cursor = crHourGlass;

	RESTRequest->Resource = "captcha";
	RESTRequest->Params->Clear();

	try{
		RESTRequest->Execute();
	}
	catch (Exception &exception){
		DebugPrint("exception: %s", exception.Message );
	}

	Screen->Cursor = crDefault;
	this->Enabled = true;

	if(RESTResponse->StatusCode==200){
		TJSONValue *jValue = RESTResponse->JSONValue;
		if(jValue==NULL)
			return;

		TJSONValue *jCode = jValue->FindValue("code");
		if( jCode==NULL )
			return;

		String code = ((TJSONString*)jCode)->Value();
		if(code=="00000"){
			TJSONValue *jData = jValue->FindValue("data");

			TJSONValue *jKey = jData->FindValue("captchaKey");
			m_sKey = ((TJSONString*)jKey)->Value();

			TJSONValue *jBase64 = jData->FindValue("captchaBase64");
			String sBase64 = ((TJSONString*)jBase64)->Value();
			sBase64 = sBase64.SubString(23,sBase64.Length()-22);
			Sysutils::TBytes bytes = TNetEncoding::Base64String->DecodeStringToBytes(sBase64);

			TPngImage * png = new TPngImage();
			png->LoadFromStream(new TBytesStream(bytes));
			m_imgCode->Picture->Assign(png);

			delete png;
		}
		else{
			MessageForm->ShowMessage( "获取验证码图片失败，请稍后重试", MB_ICONERROR | MB_OK );

			return;
		}
	}
	else{
		MessageForm->ShowMessage( "访问服务器异常，请稍后重试", MB_ICONERROR | MB_OK );

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

void __fastcall TVerifyCodeForm::CodeImgClick(TObject *Sender)
{
	RefreshCode();
}
//---------------------------------------------------------------------------

void __fastcall TVerifyCodeForm::OkClick(TObject *Sender)
{
	m_editCode->Text = m_editCode->Text.Trim();
	if(m_editCode->Text.IsEmpty()){
		m_editCode->SetFocus();
		return;
	}

    m_sCode = m_editCode->Text;

    ModalResult = mrOk;
}
//---------------------------------------------------------------------------

