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

#include <vcl.h>
#pragma hdrstop

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

TStyleUploadForm *StyleUploadForm;
//---------------------------------------------------------------------------
__fastcall TStyleUploadForm::TStyleUploadForm(TComponent* Owner, TPROSTYLE* pStyle, bool bShare)
	: TForm(Owner)
{
	m_bShare = bShare;
	m_pStyle = pStyle;
	m_iCheckCount = 0;
	m_nScore = 0;

	memset( m_iStyleType, -1, MAX_TYPE );
}
//---------------------------------------------------------------------------

void __fastcall TStyleUploadForm::FormCreate(TObject *Sender)
{
	m_GPanelBKG->Font->Size = g_szFont9;
	m_btnOk->FontSize = g_szFont9;
	m_btnCancel->FontSize = g_szFont9;
	m_editStyleName->SetTextFont(g_strTxtFontName, g_szFont9);
	m_editAuthor->SetTextFont(g_strTxtFontName, g_szFont9);
	m_editCost->SetTextFont(g_strTxtFontName, g_szFont9);

	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;

	for(int i=0; i<12; i++ ){
		m_cbTypes[i]->Font->Name = g_strTxtFontName;
		m_cbTypes[i]->Font->Size = g_szFont9;
	}

	if( m_bShare ){
		PresetType();

		m_nScore = SystemScore() + 500;

		char buf[16];
		int score10 =  (int)(m_nScore/10.0 + 0.5);
		sprintf( buf, "%.1f", score10 / 10.0 );

		m_lblNote->Caption = buf;
		m_lblCost->Caption = "系统评分：";
		m_lblCost->Left = m_editCost->Left;
		m_editCost->Visible = false;
		m_lblProtocol->Visible = false;
		m_lblTips->Top = m_btnOk->Top;
		m_lblTips->Height = 30;
	}
	else{
		m_lblTips->Top = m_lblProtocol->Top - 24;
    }

	m_editStyleName->Text = UTF8ToMBCS(m_pStyle->pLocal->strStyleName).c_str();
	m_editAuthor->Text = g_infoUser.strNickName;

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

void __fastcall TStyleUploadForm::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;
	}

	m_btnOk->Enabled = UploadEnabled();
}
//---------------------------------------------------------------------------

void __fastcall TStyleUploadForm::MemoChange(TObject *Sender)
{
	TMemo* memo = (TMemo*)Sender;

	AnsiString strText = memo->Text.Trim();
	string strTmp = GBToUTF8(strText.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;
	}

	m_btnOk->Enabled = UploadEnabled();
}
//---------------------------------------------------------------------------

void __fastcall TStyleUploadForm::CheckBoxClick(TObject *Sender)
{
	int iType = ((TCKBox*)Sender)->Tag;
	bool bChecked = ((TCKBox*)Sender)->Checked;

	bool bChanged = false;

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

	if( bChanged )
	    m_btnOk->Enabled = UploadEnabled();
}
//---------------------------------------------------------------------------
void __fastcall TStyleUploadForm::AuthorChange(TObject *Sender)
{
	m_btnOk->Enabled = UploadEnabled();
}
//---------------------------------------------------------------------------

void __fastcall TStyleUploadForm::CostChange(TObject *Sender)
{
    m_btnOk->Enabled = UploadEnabled();
}
//---------------------------------------------------------------------------

void __fastcall TStyleUploadForm::GetShareInfo( TSTYLESHARE* pInfo )
{
	AnsiString strText = m_editStyleName->Text.Trim();
	string strTmp = GBToUTF8(strText.c_str());
	strcpy( pInfo->strStyleName, strTmp.c_str() );

	strText = m_editAuthor->Text.Trim();
	strTmp = GBToUTF8(strText.c_str());
	strcpy( pInfo->strAuthor, strTmp.c_str() );

	strText = m_memoDescription->Text.Trim();
	strTmp = GBToUTF8(strText.c_str());
	strcpy( pInfo->strDescription, strTmp.c_str() );

	memset( pInfo->type, -1, MAX_TYPE );
	int count = 0;
	for( int i=0; i<TYPE_NUM; i++ ){
		if(  m_cbTypes[i]->Checked ){
			pInfo->type[count++] = m_cbTypes[i]->Tag;
		}
	}

	pInfo->score = m_nScore;
}
//---------------------------------------------------------------------------

