// MFCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFC.h"
#include "MFCDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMFCDlg dialog

CMFCDlg::CMFCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMFCDlg::IDD, pParent)
	, m_strReceivedata(_T(""))
	, m_strMsg(_T(""))
	, m_strIPaddr(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMFCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, m_strReceivedata);
	DDX_Control(pDX, IDC_COMBO1, m_cb1);
	DDX_Control(pDX, IDC_COMBO2, m_portCtrl);
	DDX_Control(pDX, IDC_COMBO3, m_baudCtrl);
	DDX_Control(pDX, IDC_COMBO4, m_msrCtrl);
	DDX_Text(pDX, IDC_ANSWER, m_strMsg);
	DDX_Text(pDX, IDC_IPADDRESS1, m_strIPaddr);
}

BEGIN_MESSAGE_MAP(CMFCDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_CBN_SELCHANGE(IDC_COMBO1, &CMFCDlg::OnCbnSelchangeCombo1)
	ON_BN_CLICKED(IDC_BUTTON12, &CMFCDlg::OnBnClickedButton12)
	ON_BN_CLICKED(IDC_BUTTON13, &CMFCDlg::OnBnClickedButton13)
	ON_BN_CLICKED(IDC_BUTTON5, &CMFCDlg::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON2, &CMFCDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CMFCDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &CMFCDlg::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON6, &CMFCDlg::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON7, &CMFCDlg::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON8, &CMFCDlg::OnBnClickedButton8)
	ON_BN_CLICKED(IDC_BUTTON9, &CMFCDlg::OnBnClickedButton9)
	ON_BN_CLICKED(IDC_BUTTON10, &CMFCDlg::OnBnClickedButton10)
	ON_BN_CLICKED(IDC_BUTTON11, &CMFCDlg::OnBnClickedButton11)
END_MESSAGE_MAP()


// CMFCDlg message handlers

BOOL CMFCDlg::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

	// TODO: Add extra initialization here
	
	m_cb1.SetCurSel(0);	
	OnCbnSelchangeCombo1();
	m_portCtrl.SetCurSel(0);
	m_baudCtrl.SetCurSel(3);
	m_msrCtrl.SetCurSel(1);

	
	GetDlgItem(IDC_IPADDRESS1)->SetWindowText(_T("192.168.1.101"));
	GetDlgItem(IDC_PORTNUM)->SetWindowText(_T("1470"));

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

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CMFCDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	if (AfxIsDRAEnabled())
	{
		DRA::RelayoutDialog(
			AfxGetResourceHandle(), 
			this->m_hWnd, 
			DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_EZ2TEST_DLL_CE_DIALOG) : 
			MAKEINTRESOURCE(IDD_EZ2TEST_DLL_CE_DIALOG));
	}
}
#endif


LRESULT CMFCDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	WPARAM chk;
	UINT length;
	// TODO: Add your specialized code here and/or call the base class
if(message == ::RegisterWindowMessage(MSG_RECEIVE_CHECK))
	{
		chk = HIWORD(wParam);
		length = LOWORD(wParam);
BYTE * tempBuff;CString temp;BYTE *data; 
		switch (chk)
		{

		case STATUS_ANSWER:
			
			tempBuff = (BYTE *)lParam;
			m_strMsg = _T("STATUS_ANSWER");
			m_strReceivedata.Format(_T("%02X"), tempBuff[0]);
			break;
		
		case DATA:		
			
			temp = _T("");
			strDataHex =  _T("");
			data= (BYTE *)lParam;
			m_strMsg = _T("DATA");
			
			strDataHex.Format(_T("Receive %dbyte\r\n"), length ); 
			for(int i=0; i < length; i++)
			{
				if(i%8 == 0)
					strDataHex = strDataHex + _T("\r\n");
				temp.Format(_T("%02X "),data[i]);
				strDataHex = strDataHex + temp;
			}

			m_strReceivedata = strDataHex;	

			break;
		}
		UpdateData(FALSE);
	}
	return CDialog::DefWindowProc(message, wParam, lParam);
}

