// LoadTapeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "LoadTapeDlg.h"
#include "Config.h"
#include "Tape.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLoadTapeDlg

IMPLEMENT_DYNAMIC(CLoadTapeDlg, CFileDialog)

CLoadTapeDlg::CLoadTapeDlg(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
                           DWORD dwFlags, LPCTSTR lpszFilter, CWnd *pParentWnd, DWORD dwSize, BOOL bVistaStyle) :
	CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd, dwSize, bVistaStyle)
{
	m_ofn.hwndOwner = pParentWnd->GetSafeHwnd();
	m_ofn.Flags |= OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLETEMPLATE;
	m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_LOAD_TAPE);
	m_ofn.lpfnHook = OFNHookProc;
	m_ofn.lCustData = reinterpret_cast<LPARAM>(this);
}


BEGIN_MESSAGE_MAP(CLoadTapeDlg, CFileDialog)
END_MESSAGE_MAP()



UINT_PTR CALLBACK CLoadTapeDlg::OFNHookProc(HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
	static CRect rectHD;
	auto obj = reinterpret_cast<CLoadTapeDlg *>(lParam);

	switch (uiMsg)
	{
		case WM_INITDIALOG:
		{
			::GetWindowRect(hdlg, &rectHD);
			break;
		}

		case WM_CHILDACTIVATE:
		{
			HWND hParent = ::GetParent(hdlg);
			CRect rectFD;
			BOOL bres = ::GetWindowRect(hParent, &rectFD);
			CRect rectHD1;
			bres = ::GetWindowRect(hdlg, &rectHD1);
			//увеличим окно в ширину
			bres = ::MoveWindow(hParent, rectFD.left, rectFD.top, rectFD.Width() + rectHD.Width(), rectFD.Height() - rectHD.Height(), FALSE);
			//переместим превьюшку на своё новое место
			bres = ::MoveWindow(hdlg, rectFD.Width() - 20, -355, rectHD.Width(), rectHD1.Height(), FALSE);
			break;
		}

		case WM_NOTIFY:
		{
			auto pNm = reinterpret_cast<LPNMHDR >(lParam);

			if (pNm->code == CDN_SELCHANGE)
			{
				CString strPath;
				HWND hParent = ::GetParent(hdlg);
				::SendMessage(hParent, CDM_GETFILEPATH, 1024, reinterpret_cast<LPARAM>(strPath.GetBufferSetLength(1024)));
				strPath.ReleaseBuffer();
				CFileStatus fs;

				if (CFile::GetStatus(strPath, fs))
				{
					if (!(fs.m_attribute & CFile::directory))
					{
						CLoadTapeDlg::DrawPreview(hdlg, strPath);
					}
				}

				return 0;
			}
		}
		break;
	}

	return 0;
}


void CLoadTapeDlg::DrawPreview(HWND hdlg, CString strPath)
{
	static CTape tape;
	CWnd *pDlg = CWnd::FromHandle(hdlg);
	CString strType;
	bool bUnk = false;
	TAPE_FILE_INFO tfi;
	memset(&tfi, 255, sizeof(TAPE_FILE_INFO));

	if (tape.LoadWaveFile(strPath))
	{
		strType = _T("WAVE");
	}
	else if (tape.LoadMSFFile(strPath, true))
	{
		strType = _T("MSF TAPE");
	}
	else if (tape.LoadBinFile(strPath, &tfi))
	{
		strType = _T("BINARY");
	}
	else
	{
		strType = _T("UNKNOWN");
		bUnk = true;
	}

	// Set preview controls
	pDlg->GetDlgItem(IDC_LT_TYPE)->SetWindowText(strType);

	if (bUnk)
	{
		return;
	}

	// Get current wave file information
	tape.GetWaveFile(&tfi, true);
	CString strError = _T("Error");
	// Set file name
	if (tfi.name[0] == 255 && tfi.name[1] == 255)
	{
		pDlg->GetDlgItem(IDC_LT_NAME)->SetWindowText(strError);
	}
	else
	{
		pDlg->GetDlgItem(IDC_LT_NAME)->SetWindowText(BKToUNICODE(tfi.name, 16));
	}
	CString strWord;
	// Set file address
	if (tfi.address == 65535)
	{
		strWord = strError;
	}
	else
	{
		WordToOctString(tfi.address, strWord);
	}
	pDlg->GetDlgItem(IDC_LT_ADDRESS)->SetWindowText(strWord);
	// Set file length
	if (tfi.length == 65535)
	{
		strWord = strError;
	}
	else
	{
		WordToOctString(tfi.length, strWord);  //длина в байтах, а не длина ваве
	}
	pDlg->GetDlgItem(IDC_LT_LENGTH)->SetWindowText(strWord);
	auto timeLen = int(float(tape.GetWaveLength()) * 1000.0 / float(tape.GetWorkingSSR()));
	CString strTime = MsTimeToTimeString(timeLen);
	pDlg->GetDlgItem(IDC_LT_TIME)->SetWindowText(strTime);
}
