/**************************************************************************
 * Copyright (C) 2008 Cocha                                               *
 * http://sourceforge.jp/projects/ecodecotool/                            *
 *                                                                        *
 *  This Program is free software; you can redistribute it and/or modify  *
 *  it under the terms of the GNU General Public License as published by  *
 *  the Free Software Foundation; either version 2, or (at your option)   *
 *  any later version.                                                    *
 *                                                                        *
 *  This Program is distributed in the hope that it will be useful,       *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          *
 *  GNU General Public License for more details.                          *
 *                                                                        *
 *  You should have received a copy of the GNU General Public License     *
 *  along with GNU Make; see the file COPYING.  If not, write to          *
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. *
 *                                                                        *
 **************************************************************************/

#include "EcoDecoTooL.h"

// ----------------------------------------------------------------------------------------------------------------------------
int test(void)
{
   // デバッグモードならメモリリークの検出をする
   #ifdef _DEBUG
      _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
   #endif

   ::CoInitialize(NULL);

   CLinkedList *pDropLinkedList = NULL;

   // リンクリストの作成
   try
   {
      pDropLinkedList = new CLinkedList();
   }
   catch(std::bad_alloc & e)
   {
      return 0;
   }

   pDropLinkedList->AddList(L"d:\\input_raw4");

   int nLanguage;
   nLanguage = 1;

   SETTINGDATA settingData;
   ::ZeroMemory(&settingData, sizeof(settingData));

   //::lstrcpy(convertData.awFileExt, L".wav");

/*
settingData.nOutputFolder = 2;
::lstrcpy(settingData.awOutputFolder, L"d:\\debug2");
settingData.nContainerType = CONTAINER_WAV;
::ZeroMemory(&settingData.wf, sizeof(settingData.wf));
//settingData.nGainMode = 2;
settingData.nGainMode = 4;
//convertData.dGain = 89.0;
settingData.dGain = 70.0;
*/

/*
settingData.nOutputFolder = 2;
::lstrcpy(settingData.awOutputFolder, L"d:\\debug2");
settingData.nContainerType = CONTAINER_AAC;
settingData.nEncodeMode = 1;
settingData.nAdvancedMode = 0;
settingData.nABRCBR = 128000;
settingData.nGainMode = 1;
settingData.dGain = 70.0;
*/

/*
settingData.nOutputFolder = 2;
::lstrcpy(settingData.awOutputFolder, L"d:\\debug2");
settingData.nContainerType = CONTAINER_MP3_FRAUNHOFER;
settingData.nEncodeMode = 1;
settingData.nAdvancedMode = 0;
settingData.nABRCBR = 128000;
settingData.nGainMode = 4;
settingData.dGain = 89.0;
*/

/*
settingData.nOutputFolder = 2;
::lstrcpy(settingData.awOutputFolder, L"d:\\debug2");
settingData.nContainerType = CONTAINER_MP3_LAME;
settingData.nEncodeMode = 1;
settingData.nAdvancedMode = 0;
settingData.nABRCBR = 128;
//settingData.nABRCBR = 128000;
settingData.nGainMode = 4;
settingData.dGain = 70.0;
*/
/*
settingData.nOutputFolder = 2;
::lstrcpy(settingData.awOutputFolder, L"d:\\debug2");
settingData.nContainerType = CONTAINER_OGG;
settingData.nEncodeMode = 1;
settingData.nAdvancedMode = 0;
settingData.nABRCBR = 128000;
//settingData.nABRCBR = 128000;
settingData.nGainMode = 4;
settingData.dGain = 70.0;
*/

settingData.nOutputFolder = 2;
::lstrcpy(settingData.awOutputFolder, L"d:\\debug2");
settingData.nContainerType = CONTAINER_RAW;

   CConvertDialog *pConvertDialog = new CConvertDialog();
   pConvertDialog->ShowDialog(&settingData, nLanguage, pDropLinkedList);
   SAFE_DELETE(pConvertDialog);


   SAFE_DELETE(pDropLinkedList);

   ::CoUninitialize();

   // メモリリークを出力ウィンドウに出力する
   #ifdef _DEBUG
      _CrtDumpMemoryLeaks();
   #endif

   return 0;
}
// ----------------------------------------------------------------------------------------------------------------------------
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
// return test();

   // デバッグモードならメモリリークの検出をする
   #ifdef _DEBUG
      _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
   #endif

   // 二重起動の防止
   HANDLE hMutex = ::OpenMutex(MUTEX_ALL_ACCESS, false, L"EcoDecoTooL");
   if(hMutex!=0)
   {
      ::CloseHandle(hMutex);
      return 0;
   }

   hMutex = ::CreateMutex(NULL, true, L"EcoDecoTooL");

   // COMの初期化
   ::CoInitialize(NULL);

   // 言語を設定する
   int nLanguage = 1;


   bool flag = false;
   SETTINGDATA settingData;
   ::ZeroMemory(&settingData, sizeof(settingData));

   CMainDialog    *pMainDialog    = NULL;
   CConvertDialog *pConvertDialog = NULL;
   CLinkedList *pDropLinkedList = NULL;

   // メモリ作成
   try
   {
      pMainDialog = new CMainDialog();
      pConvertDialog = new CConvertDialog();
      pDropLinkedList = new CLinkedList();
   }
   catch(std::bad_alloc & e)
   {
      ::MessageBox(NULL, L"Error", L"EcoDecoTooL error", MB_OK);
      return 0;
   }

   // コマンドライン引数を取得
   int nArgc;
   WCHAR **ppwArgv = NULL;
   ppwArgv = ::CommandLineToArgvW(::GetCommandLine(), &nArgc);

   if(nArgc <= 1)
   {  // コマンドライン引数が指定されていない場合

      // メインダイアログを表示する
      pMainDialog->ShowDialog(&settingData, nLanguage, pDropLinkedList);
   }
   else
   {  // コマンドライン引数が指定されている場合

      // iniファイルから設定情報を取得する
      flag = pMainDialog->LoadSetting(&settingData);

      // コマンドライン引数のファイルを格納する
      for(int i=1;i<nArgc;i++)
         pDropLinkedList->AddList(ppwArgv[i]);
   }

   if(0 < pDropLinkedList->GetCount())
   {
      // 変換する
      pConvertDialog->ShowDialog(&settingData, nLanguage, pDropLinkedList);
   }

   // 終了処理
   ::CoUninitialize();
   ::ReleaseMutex(hMutex);

   SAFE_DELETE(pDropLinkedList);
   SAFE_DELETE(pConvertDialog);
   SAFE_DELETE(pMainDialog);

   // メモリリークを出力ウィンドウに出力する
   #ifdef _DEBUG
      _CrtDumpMemoryLeaks();
   #endif

   return 0;
}
// -------------------------------------------------------------------------------------------------------------------------------------------
