// PPCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PPC.h"
#include "PPCDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPPCDlg dialog

CPPCDlg::CPPCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPPCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPPCDlg)
	m_addr = _T("");
	m_strRecvData = _T("");
	m_portNum = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPPCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPPCDlg)
	DDX_Control(pDX, IDC_COMBO4, m_ccb4);
	DDX_Control(pDX, IDC_COMBO3, m_ccb3);
	DDX_Control(pDX, IDC_COMBO2, m_ccb2);
	DDX_Control(pDX, IDC_COMBO1, m_ccb1);
	DDX_Text(pDX, IDC_EDIT1, m_addr);
	DDX_Text(pDX, IDC_EDIT3, m_strRecvData);
	DDX_Text(pDX, IDC_EDIT2, m_portNum);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPPCDlg, CDialog)
	//{{AFX_MSG_MAP(CPPCDlg)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON12, OnButton12)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
	ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
	ON_BN_CLICKED(IDC_BUTTON11, OnButton11)
	ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPPCDlg message handlers

BOOL CPPCDlg::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
	
	GetDlgItem(IDC_EDIT1)->SetWindowTextW(L"192.168.1.66");
	GetDlgItem(IDC_EDIT2)->SetWindowTextW(L"1470");
	m_ccb2.SetCurSel(4); m_ccb3.SetCurSel(3); m_ccb4.SetCurSel(1);
	m_ccb1.SetCurSel(0); OnSelchangeCombo1();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



LRESULT CPPCDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	int size = 0; int i=0;
	CString HexdataString;
	WPARAM chk; UINT length;
	if( message == ::RegisterWindowMessage(MSG_RECEIVE_CHECK))
	{
		chk = HIWORD(wParam);
		length = LOWORD(wParam);

		switch(chk)
		{
		case STATUS_ANSWER:
			BYTE *tempBuff;
			tempBuff = (BYTE*)lParam;
			GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("STATUS ANSWER"));
			m_strRecvData.Format(_T("%02X"),tempBuff[0]);
			break;

		case DATA:
			CString temp;
			BYTE *data = (BYTE*)lParam;
			GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("DATA"));
			m_strRecvData.Format(_T("Receive %dbyte"),length);

			for(i=0;i<length;i++)
			{
				if(i%8 == 0)
					HexdataString+=_T("\r\n");
				temp.Format(_T("%02X "),data[i]);
				HexdataString+=temp;
			}
			m_strRecvData += HexdataString;

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

void CPPCDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
		int status = 0;
	CString connStr,tempStr;
	int ttoiValue = 0;
	int selectItem = m_ccb1.GetCurSel();
	int selectPort = m_ccb2.GetCurSel();
	int selectBaud = m_ccb3.GetCurSel();

	if(selectItem == 0)
	{
		m_ccb2.GetLBText(selectPort,connStr);
		m_ccb3.GetLBText(selectBaud,tempStr);
		ttoiValue = _ttoi(tempStr);

		status = ConnectSerialPrinter((TCHAR*)(LPCTSTR)connStr,ttoiValue,1000,FALSE);

		switch(status)
		{
		case SUCCESS:
			GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("SUCCESS"));
			break;
		case ALREADY_OPENED:
			GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("ALREADY_OPENED"));
			break;
		case UNABLE_TO_OPEN_THE_PORT:
			GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("UNABLE_TO_OPEN_THE_PORT"));
			break;
		case UNABLE_TO_CONFIGURE_THE_SERIAL_PORT:
			GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("UNABLE_TO_CONFIGURE_THE_SERIAL_PORT"));
			break;
		case UNABLE_TO_SET_THE_TIMEOUT_PARAMETERS:
			GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("UNABLE_TO_SET_THE_TIMEOUT_PARAMETERS"));
			break;
		case NO_RESPONSE_FROM_PRINTER:
			GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("NO_RESPONSE_FROM_PRINTER"));
			break;
		case TIMEOUT:
			GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("TIMEOUT"));
			break;
		}
	}
	else if(selectItem == 1)
	{
		CString addrStr, portStr;
		
		GetDlgItem(IDC_EDIT1)->GetWindowTextW(addrStr);
		TCHAR *Wlan_buff = addrStr.GetBuffer(addrStr.GetLength());
		m_addr.ReleaseBuffer();

		GetDlgItem(IDC_EDIT2)->GetWindowTextW(portStr);
		int port = _ttoi(portStr);

		status = ConnectWirelessPrinter(Wlan_buff,port,1000);

		switch(status)
		{
		case SUCCESS:
			GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("SUCCESS"));
			break;
		case ALREADY_OPENED:
			GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("ALREADY_OPENED"));
			break;
		case SOCKET_ERROR:
			GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("SOCKET_ERROR"));
			break;
		case CONNECT_FAIL:
			GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("CONNECT_FAIL"));
			break;
		case NO_RESPONSE_FROM_PRINTER:
			GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("NO_RESPONSE_FROM_PRINTER"));
			break;
		case TIMEOUT:
			GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("TIMEOUT"));
			break;
		default:
			CString err_num;
			err_num.Format(_T("%d"),status);
			GetDlgItem(IDC_STATUS)->SetWindowTextW(err_num);
			break;
		}
	}
	UpdateData(FALSE);
}