void CMFCDlg::OnCbnSelchangeCombo1()
{
	// TODO: Add your control notification handler code here
	if(m_cb1.GetCurSel() == 0) //Serial or Bluetooth
	{
		GetDlgItem(IDC_IPADDRESS1)->ShowWindow(false);
		GetDlgItem(IDC_PORTNUM)->ShowWindow(false);
		GetDlgItem(IDC_STATIC_PORT2)->ShowWindow(false);
		GetDlgItem(IDC_STATIC_PORT)->ShowWindow(true);
		GetDlgItem(IDC_STATIC_BAUD)->ShowWindow(true);
		m_portCtrl.ShowWindow(true);
		m_baudCtrl.ShowWindow(true);

	}
	else //WiFi
	{
		GetDlgItem(IDC_IPADDRESS1)->ShowWindow(true);
		GetDlgItem(IDC_PORTNUM)->ShowWindow(true);
		GetDlgItem(IDC_STATIC_PORT2)->ShowWindow(true);
		GetDlgItem(IDC_STATIC_PORT)->ShowWindow(false);
		GetDlgItem(IDC_STATIC_BAUD)->ShowWindow(false);
		m_portCtrl.ShowWindow(false);
		m_baudCtrl.ShowWindow(false);

	}
}

void CMFCDlg::OnBnClickedButton12()
{
	// TODO: Add your control notification handler code here
	if(m_cb1.GetCurSel() == 0)	//Serial
	{
		int connectStatus = 0;
		CString CSportName,CSbaudRate;		
		m_portCtrl.GetLBText(m_portCtrl.GetCurSel(),CSportName);
		m_baudCtrl.GetLBText(m_baudCtrl.GetCurSel(),CSbaudRate);
		int bautRate = _ttoi(CSbaudRate);

		CSportName +=":";
		connectStatus = ConnectSerialPrinter((TCHAR*)(LPCTSTR)CSportName,bautRate);

		switch(connectStatus)
		{
			case SUCCESS:
				m_strMsg = _T("SUCCESS");
				break;
			case ALREADY_OPENED : 
				m_strMsg = _T("ALREADY OPEND");
				break;
			case UNABLE_TO_OPEN_THE_PORT : 
				m_strMsg = _T("UNABLE TO OPEN THE PORT");
				break;
			case UNABLE_TO_CONFIGURE_THE_SERIAL_PORT : 
				m_strMsg = _T("UNABLE_TO_CONFIGURE_THE_SERIAL_PORT");
				break;
			case UNABLE_TO_SET_THE_TIMEOUT_PARAMETERS : 
				m_strMsg = _T("UNABLE_TO_SET_THE_TIMEOUT_PARAMETERS");
				break;
			case NO_RESPONSE_FROM_PRINTER : 
				m_strMsg = _T("NO_RESPONSE_FROM_PRINTER");
				break;
			case TIMEOUT:
				m_strMsg = _T("TIMEOUT");
				break;
		}
	}
	if(m_cb1.GetCurSel() == 1) // Wifi
	{	
		int connectStatus = 0;
		CString ipaddr;
		CString CSiPortnum;
		CString err;

		GetDlgItem(IDC_IPADDRESS1)->GetWindowTextW(ipaddr);
		
		TCHAR* Wlan_buff = new TCHAR[strlen(CT2A(ipaddr))+1];
		_tcscpy(Wlan_buff, (ipaddr));

		GetDlgItem(IDC_PORTNUM)->GetWindowText(CSiPortnum);
		int portnum = _ttoi(CSiPortnum);

		connectStatus = ConnectWirelessPrinter(Wlan_buff,portnum,1000,FALSE);			

		switch(connectStatus)
		{
		case 1:
			m_strMsg = _T("SUCCESS");
			break;
		case -1:
			m_strMsg = _T("SOCKET ERROR");
			break;
		case -2:
			m_strMsg = _T("CONNECT FAIL");
			break;
		case -3:
			m_strMsg = _T("ALREADY CONNECTED");
			break;
		case -7:
			m_strMsg = _T("TIMEOUT");
			break;
		}
	}
	UpdateData(FALSE);
}

