/*****************************************************************************
Open Source MIT License

Copyright (c) 2017 Matthew Ahmed

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*****************************************************************************/

#include "Headers.h"  // Leave as is for MFC, includes stdafx
#include "Resource.h"
#include "Minimal.h"

#include "Settings.h"

////- USUALLY DELETE ONE OF BELOW INCLUDES
//#include "MainWin.h"

//#include "MinDlg.h"
#include "ParentDlg.h"


//////////////// Begin Insert for MFC    ////////////////////
BEGIN_MESSAGE_MAP(CShakerApp, CWinApp)
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

CShakerApp::CShakerApp()
{
}

CShakerApp theApp;

BOOL CShakerApp::InitInstance()
{
  HINSTANCE hInstance = AfxGetInstanceHandle();
//////////////// End Insert for MFC    ////////////////////

// INCLUDES ENTRY FUNCTION NAME ABOVE REPLACE COMMENTED OUT JUST BELOW:

//int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
//																		LPTSTR lpCmdLine, int nCmdShow)
//{
	////- Set Caption (Declared in Minimal.h for use in inlines)
	gsCapt = _T("Shaken, not stirred");
	ghMain = NULL;


#if defined(_WIN32_DCOM)  // Defined in Headers.h for For SHBrowseForFolder etc 
  if (!ZWin::CoInitDcom())
    return fnErr(_T("Error initializing DCOM"));
#endif;

  // Uncomment whatever is used. Set 0 or comment section if not using any
  DWORD dwFlagsCommonControls = ICC_UPDOWN_CLASS |
                                ICC_PROGRESS_CLASS |
                                ICC_BAR_CLASSES | // Tooltips
                                ICC_LISTVIEW_CLASSES;
  if (dwFlagsCommonControls)
  {
    INITCOMMONCONTROLSEX icc;
    icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icc.dwICC = dwFlagsCommonControls;
    if (!InitCommonControlsEx(&icc))
      return fnErr(_T("Error initializing common controls"));
  }


  // Get info for directory paths
  //TCHAR sProgDirSlash[_MAX_PATH];
  //TCHAR sAppDataDirSlash[_MAX_PATH];
  //TCHAR sAppDocsDirSlash[_MAX_PATH];
  //TCHAR sAppExeNameNoDotExt[_MAX_PATH];

  ZWinAppData zwd; // This will persist as target of ZWin member pointers

//  if (! GetAppDirsProgDataDocs(gsProgDirSlash, gsAppDataDirSlash, gsAppDocsDirSlash,
  if (! GetAppPathsInfo(zwd.sProgDirSlash, zwd.sAppDataDirSlash,
                    zwd.sAppDocsDirSlash, zwd.sExeNameNoDotExt,
                    _T("Shaken, not stirred"), // use exe name for subfolder name if NULL
                    TRUE)) // Boolean all files in prog dir if it's in user profile dir
    return fnErr(_T("Error getting output dir names"), zwd.sProgDirSlash);

//MsgB((HWND) NULL, _T("Dir names prog, AppData, AppDocs, ExeNameNoDotExt:"),
//     zwd.sProgDirSlash, zwd.sAppDataDirSlash, zwd.sAppDocsDirSlash, zwd.sExeNameNoDotExt);

  // Before any possible ini file activity, create ini file's directory if doesn't exist.
  if (!EnsureDirExists(zwd.sAppDataDirSlash))
  {
    TCHAR sErr[2048];
    UtilGetError(sErr);
    return fnErr(_T("Error ensuring config dir exists"), zwd.sAppDataDirSlash, sErr);
  }

	CSettings settings;
	if (!settings.Init(zwd.sAppDataDirSlash,
                     NULL)) ////- Can set ini name, e.g., "ThisProg.ini"
		return settings.ShowError(gsCapt, _T("Error loading INI settings"));

  // Currently settings.nReps<...> variables have the last used values.
  // Reset to defaults if preference is to not remember across sessions.
  if (settings.uRememberReps != UREPS_REMEMBER_ALWAYS)
  {
    // Is either UREPS_REMEMBER_NO or UREPS_REMEMBER_SESSION
    settings.nReps1 = settings.nReps1Default; // Frequency for song ratings, 1-5 stars
    settings.nReps2 = settings.nReps2Default;
    settings.nReps3 = settings.nReps3Default;
    settings.nReps4 = settings.nReps4Default;
    settings.nReps5 = settings.nReps5Default;
    settings.nRepsSame = settings.nRepsSameDefault; // Option all songs same frequency
  }

	CParentDlg * pDlg;
	pDlg = NULL;
//	pDlg = new CMinDlg(hInstance, &settings, gsCapt, NULL, NULL);
	pDlg = new CParentDlg(hInstance, &settings, gsCapt, NULL, NULL, &zwd);
	if (!pDlg) return fnErr(_T("Error creating dialog"));
	pDlg->Run();

	////- else (i.e., MinDlg app) Delete all below, and replace with:
	return 0; // Uncomment for MinDlg app
	////-      and also delete Tray and MainWind source files

	//MSG msg;
	//CMainWin * pMain;
	//LPCTSTR sClassName = "MinimalityClass";

	//pMain = new CMainWin(hInstance, &settings, gsCapt); // ADD &zwd?
	//if (!pMain) return fnErr("Error creating MainWindow");

	//												////- SW_HIDE for tray app, or dlg app w hidden main
	//if (!pMain->Create(sClassName, SW_SHOWNORMAL)) return 0;	// Msg already

	//////- Add only if need to know this hWnd
	//// ghMain = pMain->GetWnd();

	//while (GetMessage(&msg, NULL, 0, 0) > 0)
	//{
	//	TranslateMessage(&msg);
	//	DispatchMessage(&msg);
	//}
	//return(msg.wParam);

} // END WinMain

