// imp_configwindow.cpp : implementation file
//

#include "stdafx.h"
//#include "bloadfile.h"
#include "imp_configwindow.h"
#include "imp_block.h"
#include "resource.h"

static WCHAR BASED_CODE g_szFilter[] = L"Kohonen Files (*.koh)|*.koh|" L"All Files (*.*)|*.*||";

// CImpBlockConfigWindow dialog

IMPLEMENT_DYNAMIC(CImpBlockConfigWindow, CDialog)

CImpBlockConfigWindow::CImpBlockConfigWindow(CWnd* pParent /*=NULL*/, IUnknown* pUnkOuter)
	: CDialog(CImpBlockConfigWindow::IDD, pParent)
	, m_strName(_T(""))
	, m_bCheckFile(FALSE)
	, m_bSkipEmptyLayers(FALSE)
	, m_bDisableInput(FALSE)
{
	m_pImpBlock = (CImpBlock*)pUnkOuter;
}

CImpBlockConfigWindow::~CImpBlockConfigWindow()
{
}

void CImpBlockConfigWindow::DoDataExchange(CDataExchange* pDX)
{
	EnterCriticalSection(&m_pImpBlock->m_cs);

	CDialog::DoDataExchange(pDX);
	DDX_Check(pDX, IDC_CHECKLEARN, m_pImpBlock->m_bLearnMode);
	LeaveCriticalSection(&m_pImpBlock->m_cs);

	if ( pDX->m_bSaveAndValidate )
		m_pImpBlock->SetModifiedFlag(TRUE);
	else
		OnUpdateStatus(0, 0);
}


BEGIN_MESSAGE_MAP(CImpBlockConfigWindow, CDialog)
	ON_BN_CLICKED(IDC_APPLY, &CImpBlockConfigWindow::OnBnClickedApply)
	ON_BN_CLICKED(IDC_BUTTONSAVE, &CImpBlockConfigWindow::OnBnClickedButtonsave)
	ON_BN_CLICKED(IDC_BUTTONLOAD, &CImpBlockConfigWindow::OnBnClickedButtonload)
	ON_BN_CLICKED(IDC_BUTTONCLEAR, &CImpBlockConfigWindow::OnBnClickedButtonclear)
	ON_MESSAGE(WM_UPDATESTATUS, &CImpBlockConfigWindow::OnUpdateStatus)
END_MESSAGE_MAP()


// CImpBlockConfigWindow message handlers

void CImpBlockConfigWindow::OnCancel()
{
	CDialog::OnCancel();
	DestroyWindow();
}

void CImpBlockConfigWindow::OnOK()
{
	CDialog::OnOK();
	DestroyWindow();
}

void CImpBlockConfigWindow::OnBnClickedApply()
{
	CDataExchange dx(this, TRUE);
	DoDataExchange(&dx);
}
void CImpBlockConfigWindow::OnBnClickedButtonsave()
{

	CFileDialog fd(FALSE, L".koh", NULL, 4 | 2, g_szFilter);
	switch ( fd.DoModal() )
	{
	case IDOK:
		CFile file(fd.GetPathName(), CFile::modeCreate | CFile::modeWrite);
		CArchive ar(&file, CArchive::store);
		EnterCriticalSection(&m_pImpBlock->m_cs);
		m_pImpBlock->m_netKohonen.Serialize(ar);
		LeaveCriticalSection(&m_pImpBlock->m_cs);
		ar.Close();
		file.Close();
		break;
	}
}

void CImpBlockConfigWindow::OnBnClickedButtonload()
{
	CFileDialog fd(TRUE, L".koh", NULL, 4|2, g_szFilter);
	switch ( fd.DoModal() )
	{
	case IDOK:
		CFile file(fd.GetPathName(), CFile::modeRead);
		CArchive ar(&file, CArchive::load);
		EnterCriticalSection(&m_pImpBlock->m_cs);
		m_pImpBlock->m_netKohonen.Serialize(ar);
		LeaveCriticalSection(&m_pImpBlock->m_cs);
		ar.Close();
		file.Close();
		break;
	}
}

void CImpBlockConfigWindow::OnBnClickedButtonclear()
{
	if ( IDYES == MessageBox(L"Are you sure?", L"Kohonen", MB_YESNO) )
	{
		EnterCriticalSection(&m_pImpBlock->m_cs);
		m_pImpBlock->m_netKohonen.Clear();
		LeaveCriticalSection(&m_pImpBlock->m_cs);
	}
}

LRESULT CImpBlockConfigWindow::OnUpdateStatus(WPARAM wParam, LPARAM lParam)
{
	DWORD dwValue;
	EnterCriticalSection(&m_pImpBlock->m_cs);
	dwValue = m_pImpBlock->m_netKohonen.GetNeuronCount();
	LeaveCriticalSection(&m_pImpBlock->m_cs);

	CString str;
	str.Format(L"%d", dwValue);
	
	CEdit* pEdit = (CEdit*)GetDlgItem(IDC_STATICNEURONS);
	pEdit->SetWindowText(str);
	return 0;
}