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

#include <vcl.h>
#pragma hdrstop

#include "SaveFormUnit.h"
#include "GlobalUnit.h"
#include "Project.h"
#include <System.IOUtils.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "GeneralDialogPanel"
#pragma link "PNGButton"
#pragma link "LineEdit"
#pragma link "TextButton"
#pragma link "CKBox"
#pragma link "ComboBoxComponent"
#pragma resource "*.dfm"
TSaveForm *SaveForm;

//---------------------------------------------------------------------------
__fastcall TSaveForm::TSaveForm(TComponent* Owner, int iType)
	: TForm(Owner)
{
	SetTransparent( this );

	memset( m_aTypeIndex, -1, sizeof( m_aTypeIndex ) );

	int ind = 0;
	if( iType & SAVE_PROJECT ){
		m_aTypeIndex[ind++] = FILETYPE_CPJ;
	}

	if( (( iType & SAVE_COMPOSE ) || ( iType & SAVE_AUDIO ))
				&& ( !g_bUpdateMandatory || SOFTWARE_ID==e_Pro_Lite ) ){
		m_aTypeIndex[ind++] = FILETYPE_MP3;
		m_aTypeIndex[ind++] = FILETYPE_WAVE;
		m_aTypeIndex[ind++] = FILETYPE_MIDI;
	}

	FSaveType = -1;
	FMediaOption = iType==SAVE_ALL ? g_iLastSaveOption & 0x0F00 : iType;

	m_DefName = L"untitle-";
	m_DefName += g_strTSName[g_iMelodyTimeSignature];
	m_DefName += L"-";
	m_DefName += IntToStr(g_nMelodyTempo);

	m_MediaPath = String(g_strLastSavePath.c_str());
	m_ProjPath = String(g_strLastProjSavePath.c_str());

	String basePath;
	basePath.sprintf(L"%s", g_strUserPath.c_str());

	if( m_MediaPath.IsEmpty() ){
		m_MediaPath = basePath + L"Save";

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

		m_MediaPath += L"\\";// + m_DefName + L".mp3";
	}

	if( m_ProjPath.IsEmpty() ){
		m_ProjPath = basePath + L"Projects";

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

		m_ProjPath += L"\\";// + m_DefName + L".cpj";
	}

	if( strlen( g_infoSong.strSongName )>0 ){
		m_MediaPath += g_infoSong.strSongName;
		m_ProjPath += g_infoSong.strSongName;
	}
	else{
		m_MediaPath += m_DefName;
		m_ProjPath += m_DefName;
	}

	m_MediaPath += L".mp3";
	m_ProjPath += L".cpj";
}
//---------------------------------------------------------------------------

