/*****************************************************************************
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.
*****************************************************************************/

// CopyOptDlg.cpp: implementation of the CCopyOptDlg class.
//
//////////////////////////////////////////////////////////////////////

#include "Headers.h"
#include "resource.h"
#include "Settings.h"
#include "CopyOptDlg.h"
#include "ParentDlg.h"

CCopyOptDlg * pCopyOptDlg;

BOOL CopyOptDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	return pCopyOptDlg->OwnProc(hDlg, message, wParam, lParam);
}

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCopyOptDlg::CCopyOptDlg(HINSTANCE hInst, CSettings * pSet,
								         LPCTSTR sCapt1, LPCTSTR sCapt2,
                         ZWin * pOwner, ZWinAppData * pzwd)
	: ZDlg(hInst, sCapt1, sCapt2, pOwner, pzwd)
{
	ini = pSet;
	pCopyOptDlg = this;
	m_pProc = CopyOptDlgProc;
	m_uID = IDD_COPY_OPT;

  _tcscpy(m_sLogDir, pzwd->sAppDocsDirSlash);
  m_sLogDir[_tcslen(m_sLogDir) - 1] = _T('\0'); // Trim off trailing slash
}

CCopyOptDlg::~CCopyOptDlg()
{

}

BOOL CCopyOptDlg::OwnProc(HWND hDlg, UINT uMsg, WPARAM wP, LPARAM lP)
{
	switch (uMsg)
	{
		// Insert DTAKEMSG(WM_WHATEVER, OnWhatever); except for WM_COMMAND

// Don't bitch about no cases in WM_COMMAND's switch(wP) (warning 4065)
#pragma warning(push)
#pragma warning(disable : 4065)
		case WM_COMMAND:
			switch(LOWORD(wP))
			{
				// Insert DTAKECMD(IDC_BN_ETC, OnEtc);
        DTAKECMD(IDCO_BN_HELP, OnBnHelp);
// ==> MOVED TO LOG OPTIONS DIALOG
//        DSEECMDPARMS(IDCO_CK_LOG, OnCkLog);

				default : break;

			} // END switch(LOWORD(wP))
// Restore settings in effect at warning(push) above
#pragma warning(pop)

		default : break;
	}

	// ZDlg already calls OnInitDialog, OnOK, OnCancel, or returns FALSE
	return ZDlg::OwnProc(hDlg, uMsg, wP, lP);
}


BOOL CCopyOptDlg::OnInitDialog()
{
	SendMessage(m_hWnd, WM_SETICON, ICON_SMALL,
							(LPARAM) LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_MAIN)));

  ini->SetDlg(m_hWnd);
  if (
// ==> MOVED TO LOG OPTIONS DIALOG
//      !ini->DlgCheckFromKey(ini->bArtLog, IDCO_CK_LOG) ||
//      // Just in case ini file messed up, make sure dependent log checkboxes right
//      !ini->DlgCheckFromKey(ini->bArtLogDetail && ini->bArtLog, IDCO_CK_LOG_DETAIL) ||
//      !ini->DlgCheckFromKey(ini->bArtLogFlush && ini->bArtLog, IDCO_CK_LOG_FLUSH) ||

      !ini->DlgCheckFromKey(ini->bArtSaveHide, IDCO_CK_SAVEHIDE) ||
// ==> Removed Rips Folder option IDCO_RB_CRIP, but need to handle legacy ini settings
// that still have RB_SRC_RIP. Do that further down, can't handle with DlgRBsFromKey
//      !ini->DlgRBsFromKey(ini->uArtSrcDir, IDCO_RB_CLAST, IDCO_RB_CRIP) ||
      !ini->DlgRBsFromKey(ini->uArtTgtDir, IDCO_RB_PMUSIC, IDCO_RB_POTHER) ||
      !ini->DlgEditFromKey(ini->sArtTgtDirOth, IDCO_ED_POTHER) )
  {
    ini->ShowError(m_sCapt, _T("Error applying ini file settings to dialog"));
  }

  // Replace above ini->DlgRBsFromKey(ini->uArtSrcDir, IDCO_RB_CLAST, IDCO_RB_CRIP)
  if (ini->uArtSrcDir == RB_SRC_LAST)
  {
    CheckBn(IDCO_RB_CLAST, TRUE);
    CheckBn(IDCO_RB_CLIB, FALSE);
  }
  else // only other option RB_SRC_LIB, or obsolete option RB_SRC_RIP
  {
    CheckBn(IDCO_RB_CLAST, FALSE);
    CheckBn(IDCO_RB_CLIB, TRUE);
  }