void CPPCDlg::OnButton12() 
{
	// TODO: Add your control notification handler code here
	ClosePrinterConnection();
	GetDlgItem(IDC_STATUS)->SetWindowTextW(_T("Connection Closed"));
	UpdateData(FALSE);
}

void CPPCDlg::OnButton2() 
{
	// 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();

	Page_SetDirection(0);
	InitPageMode(0,0,_2INCH,_2INCH);
	
	Page_DrawBox(0,30,_2INCH,0,5);
	Page_DrawBox(30,0,0,_2INCH,5);
	Page_DrawLine(30,30,170,160,3);
	Page_DrawLine(170,160,130,190,3);
	Page_DrawLine(130,190,210,250,3);
	Page_DrawLine(210,250,190,270,3);
	Page_DrawLine(190,270,270,340,3);
	Page_DrawLine(270,340,210,370,3);
	Page_Newline();
	Page_Print();

	Page_ClearCurrentData();
	Page_DrawBox(0,_1INCH,_2INCH,0,5);
	Page_DrawBox(_1INCH,0,0,_2INCH,5);
	Page_DrawBox(_1INCH/2,_1INCH,_1INCH,0,0);
	Page_DrawEllipse(_1INCH,_1INCH,_1INCH/2,_1INCH,3);
	Page_Print_StandardMode();

	PrintSpool();
}

void CPPCDlg::OnButton4() 
{
	// 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 CPPCDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	InitPrinterStatus();

	//Normal BMP file print
	TextSaveSpool(L"Print Bmp file \"woosim.bmp\"");	PrintData();	
	NormalBmpSaveSpool(_T("./woosim.bmp"));
	PrintData();

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

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

	PrintSpool();
}

void CPPCDlg::OnButton6() 
{
	// 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 CPPCDlg::OnButton3() 
{
	// 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 CPPCDlg::OnButton7() 
{
	// TODO: Add your control notification handler code here
	GetPrinterStatus(1000);
}

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

void CPPCDlg::OnButton9() 
{
	// 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 CPPCDlg::OnButton10() 
{
	// TODO: Add your control notification handler code here
	int modeSelect = 0;

	//MSR sample is based 12Track magnetic card
	switch(m_ccb4.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 CPPCDlg::OnButton11() 
{
	// TODO: Add your control notification handler code here
	CancelMSRMode();
}

void CPPCDlg::OnSelchangeCombo1() 
{
	// TODO: Add your control notification handler code here
	int selectItem = m_ccb1.GetCurSel();

	if(selectItem == 0)
	{
		GetDlgItem(IDC_STATIC1)->SetWindowTextW(L"Port");
		GetDlgItem(IDC_STATIC2)->SetWindowTextW(L"Baud");
		GetDlgItem(IDC_COMBO2)->ShowWindow(true);
		GetDlgItem(IDC_COMBO3)->ShowWindow(true);
		GetDlgItem(IDC_EDIT1)->ShowWindow(false);
		GetDlgItem(IDC_EDIT2)->ShowWindow(false);
	}
	else if(selectItem == 1)
	{
		GetDlgItem(IDC_STATIC1)->SetWindowTextW(L"IPAddr");
		GetDlgItem(IDC_STATIC2)->SetWindowTextW(L"Port");
		GetDlgItem(IDC_COMBO2)->ShowWindow(false);
		GetDlgItem(IDC_COMBO3)->ShowWindow(false);
		GetDlgItem(IDC_EDIT1)->ShowWindow(true);
		GetDlgItem(IDC_EDIT2)->ShowWindow(true);
	}	
}
