
// mPosDemoDlg.cpp : 구현 파일
//

#include "stdafx.h"
#include "mPosDemo.h"
#include "mPosDemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CmPosDemoDlg 대화 상자
CmPosDemoDlg *pDlg;



CmPosDemoDlg::CmPosDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CmPosDemoDlg::IDD, pParent)
	, m_strData(_T(""))
	, m_nInterface(0)
	, m_strPort(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	pDlg = this;
}

void CmPosDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_STATIC_DATA, m_strData);
	DDX_Text(pDX, IDC_EDIT2, m_strPort);
}

BEGIN_MESSAGE_MAP(CmPosDemoDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_RADIO3, &CmPosDemoDlg::OnBnClickedRadio3)
	ON_BN_CLICKED(IDC_RADIO4, &CmPosDemoDlg::OnBnClickedRadio4)
	ON_BN_CLICKED(IDC_RADIO5, &CmPosDemoDlg::OnBnClickedRadio5)
	ON_BN_CLICKED(IDC_BUTTON1, &CmPosDemoDlg::OnBnClickedButton1)
END_MESSAGE_MAP()


// CmPosDemoDlg 메시지 처리기

BOOL CmPosDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	SetIcon(m_hIcon, TRUE);			// 큰 아이콘을 설정합니다.
	SetIcon(m_hIcon, FALSE);		// 작은 아이콘을 설정합니다.

#if _DEBUG
	//set usb interface
 	CButton* pButton = (CButton*)GetDlgItem(IDC_RADIO5);
 	pButton->SetCheck(true); 

	m_nInterface = MPOS_INTERFACE_USB;
	GetDlgItem(IDC_IPADDRESS1)->ShowWindow(FALSE);
	GetDlgItem(IDC_EDIT2)->ShowWindow(TRUE);
	m_strPort = "";
#else
	CButton* pButton = (CButton*)GetDlgItem(IDC_RADIO3);
	pButton->SetCheck(true); 

	m_nInterface = MPOS_INTERFACE_ETHERNET;
	GetDlgItem(IDC_IPADDRESS1)->ShowWindow(TRUE);
	GetDlgItem(IDC_EDIT2)->ShowWindow(FALSE);
#endif

	GetDlgItem(IDC_IPADDRESS1)->SetWindowText(_T("192.168.0.10"));

	return TRUE; 
}

void CmPosDemoDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 그리기를 위한 디바이스 컨텍스트

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 클라이언트 사각형에서 아이콘을 가운데에 맞춥니다.
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 아이콘을 그립니다.
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// 사용자가 최소화된 창을 끄는 동안에 커서가 표시되도록 시스템에서
//  이 함수를 호출합니다.
HCURSOR CmPosDemoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CmPosDemoDlg::DisconnectmPos()
{
	m_Printer.closeService(1000);
	m_BCD.closeService(1000);
	m_Scanner.closeService(0);
}

long CmPosDemoDlg::ConnectmPos()
{
	UpdateData(TRUE);
	if(m_nInterface == MPOS_INTERFACE_ETHERNET) GetDlgItemText(IDC_IPADDRESS1, m_strPort);

	m_Printer.selectInterface(m_nInterface, m_strPort);
	m_Printer.selectCommandMode(MPOS_MODE_DEFAULT);	
	int result = m_Printer.openService();
	if(result != RES_SUCCESS) {
		CString strError;
		strError.Format(_T("Printer Error Code = %d"), result);
		AfxMessageBox(strError);
		return result;
	}
	m_Printer.asbEnable(TRUE); //asb enable.
	
	m_BCD.selectInterface(m_nInterface, m_strPort);
	m_BCD.selectCommandMode(MPOS_MODE_DEFAULT);	
	result = m_BCD.openService();
	if(result != RES_SUCCESS) {
		CString strError;
		strError.Format(_T("Customer Display Error Code = %d"), result);
		AfxMessageBox(strError);
		return result;
	}

	m_Scanner.selectInterface(m_nInterface, m_strPort);
	m_Scanner.selectCommandMode(MPOS_MODE_DEFAULT);	
	result = m_Scanner.openService();
	if(result != RES_SUCCESS) {
		CString strError;
		strError.Format(_T("Scanner Error Code = %d"), result);
		AfxMessageBox(strError);
		return result;
	}
	m_Scanner.setDataEvent(CmPosDemoDlg::DataEvent);

	return result;
}


void __stdcall CmPosDemoDlg::DataEvent(LPCTSTR data)
{
	TRACE(_T("data:%s, length:%d\n"), data, _tcslen(data));
	CString str;
	str.Format(_T("%s\n"), data);
	pDlg->SetDlgItemText(IDC_STATIC_DATA, str);
}

void __stdcall CmPosDemoDlg::StatusUpdateEvent(int nStatus)
{
	CString str;
	str.Format(_T("%s Status Update\n"), pDlg->getPrinterStatusString(nStatus));
	pDlg->SetDlgItemText(IDC_STATIC_DATA, str);
}