/*
// ==> MOVED TO LOG OPTIONS DIALOG
  m_hCkLogFlush = GetItem(IDCO_CK_LOG_FLUSH);
  m_hCkLogDetail = GetItem(IDCO_CK_LOG_DETAIL);
  // Disable if Log button unchecked:
  EnableWindow(m_hCkLogFlush, ini->bArtLog);
  EnableWindow(m_hCkLogDetail, ini->bArtLog);
*/

  TCHAR sWinLibDir[MAX_PATH];
	LPTSTR sKey =
    _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
  if (!ReadReg(HKEY_CURRENT_USER, sKey, _T("My Music"), sWinLibDir, FALSE))
    _tcscpy(sWinLibDir, _T("ERROR: Folder not found in registry settings"));
  SetItemText(IDCO_TX_CLIB, sWinLibDir);

/*
No longer option for Rips folder
  // This registry value may not exist. If not Media player defaults to My Music.
	sKey = _T("Software\\Microsoft\\MediaPlayer\\Preferences");
  if (!ReadReg(HKEY_CURRENT_USER, sKey, _T("SavedTrackFoldersRipDir"), sBS, FALSE))
    _tcscpy(sBS, sWinLibDir);
  SetItemText(IDCO_TX_CRIP, sBS);
*/

/*
// ==> MOVED TO LOG OPTIONS DIALOG
  _stprintf(sBS, _T("Save log file in \"%s\" folder"),
            m_sLogDir);
  SetItemText(IDCO_CK_LOG, sBS);
*/

  return ZDlg::OnInitDialog(); // always TRUE
}

/*
// ==> MOVED TO LOG OPTIONS DIALOG
void CCopyOptDlg::OnCkLog(WPARAM wP, LPARAM)
{
  if (HIWORD(wP) != BN_CLICKED) return; // Not what we're interested in

  // Always uncheck IDC_CK_LOG_FLUSH when uncheck IDC_CK_LOG; redundant on check 
  CheckBn(IDCO_CK_LOG_FLUSH, FALSE);
  CheckBn(IDCO_CK_LOG_DETAIL, FALSE);
  EnableWindow(m_hCkLogFlush, IsBnChecked(IDCO_CK_LOG));
  EnableWindow(m_hCkLogDetail, IsBnChecked(IDCO_CK_LOG));
}
*/

void CCopyOptDlg::OnBnHelp()
{
  CParentDlg * pParent =  (CParentDlg *) m_pOwner;
  pParent->ShowHelp(_T("aa_pref.htm"));
}

void CCopyOptDlg::OnOk()
{
  if (
// ==> MOVED TO LOG OPTIONS DIALOG
//      !ini->DlgCheckToKey(ini->bArtLog, IDCO_CK_LOG) ||
//      !ini->DlgCheckToKey(ini->bArtLogDetail, IDCO_CK_LOG_DETAIL) ||
//      !ini->DlgCheckToKey(ini->bArtLogFlush, IDCO_CK_LOG_FLUSH) ||

      !ini->DlgCheckToKey(ini->bArtSaveHide, IDCO_CK_SAVEHIDE) ||
//      !ini->DlgRBsToKey(ini->uArtSrcDir, IDCO_RB_CLAST, IDCO_RB_CRIP) ||
      !ini->DlgRBsToKey(ini->uArtSrcDir, IDCO_RB_CLAST, IDCO_RB_CLIB) ||
      !ini->DlgRBsToKey(ini->uArtTgtDir, IDCO_RB_PMUSIC, IDCO_RB_POTHER) ||
      !ini->DlgEditToKey(ini->sArtTgtDirOth, IDCO_ED_POTHER, TRUE) )
  {
    ini->ShowError(m_sCapt, _T("Error reading settings from dialog fields"));
    return;
  }

  if (!ini->WriteIniSection(ini->m_sectOptCopy))
  {
    ini->ShowError(m_sCapt, _T("Error saving ini settings"));
    return;
  }

	ZDlg::OnOk();
}
