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

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

#define MAX_TYPE	4
#define MAX_STYLE	32

TUploadForm *UploadForm;

#include "ImgCopperFormUnit.h"
#include "Project.h"
//---------------------------------------------------------------------------

__fastcall TUploadForm::TUploadForm(TComponent* Owner)
	: TForm(Owner)
{
	m_iCheckCount = 0;
	m_editCpjPrice->Enabled = false;
}
//---------------------------------------------------------------------------
__fastcall TUploadForm::~TUploadForm()
{
	StopURLDownload(UrlDown_ImageCover);
}
//---------------------------------------------------------------------------

void __fastcall TUploadForm::FormCreate(TObject *Sender)
{
	m_pCBSex->AddItem(L"男");
	m_pCBSex->AddItem(L"女");
	m_pCBSex->AddItem(L"组合");
	m_pCBSex->AddItem(L"其他");
	m_pCBSex->Show();

	m_editOriginal->Enabled = false;
	m_pcbOriAmend->AddItem(L"原创");
	m_pcbOriAmend->AddItem(L"改编");
	m_pcbOriAmend->DefItemIndex = 0;
	m_pcbOriAmend->Show();

	Font->Color = RGB(156, 170, 189);

	m_cbTypes[0] = m_checkPop;
	m_cbTypes[1] = m_checkFolk;
	m_cbTypes[2] = m_checkRock;
	m_cbTypes[3] = m_checkDance;
	m_cbTypes[4] = m_checkJazz;
	m_cbTypes[5] = m_checkBlues;
	m_cbTypes[6] = m_checkSamba;
	m_cbTypes[7] = m_checkClassic;
	m_cbTypes[8] = m_checkWaltz;
	m_cbTypes[9] = m_checkIndpent;
	m_cbTypes[10] = m_checkCountry;
	m_cbTypes[11] = m_checkOthers;

	if( strlen(g_infoSong.strSongName)==0 )
		sprintf( g_infoSong.strSongName, "untitle-%s-%d",
										g_strTSName[g_iMelodyTimeSignature],
										g_nMelodyTempo );
	m_editSongName->Text = g_infoSong.strSongName;

	if( strlen(g_infoSong.strSinger)==0 )
		strcpy( g_infoSong.strSinger, g_infoUser.strNickName );
	m_editSinger->Text = g_infoSong.strSinger;

	if( strlen(g_infoSong.strArranger)==0 )
		strcpy( g_infoSong.strArranger, g_infoUser.strNickName );
	m_editArranger->Text = g_infoSong.strArranger;

	if( strlen(g_infoSong.strLyricist)==0 )
		strcpy( g_infoSong.strLyricist, g_infoUser.strNickName );
	m_editLyricist->Text = g_infoSong.strLyricist;

	if( strlen(g_infoSong.strComposer)==0 )
		strcpy( g_infoSong.strComposer, g_infoUser.strNickName );
	m_editComposer->Text = g_infoSong.strComposer;

	m_editOriginal->Text = g_infoSong.strOriginal;
	m_memoDescription->Text = g_infoSong.strDescription;
	m_checkSubmit->Checked  = g_infoSong.iSubmit;

	if( g_infoSong.iCreation==3 ){
		m_pcbOriAmend->DefItemIndex = 1;
		m_editOriginal->Enabled = true;
	}

	m_pCBSex->DefItemIndex = g_infoSong.iSingerType - 1;

	UINT aStyleIDs[MAX_STYLE];
	memset( aStyleIDs, 0, MAX_STYLE*sizeof(UINT) );

	int count = 0;
	for( int i=0; count<MAX_STYLE && i<g_lstOpusStages.size(); i++ ){
		TOpusStage* pStage = g_lstOpusStages[i];
		for( int j=0; j<pStage->aTracks.size(); j++ ){
			if( pStage->aTracks[j]->iStyleLoc==0 ){
				int id = pStage->aTracks[j]->iStyleId;

				bool bExist = false;
				for( int j=0; j<count; j++ ){
					if( id==aStyleIDs[j] ){
						bExist = true;
						break;
					}
				}
				if( !bExist ){
					aStyleIDs[count++] = id;
					if( count>=MAX_STYLE ){
						break;
					}
				}
			}
		}
	}

	if( memcmp( g_infoSong.aStyleIDs, aStyleIDs, MAX_STYLE*sizeof(UINT) ) ){
		memcpy( g_infoSong.aStyleIDs, aStyleIDs, MAX_STYLE*sizeof(UINT) );
		memset( g_infoSong.iMusicStyle, -1, MAX_TYPE );
	}

	if( g_infoSong.iMusicStyle[0]!=-1 ){
		for( int i=0; i<MAX_TYPE; i++ ){
			int iCB = g_infoSong.iMusicStyle[i];

			if( iCB==127 )
				iCB = MUSIC_TYPE_NUM-1;

			if( iCB!=-1 ){
				m_cbTypes[iCB]->Checked = true;
			}
		}
	}
	else{
		for( int i=0; m_iCheckCount<MAX_TYPE && i<count; i++ ){
			TPROSTYLE* pStyle = GetStyleInfo( 0, aStyleIDs[i] );
			for( int j=0; j<MAX_TYPE; j++ ){
				if( pStyle->pOrigin->detail.type[j]>=0 ){
					char iType = pStyle->pOrigin->detail.type[j];
					char iCB = iType;
					if( iType>=MUSIC_TYPE_NUM-1 ){
						iCB = MUSIC_TYPE_NUM-1;
						iType = 127;
					}

					bool bFound = false;
					for( int k=0; k<m_iCheckCount; k++ ){
						if( g_infoSong.iMusicStyle[k]==iType ){
							bFound = true;
							break;
						}
					}

					if( !bFound ){
						m_cbTypes[iCB]->Checked = true;

						if( m_iCheckCount>=MAX_TYPE ){
							break;
						}
					}
				}
			}
		}
	}

	m_editSongName->OnChange = EditNameChange;
	m_editOriginal->OnChange = EditNameChange;
	m_editSinger->OnChange   = EditNameChange;
	m_editArranger->OnChange = EditNameChange;
	m_editLyricist->OnChange = EditNameChange;
	m_editComposer->OnChange = EditNameChange;

	m_btnOk->Enabled = true;

	if( g_infoSong.strLyric ) {
		m_memoLyrices->Text = g_infoSong.strLyric;
	}

	String strCoverPath = g_strUserPath.c_str();
	strCoverPath += MUSIC_COVER_DIR;
	if( g_infoSong.strIconName ) {
		strCoverPath = g_infoSong.strIconName;
	}else{
		strCoverPath += MUSIC_COVER_FILE;
	}
	if( SetMusicCover(strCoverPath) )
		m_strMusicCoverPath = strCoverPath;

	if( NULL==g_infoSong.strIconName ) {

		bool bDownloadMusicCover = false;
		if( NULL==g_pTThreadMusciCover ) {

			bDownloadMusicCover = true;

		}else if(  !g_pTThreadMusciCover->Started ||
				   (g_pTThreadMusciCover->Started && g_pTThreadMusciCover->Finished) )
		{
			bDownloadMusicCover = true;
		}

		if( bDownloadMusicCover ){

			SAFE_DELETE(g_pTThreadMusciCover)
			g_pTThreadMusciCover = new TThreadMusciCover(true, this);
			g_pTThreadMusciCover->Priority = tpIdle;
			g_pTThreadMusciCover->Start();
		}
	}

	SetTransparent( this );
}
//---------------------------------------------------------------------------