void __fastcall TStyleUploadForm::GetMakeInfo( TMAKESTYLEINFO* pInfo )
{
	AnsiString strText = m_editStyleName->Text.Trim();
	string strTmp = GBToUTF8(strText.c_str());
	strcpy( pInfo->detail.strStyleName, strTmp.c_str() );

	strText = m_editAuthor->Text.Trim();
	strTmp = GBToUTF8(strText.c_str());
	strcpy( pInfo->detail.strAuthor, strTmp.c_str() );

	strText = m_memoDescription->Text.Trim();
	strTmp = GBToUTF8(strText.c_str());
	strcpy( pInfo->detail.strDescription, strTmp.c_str() );

	memset( pInfo->detail.type, -1, 4 );
	int count = 0;
	for( int i=0; i<TYPE_NUM; i++ ){
		if(  m_cbTypes[i]->Checked ){
			pInfo->detail.type[count++] = m_cbTypes[i]->Tag;
		}
	}

	pInfo->cost = StrToInt( m_editCost->Text.Trim() );
}
//---------------------------------------------------------------------------

void TStyleUploadForm::PresetType()
{
	TSTYLELOCAL* pLocal = m_pStyle->pLocal;

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

	int count = 0;
	for( int i=0; count<MAX_STYLE && i<4; i++ ){
		for( int j=0; count<MAX_STYLE && j<8; j++ ){
			for( int k=0; k<16; k++ ){
				if( pLocal->trkRef[i][j][k].idLoc==0
								&& pLocal->trkRef[i][j][k].idStyle==0 )
					break;

				int id = pLocal->trkRef[i][j][k].idStyle;

				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;
					}
				}
			}
		}
	}

	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>=TYPE_NUM-1 ){
					iCB = TYPE_NUM-1;
					iType = 127;
				}

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

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

					for( int k=0; k<MAX_TYPE; k++ ){
						if( m_iStyleType[k]==-1 ){
							m_iStyleType[k] = iType;
							m_iCheckCount++;
							break;
						}
					}

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

