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

#pragma hdrstop
#include <vcl.h>
#include "NotificationExUnit.h"
#include <System.IOUtils.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)

extern wstring	g_strUserPath;

TJSONObject* __fastcall GetJsonContext(String strContextNoti)
{
	TJSONObject *jsonTmp = new TJSONObject();
	TBytes StringBytesTmp = TEncoding::UTF8->GetBytes(strContextNoti);
	jsonTmp = (TJSONObject *)jsonTmp->ParseJSONValue(StringBytesTmp, 0);

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

TJSONObject* __fastcall GetJsonValue(TJSONObject *json, int index)
{
	if( json->Count > index ){
		String strkey = json->Get(index)->JsonString->Value();
		return (TJSONObject *)json->GetValue(strkey);
	}

	return NULL;
}
//---------------------------------------------------------------------------
typedef struct tagBingdingWhere{

	IBinding *bind;
	int iWhereIs;

}BingdingWhere;

typedef vector<BingdingWhere *> VCBingding;
VCBingding vc_Bingding;

void __fastcall StopURLDownload(int iWhereIs)
{
	for(int i=0; i<vc_Bingding.size(); i++) {
		BingdingWhere *pTmp = vc_Bingding[i];
		if( iWhereIs==pTmp->iWhereIs ) {
			if( pTmp->bind ){
				pTmp->bind->Abort();
			}
			break;
		}
	}
}

HRESULT STDMETHODCALLTYPE CBindCBHttpCallback::OnStartBinding(DWORD dwReserved, IBinding __RPC_FAR *pib)
{
	pib->AddRef();

	for(int i=0; i<vc_Bingding.size(); i++) {
		BingdingWhere *pTmp = vc_Bingding[i];
		if(pTmp->iWhereIs==iWhereIs){
			pTmp->bind->Release();
			pTmp->bind = pib;

			return S_OK;
		}
	}

	BingdingWhere *pWhere = new BingdingWhere();
	pWhere->bind = pib;
	pWhere->iWhereIs = iWhereIs;
	vc_Bingding.push_back(pWhere);

	return S_OK;
}

HRESULT STDMETHODCALLTYPE CBindCBHttpCallback::OnStopBinding(HRESULT hresult, LPCWSTR szError)
{
	for(int i=0; i<vc_Bingding.size(); i++) {
		BingdingWhere *pTmp = vc_Bingding[i];
		if( iWhereIs==pTmp->iWhereIs ) {
			if( pTmp->bind ){
				pTmp->bind->Release();
				pTmp->bind = NULL;
			}

			delete pTmp;
			vc_Bingding.erase(vc_Bingding.begin()+i);

			break;
		}
	}

	return S_OK;
}

//**********************************************************************
//***************************************TUrlLabel
//**********************************************************************
//---------------------------------------------------------------------------

__fastcall TUrlLabel::TUrlLabel( TComponent* Owner):
	Vcl::Stdctrls::TLabel( Owner )
{
	FCloseForm = false;

	Transparent = true;
	ControlStyle>>csOpaque;

	FColorNor = CLR_TEXT_NORMAL;
	FColorEnt = CLR_TEXT_ENTER;
	FColorLea = CLR_TEXT_LEAVE;

	Font->Color = FColorNor;
}
//---------------------------------------------------------------------------

__fastcall TUrlLabel::~TUrlLabel()
{

}
//---------------------------------------------------------------------------

void __fastcall TUrlLabel::CMMouseLeave(Winapi::Messages::TMessage &Msg)
{
	if( FStrUrl.Length() ) {
		Screen->Cursor = crDefault;
		Font->Color = FColorLea;
	}

	if( FOnMouseLeave )
		FOnMouseLeave(this);
}
//---------------------------------------------------------------------------

void __fastcall TUrlLabel::CMMouseEnter(Winapi::Messages::TMessage &Msg)
{
	if( FStrUrl.Length() ) {
		Screen->Cursor = crHandPoint;
		Font->Color = FColorEnt;
	}else
		Font->Color = FColorNor;

	if( FOnMouseEnter ) {
		FOnMouseEnter(this);
	}
}
//---------------------------------------------------------------------------

void __fastcall TUrlLabel::Click()
{
	if( StrUrl.Length() ) {
		ShellExecute(Parent->Handle, NULL, StrUrl.c_str(), NULL, NULL, SW_SHOWNORMAL);
		if(FCloseForm) {
			TCustomForm * parentForm = GetParentForm( this );
			parentForm->Close();
		}
	}
}
//---------------------------------------------------------------------------

void __fastcall TUrlLabel::SetStrUrl(String url)
{
	FStrUrl = url;
	if( FStrUrl.Length() ) {
		Font->Color = FColorLea;
	}
}
//---------------------------------------------------------------------------

//**********************************************************************
//***************************************TUrlPaintBox
//**********************************************************************
//---------------------------------------------------------------------------
const COLORREF
	CLR_AD_CHANGE_NOR   = RGB(63, 68, 76),
	CLR_AD_CHANGE_ENTER = RGB(94, 101, 112);

#define STATE_NORMAL 0
#define STATE_ENTER  1

__fastcall TUrlPaintBox::TUrlPaintBox( TComponent* Owner, TPicture * graphicImg):
	Vcl::Extctrls::TPaintBox( Owner )
{
	FCloseForm    = false;
	FState		  = 0;
	FStateCount   = 1;
	FIndex        = 0;
	FGraphicImg   = graphicImg;
}
//---------------------------------------------------------------------------

__fastcall TUrlPaintBox::~TUrlPaintBox()
{

}
//---------------------------------------------------------------------------

void __fastcall TUrlPaintBox::Click()
{
	if( FStrUrl.Length() ) {
		ShellExecute(Parent->Handle, NULL, FStrUrl.c_str(), NULL, NULL, SW_SHOWNORMAL);
		if(FCloseForm) {
			TCustomForm * parentForm = GetParentForm( this );
			parentForm->Close();
		}
	}
}
//---------------------------------------------------------------------------
void __fastcall TUrlPaintBox::CMMouseEnter(Winapi::Messages::TMessage &Msg)
{
	if( FStrUrl.Length() )
		Screen->Cursor = crHandPoint;

	if( FOnMouseEnter )
		FOnMouseEnter(this);

}

//---------------------------------------------------------------------------
void __fastcall TUrlPaintBox::CMMouseLeave(Winapi::Messages::TMessage &Msg)
{
	if( FStrUrl.Length() )
		Screen->Cursor = crDefault;

	if( FOnMouseLeave )
		FOnMouseLeave(this);
}
//---------------------------------------------------------------------------

void __fastcall TUrlPaintBox::SetGraphicImg(TPicture *GraphicImg)
{
	FGraphicImg = GraphicImg;
	this->Invalidate();
}
//---------------------------------------------------------------------------

void __fastcall TUrlPaintBox::SetStrUrl(String strUrl)
{
	FStrUrl = strUrl;
}
//---------------------------------------------------------------------------

void __fastcall TUrlPaintBox::SetStateCount(int stateCount)
{
	FStateCount = stateCount;
}
//---------------------------------------------------------------------------
void __fastcall TUrlPaintBox::Paint()
{
	if( GraphicImg ){
		if( 1< FStateCount )
			Canvas->Draw((-(FGraphicImg->Width/FStateCount))*FState, 0, FGraphicImg->Graphic);
		else
			Canvas->StretchDraw( TRect(0, 0, Width, Height), FGraphicImg->Graphic);
	}else{
		if( FState==STATE_NORMAL ){
			Canvas->Brush->Color = CLR_AD_CHANGE_NOR;
		}else if( FState==STATE_ENTER ){
			Canvas->Brush->Color = CLR_AD_CHANGE_ENTER;
		}
		Canvas->FillRect(TRect(0, 0, Width, Height));
	}
}
//---------------------------------------------------------------------------


//**********************************************************************
//***************************************GetUrlData
//**********************************************************************
//---------------------------------------------------------------------------
HRESULT GetUrlData(String url, String fileDirect, String &fileName, TStringStream *fileToSteam, bool bfileToStream, int iWhereIs)
{
	String BasePath = g_strUserPath.c_str();
	BasePath += fileDirect;

	fileName = BasePath + fileName;
	if(!DirectoryExists(BasePath, false))
		ForceDirectories(BasePath);

	DeleteUrlCacheEntry(url.c_str());

	CBindCBHttpCallback callBack;
	callBack.iWhereIs =  iWhereIs;
	HRESULT result = URLDownloadToFile(NULL, url.c_str(), fileName.c_str(), 0, &callBack);

	if( result==S_OK )
	{
		bool bfileExsit = true;
		if(  !FileExists(fileName) ){
			result = S_FALSE;
			fileName = L"";
			bfileExsit = false;
		}

		if( bfileToStream )
		{
			if( bfileExsit )
			{
				if( fileToSteam == NULL ){
					fileToSteam = new TStringStream(NULL, 65001);
				}

				fileToSteam->LoadFromFile(fileName);
				fileToSteam->Position = 0;

				DeleteFile(fileName);
			}
		}
	}

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

bool __fastcall GetPic(String &name, bool bCopy)
{
	bool bReturn = true;

	TMemoryStream *tmpMemoryStream = new TMemoryStream();
	tmpMemoryStream->LoadFromFile(name);
	tmpMemoryStream->Position = 0;
	WORD wType = 0;
	tmpMemoryStream->ReadBuffer(&wType, 2);

	String tmpName = name;

	if( wType==0x5089 ){  //png

		name = ChangeFileExt(name, L".png");
		RenameFile(tmpName, name);

	}else if( wType==0xD8FF  ) { //jpg

		name = ChangeFileExt(name, L".jpg");
		RenameFile(tmpName, name);

	}else if( wType==0x4947 ){

		name = ChangeFileExt(name, L".gif");
		RenameFile(tmpName, name);

	}else{
		bReturn = false;
	}

	if( tmpMemoryStream )
		delete tmpMemoryStream;

	if( bReturn && bCopy)
		CopyFile(name.c_str(), tmpName.c_str(), true);

	return bReturn;
}


