// SettleDlg.cpp : インプリメンテーション ファイル
//

#include "stdafx.h"
#include "exp00069.h"
#include "SettleDlg.h"
#include "Exp00069Doc.h"
#include "MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSettleDlg ダイアログ


CSettleDlg::CSettleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSettleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSettleDlg)
	m_iState = -1;
	//}}AFX_DATA_INIT

	m_enableTimer = false;
	m_iState = 0;
}


void CSettleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSettleDlg)
	DDX_Radio(pDX, IDC_RADIO1, m_iState);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSettleDlg, CDialog)
	//{{AFX_MSG_MAP(CSettleDlg)
	ON_BN_CLICKED(IDC_BUTTON_SETTLE, OnButtonSettle)
	ON_BN_CLICKED(IDC_CHECK_TIMER, OnCheckTimer)
	ON_BN_CLICKED(IDC_BUTTON_UPDATE, OnButtonLoad)
	ON_WM_TIMER()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSettleDlg メッセージ ハンドラ

void CSettleDlg::OnButtonSettle() 
{
	SetState(0);

	CWaitCursor wc;
	try {
		fibres.close();

		if (!fibres.construct(false))
			throw int();

		SetState(1);
		validates();
	}
	catch (int) {
		SetState(2);
		validates();
	}
}

void CSettleDlg::OnCheckTimer() 
{
	if (!UpdateData())
		return;

	KillTimer(m_timer);

	if (m_enableTimer) {
		m_timer = SetTimer(0x0100, 1000, NULL);
	}
}

BOOL CSettleDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_timer = 0;
	m_tick = SetTimer(0x0101, 100, NULL);
	m_timer_re_ent = false;

	validates();
	
	return TRUE;  // コントロールにフォーカスを設定しないとき、戻り値は TRUE となります
	              // 例外: OCX プロパティ ページの戻り値は FALSE となります
}

void CSettleDlg::OnTimer(UINT nIDEvent) 
{
	REEntLock reent(m_timer_re_ent);

	if (reent.Lock()) {
		if (nIDEvent == 0x0100) {
			OnButtonLoad();
			return;
		}
		if (nIDEvent == 0x0101) {
			CWaitCursor wc;
			if (fibres.isActive()) {
				if (fibres.receiver()) {
					for (; !fibres.reses.empty(); fibres.reses.pop_front()) {
						Fibres::RES &res = fibres.reses.front();
						switch (res.resi) {
						case Fibres::resiTransit:
							{
								if (res.ok) {
									CMemFile f;
									f.Write(res.buff.begin(), res.buff.size());
									f.SeekToBegin();
									static_cast<CExp00069Doc *>(static_cast<CMainFrame *>(AfxGetMainWnd())->GetActiveDocument())->SettleLoad(f);
								}
								else {
									AfxMessageBox(IDS_ERR_SETTLE_TRANSIT, 0 |MB_ICONEXCLAMATION);
								}
								break;
							}
						}
					}
				}
				else {
					fibres.close();
					SetState(2);
					validates();
				}
			}
			return;
		}
	}
	
	CDialog::OnTimer(nIDEvent);
}

void CSettleDlg::OnClose() 
{
	ShowWindow(SW_HIDE);

//	CDialog::OnClose();
}

void CSettleDlg::enableWindows(const UINT *nIDs, bool f)
{
	while (*nIDs != 0) {
		CWnd *p = GetDlgItem(*nIDs);
		if (p != NULL) {
			p->EnableWindow(f);
		}
		nIDs++;
	}
}

void CSettleDlg::validates()
{
	static UINT nID_Update[] = {
		IDC_BUTTON_UPDATE,
		IDC_BUTTON_SAVE,
		IDC_CHECK_TIMER,
		IDC_STATIC_WARN,
		0,
	};
	enableWindows(nID_Update, fibres.isActive());
}

void CSettleDlg::OnButtonSave() 
{
	CWaitCursor wc;

	CMemFile f;
	static_cast<CExp00069Doc *>(static_cast<CMainFrame *>(AfxGetMainWnd())->GetActiveDocument())->SettleSave(f);

	DWORD buffSize = f.GetLength();
	BYTE *buff = f.Detach();

	fibres.sendSettleSave(buff, buffSize);

	free(buff);
}

void CSettleDlg::OnButtonLoad() 
{
	if (!fibres.isActive()) return;

	fibres.simpleOrder(1);
}