int TStyleUploadForm::SystemScore()
{
	int score = 0;

	vector<UINT> aSegID;
	for( int s=0; s<4; s++ ){
		int repeat = s<2 ? 1 : 2;   //前尾奏计算大小调

		for( int r=0; r<repeat; r++ ){
			int begin = r==0 ? 0 : 4,  //前尾奏小调从4开始
				end = begin + ( s==0 ? 4 : 2 ); //伴奏计算4个，其它计算2个

			for( int n=begin; n<end; n++ ){
				int segScore = 0;

				UINT aTrkID[16], aTrkSN[16], aCount[16], nIDNum = 0;
				memset( aTrkID, 0, sizeof(aTrkID) );
				memset( aTrkSN, 0, sizeof(aTrkSN) );
				memset( aCount, 0, sizeof(aCount) );

				for( int t=0; segScore<25 && t<16; t++ ){
					TTRACKREF& trkRef = m_pStyle->pLocal->trkRef[s][n][t];
					int sn = trkRef.segNo;

					if( trkRef.idStyle==0 )
						break;

					bool bDiffID = true;
					for( int i=0; i<nIDNum; i++ ){
						if( trkRef.idStyle==aTrkID[i] && sn==aTrkSN[i] ){
							aCount[i]++;
							bDiffID = false;
							break;
						}
					}

					if( bDiffID ){
						if( nIDNum>0 )
							segScore += 5;

						aTrkID[nIDNum] = trkRef.idStyle;
						aTrkSN[nIDNum] = sn;
						aCount[nIDNum]++;
						nIDNum++;
					}
					else{
						TPROSTYLE* pTrkStyle = GetStyleInfo( 0, trkRef.idStyle );
						TSTYLEDETAIL& detail = pTrkStyle->pOrigin->detail;

						for( int i=0; i<16; i++ ){
							TCHAN& trkOrig = detail.tracks[s][sn][i];
							if( trkOrig.no<0 )
								break;

							if( trkOrig.no==trkRef.chan ){
								if( trkOrig.prog!=trkRef.prog //修改了乐器和音量
												|| trkOrig.vol!=trkRef.vol ){
									segScore += 5;
								}

								break;
							}
						}
					}
				}

				int maxCount = aCount[0], iMax = 0;
				for( int i=1; i<nIDNum; i++ ){
					if( aCount[i]>maxCount ){
						maxCount = aCount[i];
						iMax = i;
					}
				}

				if( segScore<25 ){
					TPROSTYLE* pSegStyle = GetStyleInfo( 0, aTrkID[iMax] );
					TSTYLEDETAIL& detail = pSegStyle->pOrigin->detail;

					int sn = aTrkSN[iMax];
					int tc;
					for( tc=0; tc<16 && detail.tracks[s][sn][tc].no>=0; tc++ );

					if( tc>aCount[iMax] ){
						segScore += 5 * ( tc-aCount[iMax] );
					}
				}

				bool bDiffID = true;
				for( int i=0; i<aSegID.size(); i++ ){
					if( aSegID[i]==aTrkID[iMax] ){
						bDiffID = false;
						break;
					}
				}

				bool bScore = false;
				if( bDiffID ){
					if( segScore<25 && aSegID.size()>0 ){
						segScore += 10;
						bScore = true;
					}

					aSegID.push_back( aTrkID[iMax] );
				}

				if( !bScore && m_pStyle->pLocal->nSegVol[s][n]<100
								&& m_pStyle->pLocal->nSegVol[s][n]>50 ){
					segScore += 5;
				}

				if( segScore>25 )
					segScore = 25;

				score += segScore;
			}
		}
	}

	if( score>350 )
		score = 350;

	if( score<100 ){
		m_lblTips->Caption =
				"提示：系统评分低于6分，不能上传\n          关于系统评分参看用户手册";
		m_lblTips->Font->Color = 0x004040FF;
	}

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

bool TStyleUploadForm::UploadEnabled()
{
	if( m_bShare && m_nScore<600 ){
		return false;
	}

	if( m_editStyleName->Text.Trim().IsEmpty() ){
		m_lblTips->Caption = "提示：请输入风格名称";
//		m_editStyleName->SetFocus();
		return false;
	}

	if( m_editAuthor->Text.Trim().IsEmpty() ){
		if( m_bShare )
			m_lblTips->Caption = "提示：请输入分享者名称";
		else
			m_lblTips->Caption = "提示：请输入制作者名称";
//		m_editAuthor->SetFocus();
		return false;
	}

	if( !m_bShare && m_editCost->Text.Trim().IsEmpty() ){
		m_lblTips->Caption = "提示：请输入使用价格(0~9999)";
//		m_editCost->SetFocus();
		return false;
	}

	if( m_iCheckCount==0 ){
		m_lblTips->Caption = "提示：请选择分类";
		return false;
	}

	if( m_memoDescription->Text.Trim().IsEmpty() ){
		m_lblTips->Caption = "提示：请输入描述信息";
//		m_memoDescription->SetFocus();
		return false;
	}

    m_lblTips->Caption = "";

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

void __fastcall TStyleUploadForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	switch( Key ){
	case VK_RETURN:
		if(m_btnOk->Enabled)
			ModalResult = mrOk;
		break;

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

void __fastcall TStyleUploadForm::ProtocolMouseEnter(TObject *Sender)
{
	m_lblProtocol->Font->Style = TFontStyles()<<fsUnderline<<fsBold;
	m_lblProtocol->Font->Color = clAqua;
}
//---------------------------------------------------------------------------

void __fastcall TStyleUploadForm::ProtocolMouseLeave(TObject *Sender)
{
	m_lblProtocol->Font->Style = TFontStyles()<<fsBold;
	m_lblProtocol->Font->Color = clHighlight;
}
//---------------------------------------------------------------------------


void __fastcall TStyleUploadForm::ProtocolClick(TObject *Sender)
{
	String strUrl = g_strServerURL;
	strUrl += L"/index.php/Web?tpl=style_agreement";
	ShellExecute(Handle,NULL, strUrl.c_str(), NULL,NULL,SW_SHOWNORMAL);
}
//---------------------------------------------------------------------------