void CMFCDlg::OnBnClickedButton13()
{
	// TODO: Add your control notification handler code here
	ClosePrinterConnection();
	m_strMsg = _T("Connection closed");
	UpdateData(FALSE);
}

void CMFCDlg::OnBnClickedButton5()
{
	// TODO: Add your control notification handler code here
	//Initialization & make page
	InitPageMode(0,0,_2INCH,_3INCH);

	//Draw Black & long box
	Page_DrawBox(_2INCH-80,30,30,150,20);
	Page_DrawBox(_2INCH-80,200,30,_3INCH-200-30,1);
	
	//Contained text "TEXT"
	Page_DrawBox(214,224,50,258,1);
	
	//Boxes in Box
	Page_DrawBox(60,234,104,44,1);
	Page_DrawBox(60,312,28,150,1);
	Page_DrawBox(98,312,28,150,1);
	Page_DrawBox(136,312,28,150,1);
	Page_DrawBox(50,224,124,258,3);

	//Draw vertical lines
	Page_DrawBox(205,30,0,140,1);
	Page_DrawBox(235,30,0,140,1);
	Page_DrawBox(265,30,0,140,1);

	//Draw horizontal lines
	Page_DrawBox(185,55,100,0,1);
	Page_DrawBox(185,95,100,0,1);
	Page_DrawBox(185,135,100,0,1);

	//Draw outline-Box
	Page_DrawBox(30,0,_2INCH - 60, _3INCH, 1 );

	Page_SetDirection(3);// Clockwise rotation
	Page_SetPosition(214,30);//Y_pos, X_pos
	SetTextStyle(0,false,1,1,false);
	TextSaveSpool(L"Text");
	Page_SetPosition(270,70);
	QRCodeSaveSpool(0,'H',2,L"www.woosim.com/english");

	SetTextStyle(0,true,1,0,false);
	Page_SetPosition(126,256); //Y_pos, X_pos
	TextSaveSpool(L"TEXT");

	Page_Print_StandardMode();
	PrintSpool();
}

void CMFCDlg::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
	InitPrinterStatus();

	// TODO: Add your control notification handler code here
	TextSaveSpool(L"   매 출 전 표\r\n" );
	TextSaveSpool(L"--------------------------------\r\n");

	//Company
	TextSaveSpool(L"상점명             Woosim Coffee\r\n");
	TextSaveSpool(L"사업자등록번호      119-81-36382\r\n");
	//Reg Number
	TextSaveSpool(L"대표자                  이 일 복\r\n");
	//Address
	TextSaveSpool(L"서울특별시 금천구 가산동 448번지\r\n대륭테크노타운 3차, 501호\r\n");
	//Tel
	TextSaveSpool(L"문의                02-2107-3709\r\n");
	TextSaveSpool(L"--------------------------------\r\n\r\n");
	TextSaveSpool(L"    우심 비자 카드\r\n");
	TextSaveSpool(L"--------------------------------\r\n");
	//Card number
	TextSaveSpool(L"카드번호     9445 4103 9533 2096\r\n");
	TextSaveSpool(L"유효 기간                2012/10\r\n");
	TextSaveSpool(L"--------------------------------\r\n");
	TextSaveSpool(L"거 래 일 자           2011/04/11\r\n");
	TextSaveSpool(L"거 래 종 류       [신용거래승인]\r\n");
	TextSaveSpool(L"단 말 번 호            123456789\r\n");   
	TextSaveSpool(L"승 인 번 호            987654321\r\n");
	TextSaveSpool(L"결 제 금 액             50,000원\r\n");
	TextSaveSpool(L"--------------------------------\r\n");
	TextSaveSpool(L"서명: \r\n\r\n\r\n\r\n");
	TextSaveSpool(L"--------------------------------\r\n\r\n");
	
	PrintSpool();
}

