// LKSampleMSRDlg.cpp : implementation file
//

#include "stdafx.h"
#include "LKSampleMSR.h"
#include "LKSampleMSRDlg.h"

#include "..\\include\\SEWOOP30.h" 

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

/////////////////////////////////////////////////////////////////////////////
// CLKSampleMSRDlg dialog

CLKSampleMSRDlg::CLKSampleMSRDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLKSampleMSRDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLKSampleMSRDlg)
	m_bCheck2 = FALSE;
	m_bCheck1 = FALSE;
	m_strMSRDATA1 = _T("");
	m_strMSRDATA2 = _T("");
	m_strMSRDATA3 = _T("");
	m_bTrackCheck1 = FALSE;
	m_bTrackCheck2 = FALSE;
	m_bTrackCheck3 = FALSE;
	m_strIP = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLKSampleMSRDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLKSampleMSRDlg)
	DDX_Control(pDX, IDC_EDIT_TRACK3, m_MSRDATA3);
	DDX_Control(pDX, IDC_EDIT_TRACK2, m_MSRDATA2);
	DDX_Control(pDX, IDC_EDIT_TRACK1, m_MSRDATA1);
	DDX_Check(pDX, IDC_CHECK2, m_bCheck2);
	DDX_Check(pDX, IDC_CHECK1, m_bCheck1);
	DDX_Text(pDX, IDC_EDIT_TRACK1, m_strMSRDATA1);
	DDX_Text(pDX, IDC_EDIT_TRACK2, m_strMSRDATA2);
	DDX_Text(pDX, IDC_EDIT_TRACK3, m_strMSRDATA3);
	DDX_Check(pDX, IDC_CHECK_TRACK1, m_bTrackCheck1);
	DDX_Check(pDX, IDC_CHECK_TRACK2, m_bTrackCheck2);
	DDX_Check(pDX, IDC_CHECK_TRACK3, m_bTrackCheck3);
	DDX_Text(pDX, IDC_EDIT_IP, m_strIP);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLKSampleMSRDlg, CDialog)
	//{{AFX_MSG_MAP(CLKSampleMSRDlg)
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
	ON_BN_CLICKED(IDC_CHECK_TRACK1, OnCheckTrack1)
	ON_BN_CLICKED(IDC_CHECK_TRACK2, OnCheckTrack2)
	ON_BN_CLICKED(IDC_CHECK_TRACK3, OnCheckTrack3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLKSampleMSRDlg message handlers

BOOL CLKSampleMSRDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	m_strIP = L"192.168.10.200";

	GetDlgItem(IDC_BUTTON1)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(false);

	m_bTrackCheck1 = TRUE;
	m_bTrackCheck2 = TRUE;
	m_bTrackCheck3 = FALSE;

	UpdateData(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CLKSampleMSRDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	
}

void CLKSampleMSRDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	KillTimer(2);
	CHAR	DataTrack1[256];
	CHAR	DataTrack2[256];
	CHAR	DataTrack3[256];
	long	status;

	memset(DataTrack1, 0, sizeof(DataTrack1));
	memset(DataTrack2, 0, sizeof(DataTrack2));
	memset(DataTrack3, 0, sizeof(DataTrack3));
    status = MSRReadTrack(DataTrack1, DataTrack2, DataTrack3);

	switch(status)
	{
	case 0:
		if(!m_bMode){
			m_strMSRDATA1 = L"";
			m_strMSRDATA2 = L"";
			m_strMSRDATA3 = L"";

			if(m_bTrackCheck1)
			{
				m_strMSRDATA1 = DataTrack1;
			}
			if(m_bTrackCheck2)
			{
				DataTrack2[0] = '*';
				DataTrack2[1] = '*';
				DataTrack2[2] = '*';
				DataTrack2[3] = '*';

				DataTrack2[12] = '*';
				DataTrack2[13] = '*';
				DataTrack2[14] = '*';
				DataTrack2[15] = '*';
				m_strMSRDATA2 = DataTrack2;
			}
			if(m_bTrackCheck3)
			{
				m_strMSRDATA3 = DataTrack3;
			}

			m_bMode = TRUE;

			m_bCheck2 = FALSE;

			KillTimer(2);
			UpdateData(FALSE);
			GetDlgItem(IDC_BUTTON1)->EnableWindow(true);
			GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
			MSRReadCancel();
		} else {
			m_strMSRDATA1 = L"Cancel to MSR Read";
			UpdateData(FALSE);
		}		
		return;
		break;
	case 1:
		if(!m_bMode){
			m_strMSRDATA1 = L"Check-Sum Error...";		
			UpdateData(FALSE);
			SetTimer(2,1000, NULL);	
		} else {
			m_strMSRDATA1 = L"Cancel to MSR Read";
			UpdateData(FALSE);
		}		
		return;		
		break;
	case 2:
		if(!m_bMode){
			m_strMSRDATA1 = L"Timeout Error...";
			m_strMSRDATA2 = L"Timeout Error...";
			m_strMSRDATA3 = L"Timeout Error...";
			UpdateData(FALSE);
			SetTimer(2,1000, NULL);	
		} else {
			m_strMSRDATA1 = L"Cancel to MSR Read";
			m_strMSRDATA2 = L"Cancel to MSR Read";
			m_strMSRDATA3 = L"Cancel to MSR Read";
			UpdateData(FALSE);
		}		
		return;
		break;
	}


	CDialog::OnTimer(nIDEvent);
}

