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

// CLogOptDlg.cpp: implementation of the CLogOptDlg class.
//
//////////////////////////////////////////////////////////////////////

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

CLogOptDlg * pLogOptDlg;

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

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

CLogOptDlg::CLogOptDlg(HINSTANCE hInst, CSettings * pSet, LPCTSTR sCapt1, LPCTSTR sCapt2,
                 ZWin * pOwner, ZWinAppData * pzwd)
	: ZDlg(hInst, sCapt1, sCapt2, pOwner, pzwd)
{
	ini = pSet;
	pLogOptDlg = this;
	m_pProc = LogOptDlgProc;
	m_uID = IDD_LOG_OPT;
}

CLogOptDlg::~CLogOptDlg()
{

}

BOOL CLogOptDlg::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);
        DSEECMDPARMS(IDC_CK_LOG, OnCkShuffleLog);
        DSEECMDPARMS(IDCO_CK_LOG, OnCkCopyLog);
        DTAKECMD(IDCO_BN_HELP, OnBnHelp);

				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 CLogOptDlg::OnInitDialog()
{
	SendMessage(m_hWnd, WM_SETICON, TRUE,
							(LPARAM) LoadIcon(m_hInst, MAKEINTRESOURCE(IDI_MAIN)));

  ini->SetDlg(m_hWnd);

  if (
      // Moved from Shuffle Preferences dialog
      !ini->DlgCheckFromKey(ini->bLog, IDC_CK_LOG) ||
      // Just in case ini file messed up, make sure dependent log checkbox right
      !ini->DlgCheckFromKey(ini->bLogFlush && ini->bLog, IDC_CK_LOG_FLUSH) ||
      !ini->DlgCheckFromKey(ini->bElapsed, IDC_CK_ELAPSED) ||

      // Moved from Album Art Preferences 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->ShowError(m_sCapt, _T("Error applying ini file settings to dialog"));
  }

  // These are for shuffle logging
  m_hCkShuffleLogFlush = GetItem(IDC_CK_LOG_FLUSH);
  // Disable if Log button unchecked:
  EnableWindow(m_hCkShuffleLogFlush, ini->bLog);

  // For album art logging
  m_hCkCopyLogFlush = GetItem(IDCO_CK_LOG_FLUSH);
  m_hCkCopyLogDetail = GetItem(IDCO_CK_LOG_DETAIL);
  // Disable if Log button unchecked:
  EnableWindow(m_hCkCopyLogFlush, ini->bArtLog);
  EnableWindow(m_hCkCopyLogDetail, ini->bArtLog);

  _tcscpy(sBS, m_pzwd->sAppDocsDirSlash);
  sBS[_tcslen(sBS) - 1] = _T('\0'); // Trim off ending backslash
  SetItemText(IDCL_TXT_DOCS_FOLDER, sBS);

  // Change if don't want CenterWnd and caption m_sCaptLong
  return ZDlg::OnInitDialog(); // always TRUE
} // BOOL CLogOptDlg::OnInitDialog()

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

void CLogOptDlg::OnOk()
{
	// Delete here and in .h if not adding anything

  if (
      // Shuffle logging
      !ini->DlgCheckToKey(ini->bLog, IDC_CK_LOG) ||
      !ini->DlgCheckToKey(ini->bLogFlush, IDC_CK_LOG_FLUSH) ||
      !ini->DlgCheckToKey(ini->bElapsed, IDC_CK_ELAPSED) ||
      // Art copy logging
      !ini->DlgCheckToKey(ini->bArtLog, IDCO_CK_LOG) ||
      !ini->DlgCheckToKey(ini->bArtLogDetail, IDCO_CK_LOG_DETAIL) ||
      !ini->DlgCheckToKey(ini->bArtLogFlush, IDCO_CK_LOG_FLUSH) )
  {
    ini->ShowError(m_sCapt, _T("Error reading settings from dialog fields"));
    return;
  }

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

	ZDlg::OnOk();
}

void CLogOptDlg::OnCkShuffleLog(WPARAM wP, LPARAM lP)
{
  if (HIWORD(wP) != BN_CLICKED) return; // Not what we're interested in

  // Always uncheck IDC_CK_LOG_FLUSH when uncheck IDC_CK_LOG; don't care on check 
  CheckBn(IDC_CK_LOG_FLUSH, FALSE);
  EnableWindow(m_hCkShuffleLogFlush, IsBnChecked(IDC_CK_LOG));
}

void CLogOptDlg::OnCkCopyLog(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; don't care on check 
  CheckBn(IDCO_CK_LOG_FLUSH, FALSE);
  CheckBn(IDCO_CK_LOG_DETAIL, FALSE);
  EnableWindow(m_hCkCopyLogFlush, IsBnChecked(IDCO_CK_LOG));
  EnableWindow(m_hCkCopyLogDetail, IsBnChecked(IDCO_CK_LOG));
}


// Uncomment if used. Zdlg virtual handlers will call these if declared
//void CLogOptDlg::OnDestroy()
//{
//  // Delete here and in .h if no resources to free. Maybe use OnClose instead.
//  // ZDlg version does nothing, just for virtual WM_DESTROY handler in place.
//  // Windows' default handling happens after this.
//}

//void CLogOptDlg::OnDestroy()
//{
//  // Delete here and in .h if no resources to free. Maybe use OnDestroy instead.
//  // ZDlg version does nothing, just for virtual WM_CLOSE handler in place.
//  // Windows' default handling happens after this.
//}


