
// ProjectDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Project.h"
#include "ProjectDlg.h"
#include "afxdialogex.h"

#include "WOOSIM_PRINTER_LIB.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

static const UINT UWM_RECEIVE_DATA = ::RegisterWindowMessage(MSG_RECEIVE_CHECK);

// CProjectDlg dialog



CProjectDlg::CProjectDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_PROJECT_DIALOG, pParent)
	, m_dwIpAddress(0xc0a80a01)
	, m_tcpPort(9100)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CProjectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RADIO_SERIAL, m_rdoSerial);
	DDX_Control(pDX, IDC_RADIO_USB, m_rdoUSB);
	DDX_Control(pDX, IDC_RADIO_TCPIP, m_rdoTcpIp);
	DDX_Control(pDX, IDC_STATIC_PORT, m_staticPort);
	DDX_Control(pDX, IDC_STATIC_BAUDRATE, m_staticBaud);
	DDX_Control(pDX, IDC_STATIC_IP, m_staticIp);
	DDX_Control(pDX, IDC_STATIC_TCP_PORT, m_staticTcpPort);
	DDX_CBString(pDX, IDC_COMBO_PORT, m_strPort);
	DDX_Control(pDX, IDC_COMBO_PORT, m_cboPort);
	DDX_Text(pDX, IDC_COMBO_BAUDRATE, m_BaudRate);
	DDX_Control(pDX, IDC_COMBO_BAUDRATE, m_cboBaudRate);
	DDX_IPAddress(pDX, IDC_IPADDRESS, m_dwIpAddress);
	DDX_Control(pDX, IDC_IPADDRESS, m_ctrlIpAddress);
	DDX_Text(pDX, IDC_EDIT_TCP_PORT, m_tcpPort);
	DDX_Control(pDX, IDC_EDIT_TCP_PORT, m_editTcpPort);
	DDX_Text(pDX, IDC_STATIC_COM_STATUS, m_strComStatus);
	DDX_Text(pDX, IDC_EDIT_RCV_DATA, m_strRcvData);
	DDX_Control(pDX, IDC_COMBO_MSR, m_cboMsr);
}