void CLKSampleMSRDlg::OnButtonOpen() 
{
	// TODO: Add your control notification handler code here
    long	result;
	CString strPort;
	UpdateData(TRUE);

	result = OpenPort(m_strIP, 9100);

    if(result != 0) MessageBox(L"OpenPort Failed !!!", L"Error", MB_OK);
	else {
		GetDlgItem(IDC_BUTTON1)->EnableWindow(true);
		GetDlgItem(IDC_BUTTON2)->EnableWindow(true);
		GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(true);
		GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(false);
		UpdateData(TRUE);
	}
}

void CLKSampleMSRDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	long lResult;
	long	lTrack;

//	MSRSelectTrack(2); // Only Track2.
//	MSRSelectTrack(3); // Track1/2.
	lTrack = 0;

	if(m_bTrackCheck1) lTrack = lTrack | 0x01;
	if(m_bTrackCheck2) lTrack = lTrack | 0x02;
	if(m_bTrackCheck3) lTrack = lTrack | 0x04;

	MSRSelectTrack(lTrack);

	lResult = PrinterCheck();
	switch(lResult)
	{
	case -1: // Bluetooth Write Fail. Please Check Connection.
		AfxMessageBox(L"Write Error"); 
		return;
	case -2: 
		AfxMessageBox(L"Fail to Read Printer Status");
		return;
	}
	lResult = status();

	m_bCheck1 = FALSE;
	m_bCheck2 = FALSE;

	if( (lResult & LK_STS_PRINTEROFF)	== LK_STS_PRINTEROFF)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_MSR_READ)	== LK_STS_MSR_READ)
	{
		m_bCheck2 = TRUE;
	}

	if( (lResult & LK_STS_PAPER_EMPTY)	== LK_STS_PAPER_EMPTY)
	{
		m_bCheck1 = TRUE;
	}	

	if( (lResult & LK_STS_COVER_OPEN)	== LK_STS_COVER_OPEN)
	{
		m_bCheck1 = TRUE;
	}

	if( (lResult & LK_STS_BATTERY_LOW)	== LK_STS_BATTERY_LOW)
	{
		m_bCheck1 = TRUE;
	}

	UpdateData(FALSE);

	if((lResult & LK_STS_MSR_READ) != 0x40) {
		m_strMSRDATA1 = L"MSR Not Read Mode";
		UpdateData(FALSE);
		return;
	}


	KillTimer(2);
	m_bMode = FALSE;
	GetDlgItem(IDC_BUTTON1)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON2)->EnableWindow(true);

	SetTimer(2,1000, NULL);
}

void CLKSampleMSRDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	m_bMode = TRUE;

	m_bCheck2 = FALSE;

	KillTimer(2);
	m_strMSRDATA1 = L"- Cancel to MSR Read";				
	m_strMSRDATA2 = L"- Cancel to MSR Read";				
	m_strMSRDATA3 = L"- Cancel to MSR Read";				
	m_bCheck2 = FALSE;
	UpdateData(FALSE);
	GetDlgItem(IDC_BUTTON1)->EnableWindow(true);
	GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
	MSRReadCancel();
}

void CLKSampleMSRDlg::OnButtonClose() 
{
	// TODO: Add your control notification handler code here
    long	result;
	result = ClosePort();

    if(result != 0) MessageBox(L"ClosePort Failed !!!", L"Error", MB_OK);
	else {
		GetDlgItem(IDC_BUTTON1)->EnableWindow(false);
		GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
		GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(false);
		GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(true);
		UpdateData(TRUE);
	}

}

void CLKSampleMSRDlg::OnCheckTrack1() 
{
	// TODO: Add your control notification handler code here
	int	checked;

	checked = IsDlgButtonChecked(IDC_CHECK_TRACK1);
	if(checked)
	{
		if(!m_bTrackCheck3)
		{
			m_bTrackCheck1 = TRUE;
		} else {
			MessageBox(L"Track1 and Track3 Setting's Error", L"Error", MB_OK);
			m_bTrackCheck1 = FALSE;
		}
	}
	else
	{
		m_bTrackCheck1 = FALSE;
	}
	
	UpdateData(FALSE);
}

void CLKSampleMSRDlg::OnCheckTrack2() 
{
	// TODO: Add your control notification handler code here
	int	checked;

	checked = IsDlgButtonChecked(IDC_CHECK_TRACK2);
	if(checked)
	{
		m_bTrackCheck2 = TRUE;
	}
	else
	{
		m_bTrackCheck2 = FALSE;
	}
	
	UpdateData(FALSE);
}

void CLKSampleMSRDlg::OnCheckTrack3() 
{
	// TODO: Add your control notification handler code here
	int	checked;

	checked = IsDlgButtonChecked(IDC_CHECK_TRACK3);
	if(checked)
	{
		if(!m_bTrackCheck1)
		{
			m_bTrackCheck3 = TRUE;
		} else {
			MessageBox(L"Track1 and Track3 Setting's Error", L"Error", MB_OK);
			m_bTrackCheck3 = FALSE;
		}
	}
	else
	{
		m_bTrackCheck3 = FALSE;
	}

	UpdateData(FALSE);
}