void __fastcall TUploadForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	switch( Key ){
	case VK_RETURN:  //add by tj 2013/12/8
		if( m_btnOk->Enabled &&
			!m_memoDescription->Focused() &&
			!m_memoLyrices->Focused() ){
			OkClick( NULL );
			ModalResult = mrOk;
		}
		break;

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

void __fastcall TUploadForm::OkClick(TObject *Sender)
{
	int nLength = 0;

	AnsiString strName = m_editSongName->Text.Trim();
	strcpy( g_infoSong.strSongName, strName.c_str() );

	strName = m_editSinger->Text.Trim();
	strcpy( g_infoSong.strSinger, strName.c_str() );

	strName = m_editOriginal->Text.Trim();
	strcpy( g_infoSong.strOriginal, strName.c_str() );

	strName = m_editArranger->Text.Trim();
	strcpy( g_infoSong.strArranger, strName.c_str() );

	strName = m_editLyricist->Text.Trim();
	strcpy( g_infoSong.strLyricist, strName.c_str() );

	strName = m_editComposer->Text.Trim();
	strcpy( g_infoSong.strComposer, strName.c_str() );

	strName = m_memoDescription->Text.Trim();
	strcpy( g_infoSong.strDescription, strName.c_str() );

	g_infoSong.iCreation = m_pcbOriAmend->DefItemIndex?3:1;
	g_infoSong.iSingerType = m_pCBSex->DefItemIndex + 1;
	g_infoSong.iSubmit = m_checkSubmit->Checked;

	memset( g_infoSong.iMusicStyle, -1, MAX_TYPE );
	int count = 0;
	for( int i=0; i<MUSIC_TYPE_NUM; i++ ){
		if(  m_cbTypes[i]->Checked ){
			g_infoSong.iMusicStyle[count++] = m_cbTypes[i]->Tag;
		}
	}

	m_strLyrices = m_memoLyrices->Text;
	SAFE_DELETE_ARRAY(g_infoSong.strLyric);
	if( m_strLyrices.Length() ) {
		nLength = strlen( wstr2str(m_strLyrices.c_str()).c_str() );
		g_infoSong.strLyric = new char[nLength+1];
		strcpy(g_infoSong.strLyric, wstr2str(m_strLyrices.c_str()).c_str());
		g_infoSong.strLyric[nLength] = '\0';
	}

	SAFE_DELETE_ARRAY(g_infoSong.strIconName);
	if( m_strCopperPath.Length() || m_strMusicCoverPath.Length() ) {
		if( m_strCopperPath.Length() ) {
			nLength = strlen( wstr2str(m_strCopperPath.c_str()).c_str() );
		}else if( m_strMusicCoverPath.Length() ) {
			nLength = strlen( wstr2str(m_strMusicCoverPath.c_str()).c_str() );
		}

		g_infoSong.strIconName = new char[nLength+1];

		if( m_strCopperPath.Length() ) {
			strcpy(g_infoSong.strIconName, wstr2str(m_strCopperPath.c_str()).c_str());
		}else if( m_strMusicCoverPath.Length() ) {
			strcpy(g_infoSong.strIconName, wstr2str(m_strMusicCoverPath.c_str()).c_str());
		}
		g_infoSong.strIconName[nLength] = '\0';
	}

	SAFE_DELETE_ARRAY(g_infoSong.strCpjName)
	if( m_checkProject->Checked ) {

		String strCPJ = g_strUserPath.c_str();
		strCPJ += L"temp\\";

		if(!DirectoryExists(strCPJ, false))
			ForceDirectories(strCPJ);

		strCPJ += m_editSongName->Text + L".cpj";
		wstring wStrCpjName = strCPJ.c_str();

		if( wStrCpjName.size() ) {
			nLength = strlen( wstr2str(wStrCpjName).c_str() );
			g_infoSong.strCpjName = new char[nLength+1];
			strcpy(g_infoSong.strCpjName, wstr2str(wStrCpjName).c_str());
		}

		if( m_editCpjPrice->Text.Length()==0 ) {
			g_infoSong.cpjPrice = 0;
		}else
			g_infoSong.cpjPrice = StrToInt((m_editCpjPrice->Text));
		g_infoSong.cpjVersion = g_dwProjectVersion;
	}else {
      	g_infoSong.strCpjName = NULL;
	}
}
//---------------------------------------------------------------------------

void __fastcall TUploadForm::EditNameChange(TObject *Sender)
{
	TLineEdit* pEdit = ((TLineEdit*)Sender);

	AnsiString strName = pEdit->Text.Trim();
	string strTmp = GBToUTF8(strName.c_str());

	if( strTmp.length()>pEdit->MaxLength ){
		pEdit->OnChange = NULL;
		pEdit->Text = pEdit->Text.SubString( 0,	pEdit->Text.Length()-1 );
		pEdit->SelStart = pEdit->Text.Length();
		pEdit->OnChange = EditNameChange;
	}

	if( pEdit==m_editSongName ){
		m_btnOk->Enabled = !pEdit->Text.Trim().IsEmpty();
	}
}
//---------------------------------------------------------------------------


void __fastcall TUploadForm::CheckBoxClick(TObject *Sender)
{
	for( int k=0; k<MAX_TYPE; k++ ){
		DebugPrint("g_infoSong.iMusicStyle[k]=%d",
				   g_infoSong.iMusicStyle[k]);
	}
	int iType = ((TCKBox*)Sender)->Tag;
	bool bChecked = ((TCKBox*)Sender)->Checked;

	if( bChecked ){
		if( m_iCheckCount==MAX_TYPE ){
			((TCKBox*)Sender)->Checked = false;
		}
		else{
			for( int k=0; k<MAX_TYPE; k++ ){
				if( g_infoSong.iMusicStyle[k]==-1 || g_infoSong.iMusicStyle[k]==iType ){
					g_infoSong.iMusicStyle[k] = iType;
					m_iCheckCount++;
					break;
				}
			}
		}
	}
	else{
		for( int k=0; k<MAX_TYPE; k++ ){
			if( g_infoSong.iMusicStyle[k]==iType ){
				g_infoSong.iMusicStyle[k] = -1;
				m_iCheckCount--;
				break;
			}
		}

		if( m_iCheckCount==0 ){
			m_checkOthers->Checked = true;
		}
	}
}
//---------------------------------------------------------------------------

void __fastcall TUploadForm::m_pcbOriAmendChange(TComboBoxItem *Sender, int Index)
{
	if( Index==1 ){
		m_editOriginal->Enabled = true;
	}
	else{
		m_editOriginal->Enabled = false;
		m_editOriginal->Text = L"";
	}
}
//---------------------------------------------------------------------------

void __fastcall TUploadForm::MemoChange(TObject *Sender)
{
 //	BKGMemoUnit::TMemo* memo = ((BKGMemoUnit::TMemo*)Sender);
	TMemo* memo = (TMemo*)Sender;

	AnsiString strName = memo->Text.Trim();
	string strTmp = GBToUTF8(strName.c_str());
	if( strTmp.length()>memo->MaxLength ){
		memo->OnChange = NULL;
		memo->Text = memo->Text.SubString( 0, memo->Text.Length()-1 );
		memo->SelStart = memo->Text.Length();
		memo->OnChange = MemoChange;
    }
}
//---------------------------------------------------------------------------
void __fastcall TUploadForm::m_memoLyricesChange(TObject *Sender)
{
	if( m_memoLyrices->Lines->Count>8 )	{
		m_memoLyrices->ScrollBars = ssVertical;
	}else{
		m_memoLyrices->ScrollBars = ssNone;
	}
}
//---------------------------------------------------------------------------
void __fastcall TUploadForm::m_ImgCoverClick(TObject *Sender)
{
	TPicture *pPic = new TPicture();
	TPicture *pPicDraw = new TPicture();
	TBitmap *pBmp = new TBitmap();
    pBmp->SetSize(260, 260);
	if( m_strCopperPath.Length() ){

		pPicDraw->LoadFromFile( m_strCopperPath );
		pBmp->Canvas->StretchDraw(TRect(0, 0, 260, 260), pPicDraw->Graphic);

	}else if( m_strMusicCoverPath.Length() ) {

		pPicDraw->LoadFromFile( m_strMusicCoverPath );
		pBmp->Canvas->StretchDraw(TRect(0, 0, 260, 260), pPicDraw->Graphic);

	}else{

		pBmp->LoadFromResourceName(int(HInstance), L"music_cover_default");
	}

	pPic->Bitmap = pBmp;
	SAFE_DELETE(pBmp)

	TImgCopperForm *pTImgCopperForm = new TImgCopperForm(this, TRect(0, 0, 260, 260), pPic);
	pTImgCopperForm->Width  = 340;
	pTImgCopperForm->Height = 420;

	if( mrOk==pTImgCopperForm->ShowModal() ) {

		m_strCopperPath = g_strUserPath.c_str();
		m_strCopperPath += MUSIC_COVER_DIR;
		m_strCopperPath += MUSIC_COVER_COPPER;

		CopyFile(pTImgCopperForm->m_strCopperPath.c_str(),
			     m_strCopperPath.c_str(), false);

		SetMusicCover(m_strCopperPath);
	}

	SAFE_DELETE(pTImgCopperForm)
	SAFE_DELETE(pPic)
	SAFE_DELETE(pPicDraw)
}
//---------------------------------------------------------------------------

bool __fastcall TUploadForm::SetMusicCover(String &strCoverPath)
{
	bool bSucess = false;

	if( FileExists( strCoverPath ) ) {

		TMemoryStream *memoryStream = new TMemoryStream();
		memoryStream->Position = 0;
		memoryStream->LoadFromFile(strCoverPath);

		if( !GetPicType(strCoverPath, memoryStream, true) ){
			SAFE_DELETE(memoryStream)
		}else {

			TPicture *pTPicture = new TPicture();
			try{
				pTPicture->LoadFromFile(strCoverPath);
			}catch(Exception &e){
				SAFE_DELETE(pTPicture)
				SAFE_DELETE(memoryStream)
			}

			if( pTPicture ) {

				m_ImgCover->Picture = pTPicture;
				SAFE_DELETE(pTPicture)
				bSucess = true;
			}
		}

		SAFE_DELETE(memoryStream)
	}

	return bSucess;
}
//---------------------------------------------------------------------------
void __fastcall TUploadForm::onUM_MUSICCOVER(TMessage & Msg)
{
	if( !m_strCopperPath.Length() ) {

		String strCoverPath = g_strUserPath.c_str();
		strCoverPath += MUSIC_COVER_DIR;
		strCoverPath += MUSIC_COVER_FILE;
		if( SetMusicCover(strCoverPath) )
			m_strMusicCoverPath = strCoverPath;
        SAFE_DELETE(g_pTThreadMusciCover)
	}
}
//---------------------------------------------------------------------------
void __fastcall TUploadForm::ProjectClick(TObject *Sender)
{
	if( m_checkProject->Checked ) {
		m_editCpjPrice->Enabled = true;
//		m_editCpjPrice->Color = clWindow;
//		m_editCpjPrice->Visible = true;
//		m_lbPrice->Visible = true;
//		m_lbSpriceDes->Visible = true;
	}else {
		m_editCpjPrice->Enabled = false;
//		m_editCpjPrice->Color = clInactiveCaption;
//		m_editCpjPrice->Visible = false;
//		m_lbPrice->Visible = false;
//		m_lbSpriceDes->Visible = false;
	}
}
//---------------------------------------------------------------------------

void __fastcall TUploadForm::m_editCpjPriceChange(TObject *Sender)
{
	int nLength = m_editCpjPrice->Text.Length();
	if( nLength==0 ) {
		return;
	}

	for( int i=1; i<=nLength; i++ ) {
		int index = i;
		if( !TryStrToInt(m_editCpjPrice->Text, index) ) {
			m_editCpjPrice->Text = L"0";
			m_btnOk->Enabled = true;
			return;
		}
	}

	int nPrice = StrToInt((m_editCpjPrice->Text));
	if( nPrice==0 || (nPrice>=100 && nPrice<=9999) )
		m_btnOk->Enabled = true;
	 else
		m_btnOk->Enabled = false;
}
//---------------------------------------------------------------------------