void CMFCDlg::OnBnClickedButton3()
{
	// TODO: Add your control notification handler code here	
	{
		InitPrinterStatus();
		//BMP file path is started root folder. It is NOT the same folder as the excutable file.
				
		//Normal BMP file print
		TextSaveSpool(L"Print Bmp file \"test.bmp\"");	PrintData();
		NormalBmpSaveSpool(_T("./woosim.bmp"));
		PrintLineFeed(2);

		//BMP file print using Compressed algorithm
		TextSaveSpool(L"Print Bmp file using compressed algorithm");	PrintData();
		CompressedBmpSaveSpool(L"./woosim.bmp");
		PrintLineFeed(2);

		//Print pre-download image.
		TextSaveSpool(L"Print pre-downloaded image file #0");	PrintData();
		LoadLogoSaveSpool(0);	PrintData();

		PrintSpool();
	}
}

void CMFCDlg::OnBnClickedButton4()
{
	// TODO: Add your control notification handler code here
	InitPrinterStatus();
	TextSaveSpool(L"UPCA\r\n");
	OneDimensionBarcodeSaveSpool(UPCA, 2, 100, true, L"012100000642"); PrintData();

	TextSaveSpool(L"UPCE\r\n");
	OneDimensionBarcodeSaveSpool(UPCE, 2, 100, true, L"065100004328"); PrintData();

	TextSaveSpool(L"EAN13\r\n");
	OneDimensionBarcodeSaveSpool(EAN13, 2, 100, true, L"123456478901"); PrintData();

	TextSaveSpool(L"EAN8\r\n");
	OneDimensionBarcodeSaveSpool(EAN8, 2, 100, true, L"12345678"); PrintData();

	TextSaveSpool(L"CODE39\r\n");
	OneDimensionBarcodeSaveSpool(CODE39, 2, 100, true, L"12345678901"); PrintData();

	TextSaveSpool(L"ITF\r\n");
	OneDimensionBarcodeSaveSpool(ITF, 2, 100, true, L"123456"); PrintData();

	TextSaveSpool(L"CODABAR\r\n");
	OneDimensionBarcodeSaveSpool(CODABAR, 2, 100, true, L"123456"); PrintData();

	TextSaveSpool(L"CODE93\r\n");
	OneDimensionBarcodeSaveSpool(CODE93, 2, 100, true, L"12345678901"); PrintData();

	TextSaveSpool(L"CODE128\r\n");
	OneDimensionBarcodeSaveSpool(CODE128, 2, 100, true, L"12345678901"); PrintData();

	PrintSpool();
}

void CMFCDlg::OnBnClickedButton6()
{
	// TODO: Add your control notification handler code here
	InitPrinterStatus();

	TextSaveSpool(L"PDF417");
	PrintData();
	PDF417SaveSpool(2,2,2,2,L"0123456789",false);
	PrintData();

	TextSaveSpool(L"DataMatrix");
	PrintData();
	DataMatrixSaveSpool(36,36,2,L"0123456789");
	PrintLineFeed(1);

	TextSaveSpool(L"QR Code");
	PrintData();
	QRCodeSaveSpool(0,'H',3,L"0123456789");
	PrintDotFeed(30);
	
	TextSaveSpool(L"Micro PDF417");
	PrintData();
	PDF417SaveSpool(2,1,0,2,L"01234567",false);
	PrintDotFeed(30);

	TextSaveSpool(L"Truncated PDF417");
	PrintData();
	PDF417SaveSpool(2,2,2,2,L"0123456789",false);
	PrintData();

	TextSaveSpool(L"Maxicode");
	PrintData();
	MaxicodeSaveSpool(4,L"0123456789");
	PrintData();

	PrintSpool();
}

