//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include <IdHTTP.hpp>
#include "GlobalUnit.h"
#include "ImgCopperExUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

//---------------------------------------------------------------------------
bool  GetPicType(String &name, TMemoryStream *tmpMemoryStream, bool bBac)
{
	bool bReturn = true;

	tmpMemoryStream->Position = 0;
	WORD wType = 0;
	tmpMemoryStream->ReadBuffer(&wType, 2);

	String tmpName = name;

	if( wType==0x4D42 ){  //bmp

		if( bBac )
			name = ChangeFileExt(name, L".bmp");

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

		if( bBac )
			name = ChangeFileExt(name, L".png");

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

		if( bBac )
			name = ChangeFileExt(name, L".jpg");

	}else bReturn = false;

	if( bReturn && bBac )
		CopyFile(tmpName.c_str(), name.c_str(), false);

	return bReturn;
}
//---------------------------------------------------------------------------
VCBMPEDGE g_VCBMPEDGE;
TBitmap* resizableImage(TBitmap *imgDest,  TRect rectDest,
						int tagSrc, String resourceNameSrc,
						UIEdgeInsets insetsSrc,
						System::Uitypes::TColor TransparentColor)
{
	//   ------judge the image is handled
	bool bFindBMPEDGE = false;
	BMPEDGE *pBMPEDGE = NULL;
	for( VCBMPEDGE::iterator vc_pFindVCBMPEDGE  = g_VCBMPEDGE.begin();
		 vc_pFindVCBMPEDGE != g_VCBMPEDGE.end();
		 vc_pFindVCBMPEDGE++ )
	{
		if( tagSrc==(*vc_pFindVCBMPEDGE)->iTag ) {

			pBMPEDGE     = *vc_pFindVCBMPEDGE;
			bFindBMPEDGE = true;
			break;
		}
	}
	//   ------l r t b cl cr ct cb c cn get bitmap
	if( NULL==pBMPEDGE ) {

		pBMPEDGE = new BMPEDGE(tagSrc, resourceNameSrc);
		pBMPEDGE->pImgLeftT      =  new TBitmap();
		pBMPEDGE->pImgLeftB      =  new TBitmap();
		pBMPEDGE->pImgRightT     =  new TBitmap();
		pBMPEDGE->pImgRightB     =  new TBitmap();
		pBMPEDGE->pImgCenterL    =  new TBitmap();
		pBMPEDGE->pImgCenterR    =  new TBitmap();
		pBMPEDGE->pImgCenterT    =  new TBitmap();
		pBMPEDGE->pImgCenterB    =  new TBitmap();
		pBMPEDGE->pImgCenter     =  new TBitmap();

		pBMPEDGE->pImgBMPSrc = new TBitmap();
		pBMPEDGE->pImgBMPSrc->LoadFromResourceName(int(HInstance), resourceNameSrc);
	}
	TBitmap *imgSrc = pBMPEDGE->pImgBMPSrc;

	//   ------ l r t b cl cr ct cb c cn rect
	int iLeft, iTop, iWidth, iHeight;
	int iLeftNew, iTopNew, iWidthNew, iHeightNew;

	iLeft         			 = 0;
	iTop          			 = 0;
	iWidth       	 		 = imgSrc->Width;
	iHeight       			 = imgSrc->Height;
	TRect rectSrc 		     = TRect(0, 0, iWidth, iHeight);

	iLeftNew        		 = 0;
	iTopNew         		 = 0;
	iWidthNew       		 = rectDest.Width();
	iHeightNew      	     = rectDest.Height();


	UIEdgeInsets insets;
	if( !bFindBMPEDGE ) {

		pBMPEDGE->insetsDest.left   = insetsSrc.left;
		pBMPEDGE->insetsDest.right  = insetsSrc.right;
		pBMPEDGE->insetsDest.top    = insetsSrc.top;
		pBMPEDGE->insetsDest.bottom = insetsSrc.bottom;
		insets = insetsSrc;

	}else{

        insets = pBMPEDGE->insetsDest;
	}
	int LEFTT_WIDTH          = insets.left;
	int LEFTT_HEIGHT         = insets.top;
	int LEFTB_WIDTH          = insets.left;
	int LEFTB_HEIGHT         = insets.bottom;
	int RIGHTT_WIDTH         = insets.right;
	int RIGHTT_HEIGHT        = insets.top;
	int RIGHTB_WIDTH         = insets.right;
	int RIGHTB_HEIGHT        = insets.bottom;

	int CENTERL_WIDTH        = insets.left;
	int CENTERL_HEIGHT       = rectSrc.Height() - (LEFTT_HEIGHT+LEFTB_HEIGHT);
	int CENTERR_WIDTH        = insets.right;
	int CENTERR_HEIGHT       = iHeight - (RIGHTT_HEIGHT+RIGHTB_HEIGHT);
	int CENTERT_WIDTH        = iWidth - (LEFTT_WIDTH+RIGHTT_WIDTH);
	int CENTERT_HEIGHT       = insets.top;
	int CENTERB_WIDTH        = iWidth - (LEFTB_WIDTH+RIGHTB_WIDTH);
	int CENTERB_HEIGHT       = insets.bottom;

	int CENTER_WIDTH         = CENTERT_WIDTH;
	int CENTER_HEIGHT        = iHeight - (CENTERT_HEIGHT+CENTERB_HEIGHT);

	int CENTERL_NEW_WIDTH    = insets.left;
	int CENTERL_NEW_HEIGHT   = iHeightNew - (LEFTT_HEIGHT+LEFTB_HEIGHT);
	int CENTERR_NEW_WIDTH    = insets.right;
	int CENTERR_NEW_HEIGHT   = CENTERL_NEW_HEIGHT;
	int CENTERT_NEW_WIDTH    = iWidthNew - (LEFTT_WIDTH+RIGHTT_WIDTH);
	int CENTERT_NEW_HEIGHT   = insets.top;
	int CENTERB_NEW_WIDTH    = CENTERT_NEW_WIDTH;
	int CENTERB_NEW_HEIGHT   = insets.bottom;

	int CENTER_NEW_WIDTH     = CENTERT_NEW_WIDTH;
	int CENTER_NEW_HEIGHT    = CENTERL_NEW_HEIGHT;

	//   ------  size transparent
	TBitmap *pImgLeftT       = pBMPEDGE->pImgLeftT;
	TBitmap *pImgLeftB       = pBMPEDGE->pImgLeftB;
	TBitmap *pImgRightT      = pBMPEDGE->pImgRightT;
	TBitmap *pImgRightB      = pBMPEDGE->pImgRightB;
	TBitmap *pImgCenterL     = pBMPEDGE->pImgCenterL;
	TBitmap *pImgCenterR     = pBMPEDGE->pImgCenterR;
	TBitmap *pImgCenterT     = pBMPEDGE->pImgCenterT;
	TBitmap *pImgCenterB     = pBMPEDGE->pImgCenterB;
	TBitmap *pImgCenter      = pBMPEDGE->pImgCenter;

	if( !bFindBMPEDGE ) {

		pImgLeftT->SetSize(   LEFTT_WIDTH,    LEFTT_HEIGHT  );
		pImgLeftB->SetSize(   LEFTB_WIDTH,    LEFTB_HEIGHT  );
		pImgRightT->SetSize(  RIGHTT_WIDTH,   RIGHTT_HEIGHT );
		pImgRightB->SetSize(  RIGHTB_WIDTH,   RIGHTB_HEIGHT );
		pImgCenterL->SetSize( CENTERL_WIDTH,  CENTERL_HEIGHT);
		pImgCenterR->SetSize( CENTERR_WIDTH,  CENTERR_HEIGHT);
		pImgCenterT->SetSize( CENTERT_WIDTH,  CENTERT_HEIGHT);
		pImgCenterB->SetSize( CENTERB_WIDTH,  CENTERB_HEIGHT);
		pImgCenter->SetSize(  CENTER_WIDTH,   CENTER_HEIGHT );

		pBMPEDGE->SetTransparent(TransparentColor);
	}

	if( NULL==imgDest ) {
		imgDest = new TBitmap();
		imgDest->SetSize(iWidthNew, iHeightNew);
	}
	imgDest->Transparent       = true;
	imgDest->TransparentColor  = clWhite;


	//   ------new bitmap
	if( !bFindBMPEDGE ) {
//   -----
StretchBlt(pImgLeftT->Canvas->Handle,   0, 0, LEFTT_WIDTH, LEFTT_HEIGHT,
		   imgSrc->Canvas->Handle,      0, 0, LEFTT_WIDTH, LEFTT_HEIGHT,
		   SRCCOPY);
StretchBlt(pImgLeftB->Canvas->Handle,   0, 0, LEFTB_WIDTH, LEFTB_HEIGHT,
		   imgSrc->Canvas->Handle,      0, iHeight-LEFTB_HEIGHT, LEFTB_WIDTH, LEFTB_HEIGHT,
		   SRCCOPY);
StretchBlt(pImgRightT->Canvas->Handle,  0, 0, RIGHTT_WIDTH, RIGHTT_HEIGHT,
		   imgSrc->Canvas->Handle,      iWidth-RIGHTT_WIDTH, 0, RIGHTT_WIDTH, RIGHTT_HEIGHT,
		   SRCCOPY);
StretchBlt(pImgRightB->Canvas->Handle,  0, 0, RIGHTB_WIDTH, RIGHTB_HEIGHT,
		   imgSrc->Canvas->Handle,      iWidth-RIGHTB_WIDTH, iHeight-RIGHTB_HEIGHT, RIGHTB_WIDTH, RIGHTB_HEIGHT,
		   SRCCOPY);

StretchBlt(pImgCenterL->Canvas->Handle, 0, 0, CENTERL_WIDTH, CENTERL_HEIGHT,
		   imgSrc->Canvas->Handle,      0, LEFTT_HEIGHT, CENTERL_WIDTH, CENTERL_HEIGHT,
		   SRCCOPY);
StretchBlt(pImgCenterR->Canvas->Handle, 0, 0, CENTERR_WIDTH, CENTERR_HEIGHT,
		   imgSrc->Canvas->Handle,      iWidth-RIGHTT_WIDTH, RIGHTT_HEIGHT, CENTERR_WIDTH, CENTERR_HEIGHT,
		   SRCCOPY);
StretchBlt(pImgCenterT->Canvas->Handle, 0, 0, CENTERT_WIDTH, CENTERT_HEIGHT,
		   imgSrc->Canvas->Handle,        LEFTT_WIDTH, 0, CENTERT_WIDTH, CENTERT_HEIGHT,
		   SRCCOPY);
StretchBlt(pImgCenterB->Canvas->Handle, 0, 0, CENTERB_WIDTH, CENTERB_HEIGHT,
		   imgSrc->Canvas->Handle,      LEFTB_WIDTH, iHeight-LEFTB_HEIGHT, CENTERB_WIDTH, CENTERB_HEIGHT,
		   SRCCOPY);

StretchBlt(pImgCenter->Canvas->Handle,  0, 0, CENTER_WIDTH, CENTER_HEIGHT,
		   imgSrc->Canvas->Handle,      LEFTT_WIDTH, LEFTT_HEIGHT, CENTER_WIDTH, CENTER_HEIGHT,
		   SRCCOPY);
	}

//   -----
StretchBlt(imgDest->Canvas->Handle,    0, 0, LEFTT_WIDTH, LEFTT_HEIGHT,
		   pImgLeftT->Canvas->Handle,  0, 0, LEFTT_WIDTH, LEFTT_HEIGHT,
		   SRCCOPY);
StretchBlt(imgDest->Canvas->Handle,    0, iHeightNew-LEFTB_HEIGHT, LEFTB_WIDTH, LEFTB_HEIGHT,
		   pImgLeftB->Canvas->Handle,  0, 0, LEFTB_WIDTH, LEFTB_HEIGHT,
		   SRCCOPY);
StretchBlt(imgDest->Canvas->Handle,    iWidthNew-RIGHTT_WIDTH, 0, RIGHTT_WIDTH, RIGHTT_HEIGHT,
		   pImgRightT->Canvas->Handle, 0, 0, RIGHTT_WIDTH, RIGHTT_HEIGHT,
		   SRCCOPY);
StretchBlt(imgDest->Canvas->Handle,    iWidthNew-RIGHTB_WIDTH, iHeightNew-RIGHTB_HEIGHT, RIGHTB_WIDTH, RIGHTB_HEIGHT,
		   pImgRightB->Canvas->Handle, 0, 0, RIGHTB_WIDTH, RIGHTB_HEIGHT,
		   SRCCOPY);

StretchBlt(imgDest->Canvas->Handle,    0, LEFTT_HEIGHT, CENTERL_NEW_WIDTH, CENTERL_NEW_HEIGHT,
		   pImgCenterL->Canvas->Handle,0, 0, CENTERL_WIDTH, CENTERL_HEIGHT,
		   SRCCOPY);
StretchBlt(imgDest->Canvas->Handle,    iWidthNew-RIGHTT_WIDTH, RIGHTT_HEIGHT, CENTERR_NEW_WIDTH, CENTERR_NEW_HEIGHT,
		   pImgCenterR->Canvas->Handle,0, 0, CENTERR_WIDTH, CENTERR_HEIGHT,
		   SRCCOPY);
StretchBlt(imgDest->Canvas->Handle,    LEFTT_WIDTH, 0, CENTERT_NEW_WIDTH, CENTERT_NEW_HEIGHT,
		   pImgCenterT->Canvas->Handle,0, 0, CENTERT_WIDTH, CENTERT_HEIGHT,
		   SRCCOPY);
StretchBlt(imgDest->Canvas->Handle,    LEFTT_WIDTH, iHeightNew-LEFTB_HEIGHT, CENTERB_NEW_WIDTH, CENTERB_NEW_HEIGHT,
		   pImgCenterB->Canvas->Handle,0, 0, CENTERB_WIDTH, CENTERB_HEIGHT,
		   SRCCOPY);
StretchBlt(imgDest->Canvas->Handle,    LEFTT_WIDTH, LEFTT_HEIGHT, CENTER_NEW_WIDTH, CENTER_NEW_HEIGHT,
		   pImgCenter->Canvas->Handle, 0, 0, CENTER_WIDTH, CENTER_HEIGHT,
		   SRCCOPY);

	if( !bFindBMPEDGE ) {
		g_VCBMPEDGE.push_back(pBMPEDGE);
    }

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


TThreadMusciCover *g_pTThreadMusciCover = NULL;
__fastcall TThreadMusciCover::TThreadMusciCover(bool CreateSuspended, TForm *Form)
	: TThread(CreateSuspended)
{
	m_pForm = Form;
}
//---------------------------------------------------------------------------
void __fastcall TThreadMusciCover::Execute()
{
	TStringStream *strStream = new TStringStream(NULL, 65001);

	String strUrl  = g_strServerURL;
	strUrl += L"/index.php/Client/getUserMusicCover/userId/";
	strUrl += IntToStr(g_infoUser.accountInfo.ID);

	String dir  = MUSIC_COVER_DIR;
	String file = L"tmp";
	HRESULT result = GetUrlData(strUrl, dir, file, strStream, true, UrlDown_ImageCover);
	strUrl = strStream->DataString;
	delete strStream;

	if( !(result==S_OK && strUrl.Length() > 1) ){
		return;
	}

	file = MUSIC_COVER_FILE;
	if( S_OK==GetUrlData(strUrl, dir, file, NULL, false, UrlDown_ImageCover) ) {
		PostMessage(m_pForm->Handle, UM_MUSICCOVER, 0, 0 );
	}
}
//---------------------------------------------------------------------------


TThreadHttpPostUsrIcon *g_pTThreadHttpPostUsrIcon = NULL;
__fastcall TThreadHttpPostUsrIcon::TThreadHttpPostUsrIcon(bool CreateSuspended, TWinControl *pWnCtrl, String strPath)
	: TThread(CreateSuspended)
{
	m_pWnCtrl = pWnCtrl;
	m_strPath = strPath;
}
//---------------------------------------------------------------------------
void __fastcall TThreadHttpPostUsrIcon::Execute()
{
	String strFileName = L"";
	bool bSucess  = true;

	String strUrl        = g_strServerURL;
	strUrl 		        += L"/index.php/Client/uploadUserPortrait";
	String struserId     = IntToStr(g_infoUser.accountInfo.ID);
	String strusername   = g_infoUser.strUserName;
	String strpassword   = g_infoUser.strPassword;
	String strCopperPath = m_strPath;

	TIdMultiPartFormDataStream *stream = new TIdMultiPartFormDataStream();
	stream->AddFormField(L"userId",   struserId);
	stream->AddFormField(L"username", strusername, L"UTF-8")->ContentTransfer = L"8bit";
	stream->AddFormField(L"password", strpassword, L"UTF-8")->ContentTransfer = L"8bit";
	stream->AddFile(L"Filedata", strCopperPath, L"image/jpg");


	String strResJson;
	TIdHTTP *httpPost = NULL;
	try{

		httpPost = new TIdHTTP();
		httpPost->Request->ContentType = L"multipart/form-data;";
		strResJson = httpPost->Post(strUrl, stream);

	}catch(Exception &e) {

		SAFE_DELETE(httpPost);
		SAFE_DELETE(stream);
		return;
	}


	TJSONObject *jsonOri = NULL;
	int nState = -1;
	try
	{
		jsonOri = GetJsonContext(strResJson);
		if( jsonOri!=NULL ) {

			TJSONObject *ValueState = GetJsonValue(jsonOri, 0);
			if( ValueState!=NULL ) {

				nState = StrToInt(ValueState->Value());
				if( 1==nState ) {

					TJSONObject *ValueFileName = GetJsonValue(jsonOri, 1);
					if( ValueFileName!=NULL )
						strFileName = ValueFileName->Value();
				}
			}
		}

		SAFE_DELETE(jsonOri);

	}catch(Exception &e){

		SAFE_DELETE(jsonOri);
		SAFE_DELETE(httpPost);
		SAFE_DELETE(stream);
		return;
	}


	SAFE_DELETE(httpPost);
	SAFE_DELETE(stream);

	m_strFileName = strFileName;

//	PostMessage(m_pWnCtrl->Handle, UM_UPLOADUSRICON, 0, 0 );

	if( m_strFileName.Length() )  {

		String strHeaderPath = g_strUserPath.c_str();
		strHeaderPath += L"header\\";
		strHeaderPath += strFileName;

		CopyFile(strCopperPath.c_str(), strHeaderPath.c_str(), true);
		DeleteFile(strCopperPath);
		strcpy(g_infoUser.strIconFile, wstr2str(strFileName.c_str()).c_str());

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