void CmPosDemoDlg::OnBnClickedRadio3()
{
	m_nInterface = MPOS_INTERFACE_ETHERNET;
	GetDlgItem(IDC_IPADDRESS1)->ShowWindow(TRUE);
	GetDlgItem(IDC_IPADDRESS1)->SetWindowText(_T("192.168.0.10"));
	GetDlgItem(IDC_EDIT2)->ShowWindow(FALSE);
	UpdateData(FALSE);
}

void CmPosDemoDlg::OnBnClickedRadio4()
{
	m_nInterface = MPOS_INTERFACE_BLUETOOTH;
	GetDlgItem(IDC_IPADDRESS1)->ShowWindow(FALSE);
	GetDlgItem(IDC_EDIT2)->ShowWindow(TRUE);
	m_strPort = "COM3";
	UpdateData(FALSE);
}

void CmPosDemoDlg::OnBnClickedRadio5()
{
	m_nInterface = MPOS_INTERFACE_USB;
	GetDlgItem(IDC_IPADDRESS1)->ShowWindow(FALSE);
	GetDlgItem(IDC_EDIT2)->ShowWindow(TRUE);
	m_strPort = _T("");
	UpdateData(FALSE);
}

void CmPosDemoDlg::OnBnClickedButton1()
{
	ConnectmPos();
	displayBCD();
	printReceipt();
}

void CmPosDemoDlg::displayBCD()
{
	m_BCD.clearScreen();
	m_BCD.displayString(_T("Subtotal      895.00"), BCD_CODEPAGE_CP437, POS_ICS_USA, 1);
	m_BCD.displayString(_T("Total       1,000.00"), BCD_CODEPAGE_CP437, POS_ICS_USA, 2);
}

void CmPosDemoDlg::printReceipt()
{
	m_Printer.setTransaction(MPOS_TRANSACTION_IN);
	m_Printer.printBitmapFile(_T("Logo.bmp"), -1, POS_ALIGNMENT_CENTER, 0, FALSE, FALSE);

	CString ESC = _T("\x1B");
	CString strPrint = _T("MiraeAsset Venture Tower 685,\nSampyeong-dong, Bundang-gu,\nSeongnam-si, Gyeonggi-do,\n463-400, Korea\n\n\n");
	m_Printer.printText(ESC + _T("|cA") + strPrint, 0, 0, 0, 0, 0, 0, 0);
	strPrint = _T("");
	strPrint += _T("------------------------------------------\n");
	strPrint += _T("Items 1                             100.00\n");
	strPrint += _T("Items 1                             100.00\n");
	strPrint += _T("Items 1                             100.00\n");
	strPrint += _T("Items 1                             100.00\n");
	strPrint += _T("Items 1                             100.00\n");
	strPrint += _T("Items 1                             100.00\n");
	strPrint += _T("Items 1                             100.00\n");
	strPrint += _T("Items 1                             100.00\n");
	strPrint += _T("Items 1                             100.00\n");
	strPrint += _T("Items 1                             100.00\n");
	strPrint += _T("------------------------------------------\n");
	strPrint += _T("                          Sub-Total 895.00\n");
	strPrint += _T("                          Discount    5.00\n");
	strPrint += _T("                          ----------------\n");
	strPrint += _T("                          Tax Total 100.00\n");
	strPrint += _T("                          ----------------\n");
	strPrint += _T("                          Total   1,000.00\n");
	strPrint += _T("                          ----------------\n");
	m_Printer.printText(strPrint, 0, 0, 0, 0, 0, 0, 0);

	strPrint = _T("\n\n\nwww.bixolon.com\nwww.bixolonusa.com\nwww.bixoloneu.com\n\n\n\n\n\n");
	m_Printer.printText(ESC + _T("|cA") + strPrint, 0, 0, 0, 0, 0, 0, 0);

	m_Printer.print1DBarcode(_T("1234567890123456"), POS_BARCODE_CODE128, 3, 100, POS_ALIGNMENT_CENTER, POS_BARCODE_TEXT_BELOW);
	
	m_Printer.cutPaper(POS_FEED_PARTIAL_CUT);
	m_Printer.setTransaction(MPOS_TRANSACTION_OUT);
	m_Printer.openDrawer(POS_DRAWER_PIN2, 200, 200);
}


CString CmPosDemoDlg::getPrinterStatusString(int status)
{
	if(POS_STATUS_COVEROPEN == status)
		return _T("Cover open");
	else if(POS_STATUS_PAPEREMPTY == status)
		return _T("Paper empty");
	else if(POS_STATUS_PAPER_NEAREND == status)
		return _T("Nearend Paper");
	else if(POS_STATUS_ERROR == status)
		return _T("Error");
	else if (POS_STATUS_DRAWER_HIGH == status)
		return _T("Drawer High");
	else if (POS_STATUS_DRAWER_LOW == status)
		return _T("Drawer Low");
	else// if(MPOS_STATUS_NORMAL == status)
		return _T("Normal");
}