void CMFCDlg::OnBnClickedButton7()
{
	// TODO: Add your control notification handler code here
	GetPrinterStatus(1000);
}

void CMFCDlg::OnBnClickedButton8()
{
	// TODO: Add your control notification handler code here
	GetPrinterModelName();
	GetFirmwareVersion();
}

void CMFCDlg::OnBnClickedButton9()
{
	// TODO: Add your control notification handler code here
	InitPageMode(0,0,_2INCH,700);
	SetTextStyle(0,true,1,1,false);
	TextSaveSpool(L"Woosim Cafe\r\n");

	SetTextStyle(0,false,0,0,false);
	TextSaveSpool(L"Coffee, tea and bread\r\n");
	Page_DotFeed(15);
	SetFontSize(1);
	Page_SetPosition(277,105);
	TextSaveSpool(L"2015-01-27\r\n");
	SetFontSize(0);

	Page_SetPosition(290,5);
	QRCodeSaveSpool(0,'H',2,L"www.woosim.com/english");
	
	Page_DrawBox(0,130,_2INCH,0,2);
	Page_DotFeed(5);
	TextSaveSpool(L" #      Item      Num    Charge\r\n");
	TextSaveSpool(L" 1      Latte      1     $ 3.35");	Page_Newline();
	TextSaveSpool(L" 2  House Coffee   2     $ 3.98");	Page_Newline();
	TextSaveSpool(L" 3       Tea	       1     $ 1.95");	Page_DotFeed(10);
	Page_DrawBox(0,270,_2INCH,0,2);	Page_Newline();

	Page_DrawBox(0,310,115,0,2); Page_DrawBox(260,310,124,0,2); Page_DotFeed(15);
	Page_SetPosition(123,300);
	TextSaveSpool(L"Credit Card\r\n");

	TextSaveSpool(L"Number:         ");
	SetFontSize(1);
	TextSaveSpool(L"6688-9988-6644-3311");	Page_Newline();
	SetFontSize(0);
	TextSaveSpool(L"Payment:                $ 9.50 ");	Page_Newline();

	Page_DrawBox(0,400,120,0,2);	Page_DrawBox(250,400,134,0,2);Page_DotFeed(15);
	Page_SetPosition(145,390);
	TextSaveSpool(L"Payment\r\n");

	TextSaveSpool(L"Price:                  $ 9.28");	Page_Newline();
	TextSaveSpool(L"Tax:                    $ 0.22");	Page_Newline();

	Page_DrawBox(0,490,_2INCH,0,2);	Page_DotFeed(15);
	TextSaveSpool(L"Total:                  $ 9.50");	Page_Newline();
	TextSaveSpool(L"Credit Card:            $ 9.50");	Page_Newline();

	Page_SetPosition(40,580);
	OneDimensionBarcodeSaveSpool(CODE128,2,50,TRUE,L"20150127123456789");
	Page_LineFeed(2);

	Page_Print_StandardMode();
	PrintSpool();
}

void CMFCDlg::OnBnClickedButton10()
{
	// TODO: Add your control notification handler code here
	int modeSelect = 0;

	//MSR sample is based 12Track magnetic card
	switch(m_msrCtrl.GetCurSel())
	{
	case 0:
		modeSelect = 0;//Read 1T. If 23T card, read 2T.
		break;
	
	case 1:
		modeSelect = 1;//Read 2T. If 23T card, read 3T.
		break;
	
	case 2:
		modeSelect = 4;//Read 3T. It only use for 123T.
		break;
	
	case 3:
		modeSelect = 2;//Read 12T. If 23T card, read 23T.
		break;
	
	case 4:
		modeSelect = 3;//Read 123T. It only use for 123T.
		break;
	}

	EnterMSRMode(modeSelect);
}

void CMFCDlg::OnBnClickedButton11()
{
	// TODO: Add your control notification handler code here
	CancelMSRMode();
}