BEGIN_MESSAGE_MAP(CProjectDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_REGISTERED_MESSAGE(UWM_RECEIVE_DATA, OnReceiveData)
	ON_BN_CLICKED(IDC_RADIO_SERIAL, &CProjectDlg::OnBnClickedRadioSerial)
	ON_BN_CLICKED(IDC_RADIO_USB, &CProjectDlg::OnBnClickedRadioUsb)
	ON_BN_CLICKED(IDC_RADIO_TCPIP, &CProjectDlg::OnBnClickedRadioTcpip)
	ON_BN_CLICKED(IDC_BUTTON_OPEN, &CProjectDlg::OnBnClickedButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE_PORT, &CProjectDlg::OnBnClickedButtonClosePort)
	ON_BN_CLICKED(IDC_BUTTON_MSR_SET, &CProjectDlg::OnBnClickedButtonMsrSet)
	ON_BN_CLICKED(IDC_BUTTON_MSR_CANCEL, &CProjectDlg::OnBnClickedButtonMsrCancel)
	ON_BN_CLICKED(IDC_BUTTON_TEXT, &CProjectDlg::OnBnClickedButtonText)
	ON_BN_CLICKED(IDC_BUTTON_BARCODE1, &CProjectDlg::OnBnClickedButtonBarcode1)
	ON_BN_CLICKED(IDC_BUTTON_BARCODE2, &CProjectDlg::OnBnClickedButtonBarcode2)
	ON_BN_CLICKED(IDC_BUTTON_TTF, &CProjectDlg::OnBnClickedButtonTtf)
	ON_BN_CLICKED(IDC_BUTTON_PAGEMODE, &CProjectDlg::OnBnClickedButtonPagemode)
	ON_BN_CLICKED(IDC_BUTTON_IMAGE, &CProjectDlg::OnBnClickedButtonImage)
	ON_BN_CLICKED(IDC_BUTTON_BOX_LINE, &CProjectDlg::OnBnClickedButtonBoxLine)
	ON_BN_CLICKED(IDC_BUTTON_STATUS, &CProjectDlg::OnBnClickedButtonStatus)
	ON_BN_CLICKED(IDC_BUTTON_INFO, &CProjectDlg::OnBnClickedButtonInfo)
END_MESSAGE_MAP()


// CProjectDlg message handlers

BOOL CProjectDlg::OnInitDialog()
{
	CDialogEx::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

	m_rdoSerial.SetCheck(1);
	m_cboPort.SetCurSel(0);				// COM1
	m_cboBaudRate.SetCurSel(3);			// 57600
	OnBnClickedRadioSerial();

	m_cboMsr.SetCurSel(3);				// Triple Track
	mConnectionStatus = 0;

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

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CProjectDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

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

		// Center icon in client rectangle
		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;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CProjectDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CProjectDlg::OnClose()
{
	OnBnClickedButtonClosePort();
	CDialogEx::OnClose();
}

void CProjectDlg::OnBnClickedRadioSerial()
{
	m_staticPort.ShowWindow(SW_SHOW);
	m_staticBaud.ShowWindow(SW_SHOW);
	m_cboPort.ShowWindow(SW_SHOW);
	m_cboBaudRate.ShowWindow(SW_SHOW);

	m_staticIp.ShowWindow(SW_HIDE);
	m_staticTcpPort.ShowWindow(SW_HIDE);
	m_ctrlIpAddress.ShowWindow(SW_HIDE);
	m_editTcpPort.ShowWindow(SW_HIDE);
}

void CProjectDlg::OnBnClickedRadioUsb()
{
	m_staticPort.ShowWindow(SW_HIDE);
	m_staticBaud.ShowWindow(SW_HIDE);
	m_cboPort.ShowWindow(SW_HIDE);
	m_cboBaudRate.ShowWindow(SW_HIDE);

	m_staticIp.ShowWindow(SW_HIDE);
	m_staticTcpPort.ShowWindow(SW_HIDE);
	m_ctrlIpAddress.ShowWindow(SW_HIDE);
	m_editTcpPort.ShowWindow(SW_HIDE);
}

void CProjectDlg::OnBnClickedRadioTcpip()
{
	m_staticPort.ShowWindow(SW_HIDE);
	m_staticBaud.ShowWindow(SW_HIDE);
	m_cboPort.ShowWindow(SW_HIDE);
	m_cboBaudRate.ShowWindow(SW_HIDE);

	m_staticIp.ShowWindow(SW_SHOW);
	m_staticTcpPort.ShowWindow(SW_SHOW);
	m_ctrlIpAddress.ShowWindow(SW_SHOW);
	m_editTcpPort.ShowWindow(SW_SHOW);
}

void CProjectDlg::OnBnClickedButtonOpen()
{
	if (mConnectionStatus == SUCCESS)
		return;

	UpdateData(TRUE);

	if (m_rdoSerial.GetCheck())
	{
		char serialPort[10];
		memset(serialPort, 0x00, 10);
		memcpy(serialPort, CT2A(m_strPort), m_strPort.GetLength());

		mConnectionStatus = ConnectSerialPrinter(serialPort, m_BaudRate);
	}
	else if (m_rdoUSB.GetCheck())
	{
		mConnectionStatus = ConnectUSBPrinter();
	}
	else if (m_rdoTcpIp.GetCheck())
	{
		CString	strIpAddress;
		strIpAddress.Format(_T("%d.%d.%d.%d"), (m_dwIpAddress >> 24) & 0xff, (m_dwIpAddress >> 16) & 0xff, (m_dwIpAddress >> 8) & 0xff, m_dwIpAddress & 0xff);

		char address[20];
		memset(address, 0x00, 20);
		memcpy(address, CT2A(strIpAddress), strIpAddress.GetLength());

		mConnectionStatus = ConnectWirelessPrinter(address, m_tcpPort);
	}

	if (mConnectionStatus == SUCCESS)
	{
		m_strComStatus = _T("Connection success");
	}
	else
	{
		CString strError;
		strError.Format(_T("Connection is failed with return value (%d)"), mConnectionStatus);
		m_strComStatus = strError;
	}

	UpdateData(FALSE);
}

void CProjectDlg::OnBnClickedButtonClosePort()
{
	if (mConnectionStatus == SUCCESS)
	{
		ClosePrinterConnection();
		mConnectionStatus = 0;

		m_strComStatus = _T("Connection closed");
		UpdateData(FALSE);
	}
}

LRESULT CProjectDlg::OnReceiveData(WPARAM wParam, LPARAM lParam)
{
	UINT type = HIWORD(wParam);
	UINT length = LOWORD(wParam);
	BYTE* pData = (BYTE*)lParam;
	CString strTemp;

	m_strRcvData.Empty();
	switch (type)
	{
	case STATUS_ANSWER:
		m_strRcvData.Format(_T("%02X"), pData[0]);
		break;

	case DATA:
		m_strRcvData.Format(_T("Receive %d bytes\r\n\r\n"), length);
		for (UINT i = 0; i < length; i++)
		{
			strTemp.Format(_T("%c"), pData[i]);
			m_strRcvData += strTemp;
		}
		break;
	}

	UpdateData(FALSE);
	return TRUE;
}


void CProjectDlg::OnBnClickedButtonText()
{
	InitPrinterStatus();
	SetTextAlignment(1);
	TextSaveSpool("SALES  SLIP\n");
	SetTextAlignment(0);
	TextSaveSpool("--------------------------------\n");
	TextSaveSpool("MERCHANT NAME      Woosim Coffee\n");
	TextSaveSpool("MASTER                LEE IL BOK\n");
	TextSaveSpool("ADDRESS\n");
	TextSaveSpool("     #60, Sandan-ro 388beon-gil,\n");
	TextSaveSpool("    Galsan-myeon, Hongseong-gun,\n");
	TextSaveSpool("       Chungnam-do, 32200, Korea\n");
	TextSaveSpool("HELP DESK        +82-41-339-3700\n");
	TextSaveSpool("--------------------------------\n\n");
	TextSaveSpool("          WOOSIM  CARD          \n");
	TextSaveSpool("--------------------------------\n");
	TextSaveSpool("CARD NO.     4428-8819-0118-****\n");
	TextSaveSpool("EXPIRY                   2022/10\n");
	TextSaveSpool("--------------------------------\n");
	TextSaveSpool("TRANS. YEAR & DATE    2020/04/15\n");
	TextSaveSpool("TRANS CLASS             [Credit]\n");
	TextSaveSpool("TERMINAL ID.           123456789\n");
	TextSaveSpool("APPROVAL NO.           987654321\n");
	TextSaveSpool("AMOUNT                   500.00$\n");
	TextSaveSpool("--------------------------------\n");
	TextSaveSpool("SIGNATURE: \n\n");
	TextSaveSpool("--------------------------------\n\n");

	PrintSpool();
}


void CProjectDlg::OnBnClickedButtonBarcode1()
{
	InitPrinterStatus();
	TextSaveSpool("UPCA\n");
	OneDimensionBarcodeSaveSpool(UPCA, 2, 60, true, "012100000642"); 
	PrintData();
	TextSaveSpool("UPCE\n");
	OneDimensionBarcodeSaveSpool(UPCE, 2, 60, true, "065100004328"); 
	PrintData();
	TextSaveSpool("EAN13\n");
	OneDimensionBarcodeSaveSpool(EAN13, 2, 60, true, "123456478901"); 
	PrintData();
	TextSaveSpool("EAN8\n");
	OneDimensionBarcodeSaveSpool(EAN8, 2, 60, true, "12345678");
	PrintData();
	TextSaveSpool("CODE39\n");
	OneDimensionBarcodeSaveSpool(CODE39, 2, 60, true, "12345678901"); 
	PrintData();
	TextSaveSpool("ITF\n");
	OneDimensionBarcodeSaveSpool(ITF, 2, 60, true, "123456"); 
	PrintData();
	TextSaveSpool("CODABAR\n");
	OneDimensionBarcodeSaveSpool(CODABAR, 2, 60, true, "123456"); 
	PrintData();
	TextSaveSpool("CODE93\n");
	OneDimensionBarcodeSaveSpool(CODE93, 2, 60, true, "12345678901");
	PrintData();
	TextSaveSpool("CODE128\n");
	OneDimensionBarcodeSaveSpool(CODE128, 2, 60, true, "12345678901"); 
	PrintLineFeed(2);

	PrintSpool();
}


void CProjectDlg::OnBnClickedButtonBarcode2()
{
	InitPrinterStatus();
	TextSaveSpool("PDF417\n");
	PDF417SaveSpool(2, 2, 2, 4, "0123456789", false);
	PrintData();
	TextSaveSpool("DataMatrix\n");
	DataMatrixSaveSpool(36, 36, 2, "0123456789");
	PrintData();
	TextSaveSpool("QR Code\n");
	QRCodeSaveSpool(0, 'H', 3, "0123456789");
	PrintData();
	TextSaveSpool("Micro PDF417\n");
	MicroPDF417SaveSpool(2, 1, 0, 2, "01234567", false);
	PrintData();
	TextSaveSpool("Truncated PDF417\n");
	TruncatedPDF417SaveSpool(2, 2, 2, 2, "0123456789", false);
	PrintData();
	TextSaveSpool("Maxicode\n");
	MaxicodeSaveSpool(4, "0123456789");
	PrintData();

	PrintSpool();
}


void CProjectDlg::OnBnClickedButtonTtf()
{
	SetFontForTTF("WSTTFL.ttf");
	PrintSpoolForTTF("Woosim System Inc.", 30, 30);
	PrintLineFeed(2);
	PrintSpool();
}


void CProjectDlg::OnBnClickedButtonPagemode()
{
	InitPageMode(0, 0, _2INCH, 700);
	SetTextStyle(0, true, 1, 1, false);
	TextSaveSpool("Woosim Cafe\n");

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

	Page_SetPosition(290, 5);
	QRCodeSaveSpool(0, 'H', 2, "www.woosim.com/english");

	Page_DrawBox(0, 130, _2INCH, 0, 2);
	PrintDotFeed(5);
	TextSaveSpool(" #      Item      Num    Charge\n");
	TextSaveSpool(" 1      Latte      1     $ 3.35\n");
	TextSaveSpool(" 2  House Coffee   2     $ 3.98\n");
	TextSaveSpool(" 3       Tea       1     $ 1.95");	
	PrintDotFeed(10);
	Page_DrawBox(0, 270, _2INCH, 0, 2);

	Page_DrawBox(0, 310, 115, 0, 2); 
	Page_DrawBox(260, 310, 124, 0, 2); 
	Page_SetPosition(123, 300);
	TextSaveSpool("Credit Card\n");
	TextSaveSpool("Number:         ");
	SetFontSize(1);
	TextSaveSpool("6688-9988-6644-3311\n");
	SetFontSize(0);
	TextSaveSpool("Payment:                $ 9.50\n");

	Page_DrawBox(0, 400, 120, 0, 2);	
	Page_DrawBox(250, 400, 134, 0, 2); 
	Page_SetPosition(145, 390);
	TextSaveSpool("Payment\n");
	TextSaveSpool("Price:                  $ 9.28\n");
	TextSaveSpool("Tax:                    $ 0.22\n");
	Page_DrawBox(0, 490, _2INCH, 0, 2);	
	PrintDotFeed(15);
	TextSaveSpool("Total:                  $ 9.50\n");
	TextSaveSpool("Credit Card:            $ 9.50\n");

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

	Page_Print_StandardMode();
	PrintSpool();
}


void CProjectDlg::OnBnClickedButtonImage()
{
	if (PathFileExists(_T("./woosim.bmp")) == FALSE)
	{
		AfxMessageBox(_T("woosim.bmp file is not found!"));
		return;
	}

	InitPrinterStatus();

	//Normal BMP file print
	TextSaveSpool("Print Bmp file \n");
	NormalBmpSaveSpool("./woosim.bmp");
	PrintLineFeed(2);

	//BMP file print using Compressed algorithm
	TextSaveSpool("Print Bmp file with compressing\n");
	InitPageMode(0, 0, _2INCH, 200);
	CompressedBmpSaveSpool("./woosim.bmp");
	Page_Print_StandardMode();

	//Print image downloaded in printer
	TextSaveSpool("Print pre-downloaded image file #0\n");
	LoadLogoSaveSpool(0);	
	PrintLineFeed(2);

	PrintSpool();
}


void CProjectDlg::OnBnClickedButtonBoxLine()
{
	//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("Text");
	Page_SetPosition(270, 70);
	QRCodeSaveSpool(0, 'H', 2, "www.woosim.com/english");

	SetTextStyle(0, true, 1, 0, false);
	Page_SetPosition(126, 256); // Y_pos, X_pos
	TextSaveSpool("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);
	PrintData();
	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 - 20, 3);
	Page_Print_StandardMode();
	PrintLineFeed(2);

	PrintSpool();
}


void CProjectDlg::OnBnClickedButtonStatus()
{
	GetPrinterStatus(1000);
}


void CProjectDlg::OnBnClickedButtonInfo()
{
	GetPrinterModelName();
	GetFirmwareVersion();
}


void CProjectDlg::OnBnClickedButtonMsrSet()
{
	EnterMSRMode(m_cboMsr.GetCurSel());
}


void CProjectDlg::OnBnClickedButtonMsrCancel()
{
	CancelMSRMode();
}