void __fastcall TSaveForm::BtnBrowseClick(TObject *Sender)
{
    TSaveDialog * pDlg = new TSaveDialog(NULL);
	pDlg->Options << ofCreatePrompt << ofOverwritePrompt;
	pDlg->Title = L"导出为";
    pDlg->FileName = ExtractFileName(m_editFileName->Text);
    pDlg->InitialDir = ExtractFilePath(m_editFileName->Text);

	switch( m_aTypeIndex[m_SaveTypeCombo->DefItemIndex] ){
		case FILETYPE_CPJ:
			if(m_editFileName->Text.Length() == 0)
				pDlg->FileName = m_DefName;

			pDlg->Filter = "编曲工程文件(*.cpj)|*.cpj";
			pDlg->DefaultExt = "cpj";
			break;
		case FILETYPE_MP3:
			pDlg->Filter = "MP3文件(*.mp3)|*.mp3";
			pDlg->DefaultExt = "mp3";
			break;
		case FILETYPE_WAVE:
			pDlg->Filter = "WAVE文件(*.wav)|*.wav";
			pDlg->DefaultExt = "wav";
			break;
		case FILETYPE_MIDI:
			pDlg->Filter = "MIDI文件(*.mid)|*.mid";
			pDlg->DefaultExt = "mid";
			break;
		default:
			pDlg->Filter = "所有文件(*.*)|*.*";
			break;

	};

	if(pDlg->Execute()){
		m_editFileName->Text = pDlg->FileName;
	}

	delete pDlg;

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

void __fastcall TSaveForm::FileNameChange(TObject *Sender)
{
	FFilePath = m_editFileName->Text;

    String fileExt = ExtractFileExt(m_editFileName->Text);

    if(FilePath.IsEmpty())
	{
		m_btnOk->Enabled = false;
    	return;
	}

	int iType = m_aTypeIndex[m_SaveTypeCombo->DefItemIndex];

	if( iType==FILETYPE_CPJ ){
		// 保存工程
		if(fileExt.CompareIC(L".cpj") == 0)
			m_btnOk->Enabled = true;
		else
			m_btnOk->Enabled = false;

		m_ProjPath = FFilePath;
	}
	else{
		bool bPostfixGood = true;
		if( iType==FILETYPE_MP3 ){
			if( fileExt.CompareIC(L".mp3") )
				bPostfixGood = false;
		}
		else if( iType==FILETYPE_WAVE ){
			if( fileExt.CompareIC(L".wav") )
				bPostfixGood = false;
		}
		else if( iType==FILETYPE_MIDI ){
			if( fileExt.CompareIC(L".mid") )
				bPostfixGood = false;
		}

		m_btnOk->Enabled = bPostfixGood && FMediaOption;
		m_MediaPath = FFilePath;
	}
}
//---------------------------------------------------------------------------

void __fastcall TSaveForm::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 TSaveForm::DoComboChange(TComboBoxItem *Sender, int Index)
{
	int iType = m_aTypeIndex[Index];

	if( m_aTypeIndex[0]!=FILETYPE_CPJ || iType==FILETYPE_CPJ ){
		m_ComposeCheck->Visible = false;
		m_SongCheck->Visible = false;
	}

	if( iType==FILETYPE_CPJ ){
		m_editFileName->Text = ChangeFileExt( m_ProjPath, L".cpj" );
	}
	else{
		m_editFileName->Text = ChangeFileExt(m_MediaPath, iType==FILETYPE_MP3 ? L".mp3"
														: iType==FILETYPE_WAVE ? L".wav"
														: L".mid");

		if( m_aTypeIndex[0]==FILETYPE_CPJ ){
			if( iType==FILETYPE_MIDI ){
				m_ComposeCheck->Visible = false;
				m_ComposeCheck->Checked = true;

				m_SongCheck->Visible = false;
				m_SongCheck->Checked = false;
			}
			else{
				m_ComposeCheck->Visible = true;
				m_SongCheck->Visible = true;
				m_SongCheck->Enabled = iType!=FILETYPE_MIDI
									&& (( g_iPreStage==0 && g_aAudioTracks[0]->lstBars.size()>0 )
										|| g_iPreStage>0 && g_aAudioTracks.size()>1 );
			}
		}
	}

	FSaveType = iType;

	FileNameChange(NULL);
}
//---------------------------------------------------------------------------

void __fastcall TSaveForm::DoComposeCheck(TObject *Sender)
{
	if( m_ComposeCheck->Checked ){
		FMediaOption |= SAVE_COMPOSE;
	}
	else{
		FMediaOption &= ~SAVE_COMPOSE;
	}

	m_btnOk->Enabled = FMediaOption!=0;
}
//---------------------------------------------------------------------------

void __fastcall TSaveForm::DoSongCheck(TObject *Sender)
{
	if( m_SongCheck->Checked ){
		FMediaOption |= SAVE_AUDIO;
	}
	else{
		FMediaOption &= ~SAVE_AUDIO;
    }

	m_btnOk->Enabled = FMediaOption!=0;
}
//---------------------------------------------------------------------------

void __fastcall TSaveForm::SetMediaOption(int Value)
{
    if(FMediaOption != Value)
	{
		FMediaOption = Value;
	}
}
//---------------------------------------------------------------------------

void __fastcall TSaveForm::FormCreate(TObject *Sender)
{
	DialogPanel->Font->Size = g_szFont9;
	m_btnOk->FontSize = g_szFont9;
	m_btnBrowse->FontSize = g_szFont9;
	m_btnCancel->FontSize = g_szFont9;
	m_SongCheck->Font->Size = g_szFont9;
	m_ComposeCheck->Font->Size = g_szFont9;
	m_SaveTypeCombo->SetFont(g_strTxtFontName, g_szFont9);
	m_editFileName->SetTextFont(g_strTxtFontName, g_szFont9);

	m_ComposeCheck->Checked = (bool)(FMediaOption & SAVE_COMPOSE);
	m_SongCheck->Checked = (bool)(FMediaOption & SAVE_AUDIO) && ( g_iPreStage>0 && g_aAudioTracks.size()>1
								|| g_iPreStage==0 && g_aAudioTracks[0]->lstBars.size()>0 );

	int ind = -1;

	for( int i=0; i<4; i++ ){
		if( m_aTypeIndex[i]==FILETYPE_CPJ ){
			m_SaveTypeCombo->AddItem("工程文件(*.cpj)");
		}
		else if( m_aTypeIndex[i]==FILETYPE_MP3 ){
			m_SaveTypeCombo->AddItem("MP3文件(*.mp3)");
		}
		else if( m_aTypeIndex[i]==FILETYPE_WAVE ){
			m_SaveTypeCombo->AddItem("WAVE文件(*.wav)");
		}
		else if( m_aTypeIndex[i]==FILETYPE_MIDI ){
			m_SaveTypeCombo->AddItem("MIDI文件(*.mid))");
		}
		else{
			break;
		}

		if( ind<0 || g_iLastSaveType==m_aTypeIndex[i] )
			ind = i;
	}

	m_SaveTypeCombo->DefItemIndex = ind;

	DoComboChange(NULL, ind);
}
//---------------------------------------------------------------------------

