
package TestApp01_call;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

// Display Related
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.global.api.entities.enums.BaudRate;
import com.global.api.entities.enums.ConnectionModes;
import com.global.api.entities.enums.DataBits;
import com.global.api.entities.enums.DeviceType;
import com.global.api.entities.enums.Parity;
import com.global.api.entities.enums.StopBits;
import com.global.api.entities.exceptions.ApiException;
import com.global.api.services.DeviceService;
import com.global.api.terminals.ConnectionConfig;
import com.global.api.terminals.abstractions.IDeviceInterface;
import com.global.api.terminals.abstractions.IDeviceResponse;
import com.global.api.terminals.abstractions.IInitializeResponse;
import com.global.api.terminals.abstractions.ITerminalReport;
import com.global.api.terminals.abstractions.ITerminalResponse;
import com.global.api.terminals.ingenico.pat.PATRequest;
import com.global.api.terminals.ingenico.pat.TransactionOutcome;
import com.global.api.terminals.ingenico.responses.DataResponse;
import com.global.api.terminals.ingenico.responses.IngenicoBaseResponse;
import com.global.api.terminals.ingenico.responses.IngenicoTerminalResponse;
import com.global.api.terminals.ingenico.responses.TerminalStateResponse;
import com.global.api.terminals.ingenico.variables.PATPaymentMode;
import com.global.api.terminals.ingenico.variables.PATRequestType;
import com.global.api.terminals.ingenico.variables.PATResponseType;
import com.global.api.terminals.ingenico.variables.PaymentMode;
import com.global.api.terminals.messaging.IBroadcastMessageInterface;
import com.global.api.terminals.messaging.IMessageSentInterface;
import com.global.api.terminals.messaging.IPayAtTableRequestInterface;
import com.global.api.utils.IOUtils;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.custom.SashForm;

public class testapp01_call extends Thread implements Observer {
	private DataBindingContext m_bindingContext;

	// Display Global Declaration Section
	protected Shell shlTestcom;
	protected IDeviceInterface device;
	protected IDeviceResponse dResponse = null;
	protected ITerminalResponse response = null;
	protected ITerminalReport rResponse = null;
	protected ConnectionConfig _config;
	protected IBroadcastMessageInterface onBroadcastReceived;
	protected IMessageSentInterface onMessageSent;
	protected IPayAtTableRequestInterface onPayAtTableRequest;
	protected static testapp01_call window;

	popUp05 popsy05 = new popUp05(null);
	transactionInProgress inProgress = new transactionInProgress(null);
	Thread t;
	String appendBroadcast = "";

	//Groups
	private Group grpReqMsg;
	private Group grpRespmsg;
	private Group grpAddtnlData;
	private Group grpReport;
	private Group grpTerminalConfig;
	private Composite grpPAT;
	private Composite groupTableList;
	private Composite groupTableLock;
	private Composite groupRawPAT;
	private Composite groupReceipt;
	private Composite groupTransactionOutcome;
	private Composite groupAddDetails;
	private Composite groupTransferData;
	private Composite groupSplitSale;
	private Composite grpPATTransact;
	
	private ArrayList<String> ipList = new ArrayList<String>();
	private ArrayList<String> commandList = new ArrayList<String>();
	private ArrayList<PATRequest> patRequestList= new ArrayList<PATRequest>();
	
	// Com Port
	public Text comPortText;
	public Text baudrateText;
	
	public boolean isIPUnique = false;

	// TCP IP
	private Text ipPortText;
	
	private String lastCommand;
	
	private String fileLocation = "";
	
	private String _rawPATtMessage = "";
	
	private Socket currentSocket;
	
	private Socket socketCLient;

	// Request Messages
	private Text ePosNumText;
	private Text reqsAmntText;
	private Text returnREPText;
	private Text paymentModeText;
	private Combo paymentTypeText;
	private Text currencyText;
	private Text privDataText;
	private Text immediateAnsText;
	private Text forOnlineText;
	private Text extendedValueText;
	public Text rawReqText;
	public String reqAuthCode;
	public String reqTableNumber;
	public String reqCashback;
	public String reqTransCode;
	private Label lblNewLabel_1 ;
	private Label lblNewLabel_2;
	private Label lblNewLabel_2_3_1_1;
	
	private Socket _socket;

	// Response Message
	public Text ePosNumText2;
	public Text transacStatusText;
	public Text respAmntText;
	public Text paymentText;
	public Text respDataText;
	public Text currency2Text;
	public Text privDataText2;
	public Text authCodetext;
	public Text cashBackText;
	public Text gratuityText;
	public Text ftxnAmntTxt;
	public Text availAmntText;
	public Text payMethodText;
	public Text txnSubTypeText;
	public Text sSaleAmntText;
	public Text dccAmnt;
	public Text dccCurrency;
	public Text dccStatus;
	private Button btnBroadCast;
	private Button btnTransactionOutcome;
	private Button btnSplitDone;
	private Button btnSplitAdditionalMessage;
	private Button btnPayTable;
	private Button btnTableListBrowse;
	private Button btnTableListBrowse_1;
	private Label lblDisconnected;
	private Label lblReceiptPath;
	private Label lblPath_2;
	private Label lblAddDetailsPath;
	private Button btntableNum;
	private Button btnReverse;
	private Button btbDuplicate;
	private Button btnSendRequestMsg;
	private Button btnTableLockAccept;
	private Button btnTableLockDecline;
	private Label lblConnectedDevices ;
	private Button btnTransfer;
	private Button btnTransferDone;
	private Button btnTransAdditionalData;
	private Button btnTransactionOutcomeAccept;
	private Button btnTransactionOutcomeDecline; 
	private Button btnBrowseReceipt;
	private Button btnReceipt;

	private Label test;
	private Label lblPath;

	// Response Message
	public Text respMsgText;
	private Text addRespDataText;

	// Definition of Screen Part Process
	private comPort ComCall;
	private tcpIP TcpCall;
	private reqMsg ReqMsgCall;
	private respMsg _RespMsgCall = new respMsg();
	private addtnlData AddtnlDataCall;
	private TransactionOutcome transOut;

	// Display Define
	Display display;
	public boolean sendrepeat = false;
	private boolean isValidXML = false;
	private boolean isClicked = false;
	private Text immediateAns;
	private boolean isConnected = false;
	public boolean isSerial = false;
	private boolean isTcp = false;
	public boolean isCashback = false;
	public boolean isTableNumber = false;
	public boolean isAuthCode = false;
	private Text txtTimeout;
	private boolean isPayAtTable = false;
	private boolean isAdditionalMsg = false;
	private boolean ongoing = false;
	private boolean isDone = false;
	private boolean isAdditionalMessage = false;
	private boolean isDuplicate = false;
	private String receipt;

	private BigDecimal reqAmount;
	private Text txtRawMessages;
	private Text txtRawMessagesPAT;
	private Text txtWaiterIDTableList;
	private Text txtWaiterID;
	private Text txtTableNumber;
	private Text txtTerminalID;
	private Text txtTerminalCurrency;
	private Text txtTransStatus;
	private Text txtTransOutCome;
	private Text txtTransAmount;
	private Text txtTransCurrency;
	private Text txtAmount;
	
	private List listAddress;
	private List listCommand;
	private Text txtTransferAmount;
	private Text txtSplitAmount;
	private Text txtSplitRawData;

	private Integer previousDeviceCount = -1;
	private Text txtTableID;
	private Text txtTerminalid;
	private Text txtTerminalCurr;
	
	private String waiterID;
	private String tableID;
	private String terminalID;
	private String terminalCurrency;
	
	private ConfirmationResponse conf = new ConfirmationResponse(null);
	private Button chkEnableAutoTicket;
	
	public testapp01_call() {

	}
	
	public static void main(String[] args) {
		Display display = Display.getDefault();
				// Local Display Declarations
				try {
					window = new testapp01_call();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
	} // main end

	// Open the window.
	public void open() throws NumberFormatException, ApiException {
		// Initialize values`
		display = Display.getDefault();
		ComCall = new comPort();
		TcpCall = new tcpIP();
		AddtnlDataCall = new addtnlData();

		// Create Display Objects
		createContents();
		hideAllGroups();
		shlTestcom.open();
		shlTestcom.layout();
		while (!shlTestcom.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	
	protected void hideAllGroups() {
		grpReqMsg.setVisible(false);
		grpRespmsg.setVisible(false);
		grpAddtnlData.setVisible(false);
		grpReport.setVisible(false);
		grpTerminalConfig.setVisible(false);
	}
	
	protected void showNonPATGroups() {
		grpReqMsg.setVisible(true);
		grpRespmsg.setVisible(true);
		grpAddtnlData.setVisible(true);
		grpReport.setVisible(true);
		grpTerminalConfig.setVisible(true);
		grpPAT.setVisible(false);
	}
	
	protected void hidePatTransactions() {
//		grpPATTransact.setVisible(false);
	}
	
	protected void showPatTransactions() {
		grpPATTransact.setVisible(true);
	}
	
	protected void showPATGroups() {
		grpPAT.setVisible(true);
		groupTableList.setVisible(false);
		groupTableLock.setVisible(false);
		groupTransactionOutcome.setVisible(false);
		groupAddDetails.setVisible(false);
		groupTransferData.setVisible(false);
		groupSplitSale.setVisible(false);
	}
	
	protected void hideAllPatTransaction() {
		groupTableList.setVisible(false);
		groupTableLock.setVisible(false);
		groupTransactionOutcome.setVisible(false);
		groupReceipt.setVisible(false);
		groupAddDetails.setVisible(false);
		groupTransferData.setVisible(false);
		groupSplitSale.setVisible(false);
	}

	/* Create contents of the window. */
	protected void createContents() {
		// Object Definition (Display)
		shlTestcom = new Shell(display, SWT.CLOSE | SWT.TITLE);
		shlTestcom.setSize(1190, 779);
		Monitor primary = display.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shlTestcom.getBounds();

		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
//	    
		shlTestcom.setLocation(x, y);
		shlTestcom.setMinimumSize(new Point(200, 40));
		shlTestcom.setTouchEnabled(true);
		shlTestcom.setText("Java Simulator App v1.8.2");
		shlTestcom.setLayout(null);

		// Connection Status
		lblDisconnected = new Label(shlTestcom, SWT.NONE);
		lblDisconnected.setFont(SWTResourceManager.getFont("Tahoma", 11, SWT.BOLD));
		lblDisconnected.setBounds(10, 0, 181, 29);
		lblDisconnected.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblDisconnected.setText("DISCONNECTED");
		test = lblDisconnected;

		// Com Port Group
		Group grpComPort = new Group(shlTestcom, SWT.SHADOW_IN);
		grpComPort.setBounds(20, 35, 200, 233);
		grpComPort.setText("COM Port");

		comPortText = new Text(grpComPort, SWT.BORDER);
		comPortText.setBounds(10, 43, 78, 26);
		comPortText.setText("9");

		baudrateText = new Text(grpComPort, SWT.BORDER);
		baudrateText.setBounds(10, 83, 78, 26);
		baudrateText.setText("9600");

		Label lblComPort = new Label(grpComPort, SWT.NONE);
		lblComPort.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.NORMAL));
		lblComPort.setBounds(95, 46, 85, 20);
		lblComPort.setText("COM Port");

		Label lblBaudRate = new Label(grpComPort, SWT.NONE);
		lblBaudRate.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.NORMAL));
		lblBaudRate.setBounds(95, 86, 80, 20);
		lblBaudRate.setText("BaudRate");

		Button btnOpenPort = new Button(grpComPort, SWT.CENTER);
		btnOpenPort.setBounds(10, 120, 165, 40);
		btnOpenPort.addMouseListener(new MouseAdapter() {
			// Open Port Start
			public void mouseDown(MouseEvent e) {
				if (device != null && isTcp)
					JOptionPane.showMessageDialog(null, "SIMULATOR IS CONNECTED VIA TCP/IP");
				else {
					BaudRate baudRate = null;
					BaudRate localBaud = null;
					String baudRatemp = baudrateText.getText();
					if (baudRatemp == "")
						baudRatemp = "0";

					for (BaudRate d : baudRate.values()) {
						if (d.getValue() == Integer.valueOf(baudRatemp)) {
							localBaud = d;
							break;
						}
					}

					if (localBaud == null)
						JOptionPane.showMessageDialog(null, "INVALID BAUDRATE");
					else {
						String timeout = (txtTimeout.getText().isEmpty() ? "65000" : txtTimeout.getText());
						_config = new ConnectionConfig();
						_config.setDeviceType(DeviceType.INGENICO_L3000);
						_config.setConnectionMode(ConnectionModes.SERIAL);
						_config.setPort(comPortText.getText());
						_config.setBaudRate(localBaud);

						_config.setDataBits(DataBits.Seven);
						_config.setParity(Parity.Even);
						_config.setStopBits(StopBits.One);
						_config.setTimeout(Integer.parseInt(timeout));
						try {
							device = DeviceService.create(_config);

							if (device == null) {
								lblDisconnected.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
								lblDisconnected.setText("DISCONNECTED");
							} else {
								lblDisconnected.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
								lblDisconnected.setText("CONNECTED");
								isConnected = true;
								isSerial = true;
								JOptionPane.showMessageDialog(null, "COM PORT OPEN");
								ReqMsgCall = new reqMsg(device, null);
								
								Display.getDefault().asyncExec(new Runnable() {
									@Override
									public void run() {
										showNonPATGroups();
									}
								});

								Display.getDefault().asyncExec(new Runnable() {
									@Override
									public void run() {
										device.setOnMessageSent(new IMessageSentInterface() {
											public void messageSent(String message) {
												Display.getDefault().asyncExec(new Runnable() {
													@Override
													public void run() {														
														rawReqText.setText(message);
														_rawPATtMessage = "";
														_rawPATtMessage += message;
														txtRawMessagesPAT.append(_rawPATtMessage);
														txtRawMessagesPAT.append("\n");	
													}
												});
											}
										});
									}
								});
							}
						} catch (ApiException e1) {
							JOptionPane.showMessageDialog(null, e1.getMessage());
						}
//						sendrepeat = false;
					}
				}
			}
		});
		btnOpenPort.setText("Open Port");

		Button btnClosePort = new Button(grpComPort, SWT.NONE);
		btnClosePort.setBounds(10, 170, 165, 40);
		btnClosePort.addMouseListener(new MouseAdapter() {
			// Close Port Start
			public void mouseDown(MouseEvent e) {
				if (device != null) {
					if (isTcp)
						JOptionPane.showMessageDialog(null, "SIMULATOR IS CONNECTED VIA TCP/IP");
					else {
						device.dispose();
						device = null;

						if (device == null) {
							lblDisconnected.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
							lblDisconnected.setText("DISCONNECTED");
							isConnected = false;
							isSerial = false;
							btnBroadCast.setSelection(false);
							btnPayTable.setSelection(false);
							JOptionPane.showMessageDialog(null, "COM PORT CLOSE");
							
							hideAllGroups();				
							
							clear();
						} else {
							lblDisconnected.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
							lblDisconnected.setText("CONNECTED");
							isConnected = true;
							isSerial = true;
							JOptionPane.showMessageDialog(null, "COM PORT OPEN");
						}
					}
				} else
					JOptionPane.showMessageDialog(null, "COM PORT CLOSE");
			}
		});
		btnClosePort.setText("Close Port");

		// TCP / IP Group
		Group grpTCPIP = new Group(shlTestcom, SWT.NONE);
		grpTCPIP.setBounds(20, 270, 200, 276);
		grpTCPIP.setText("TCP/IP");

		ipPortText = new Text(grpTCPIP, SWT.BORDER);
		ipPortText.setTextLimit(5);
		ipPortText.setText("18101");
		ipPortText.setBounds(10, 40, 78, 26);
		ipPortText.addVerifyListener(new VerifyListener() {

			@Override
			public void verifyText(VerifyEvent e) {
				String string = e.text;
				char[] chars = new char[string.length()];
				string.getChars(0, chars.length, chars, 0);
				for (int i = 0; i < chars.length; i++) {
					if (!('0' <= chars[i] && chars[i] <= '9')) {
						e.doit = false;
						return;
					}
				}
			}
		});

		Label lblIpPort = new Label(grpTCPIP, SWT.NONE);
		lblIpPort.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.NORMAL));
		lblIpPort.setText("IP Port");
		lblIpPort.setBounds(95, 43, 80, 20);

		Button btnStartLstn = new Button(grpTCPIP, SWT.CENTER);
		btnStartLstn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		btnStartLstn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				initializeSocket();
			}
		});
		btnStartLstn.setText("Start Listening");
		btnStartLstn.setBounds(10, 80, 165, 40);

		Button btnStopLstn = new Button(grpTCPIP, SWT.NONE);
		btnStopLstn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		btnStopLstn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				Display.getDefault().asyncExec(new Runnable() {
					@Override
					public void run() {
						if (device != null) {
							if (isSerial)
								JOptionPane.showMessageDialog(null, "SIMULATOR IS CONNECTED VIA SERIAL");
							else {
								if (device != null) {
									device = TcpCall.stopLstn(device);

								}
								
								
								if (device == null) {
									isConnected = false;
									isTcp = false;
									isPayAtTable = false;
									ongoing = false;
									previousDeviceCount = -1;
									
									Display.getDefault().asyncExec(new Runnable() {
										@Override
										public void run() {
											grpPAT.setVisible(false);
											hideAllPatTransaction();
											hidePatTransactions();
											hideAllGroups();
											lblDisconnected.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
											lblDisconnected.setText("DISCONNECTED");
											btnBroadCast.setSelection(false);
											btnPayTable.setSelection(false);
											listAddress.removeAll();
											listCommand.removeAll();	
											patRequestList.clear();
											txtRawMessagesPAT.setText("");
											clear();
										}
									});
									
									new Thread(new Runnable() {
										@Override
										public void run() {
											if (!patRequestList.isEmpty()) {
												for (int k = 0 ; k < patRequestList.size() ; k++) {
													try {
														patRequestList.get(k).getSocket().close();
													} catch (IOException e) {
														// TODO Auto-generated catch block
														e.printStackTrace();
													}
												}
											}
										
										}
									}).start();
									
									
								}
								
							}
						} else {
//							JOptionPane.showMessageDialog(null, "IP LISTEN CLOSE");
						}
					}
				});
			}
		});
		btnStopLstn.setText("Stop Listening");
		btnStopLstn.setBounds(10, 130, 165, 40);

		Label lblBrodcasttik = new Label(grpTCPIP, SWT.NONE);
		lblBrodcasttik.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.NORMAL));
		lblBrodcasttik.setText("Display Broadcast");
		lblBrodcasttik.setBounds(30, 180, 150, 20);

		Label lblMessage = new Label(grpTCPIP, SWT.NONE);
		lblMessage.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.NORMAL));
		lblMessage.setText("Message");
		lblMessage.setBounds(30, 200, 80, 30);

		btnBroadCast = new Button(grpTCPIP, SWT.CHECK);
		btnBroadCast.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (device == null) {
					JOptionPane.showMessageDialog(null, "PLEASE CONNECT FIRST TO THE TERMINAL");
					btnBroadCast.setSelection(false);
					return;
				}

				if (btnBroadCast.getSelection())
					device.setOnBroadcastMessageReceived(new IBroadcastMessageInterface() {
						public void broadcastReceived(String code, String message) {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {	
									appendBroadcast = appendBroadcast + String.format("%s  |  %s \n", code, message);
									addRespDataText.setText(appendBroadcast);
								
								}
							});
						}
					});
				else
					device.setOnBroadcastMessageReceived(new IBroadcastMessageInterface() {
						public void broadcastReceived(String code, String message) {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									addRespDataText.setText("");
								}
							});
						}
					});
			}
		});
		btnBroadCast.setBounds(10, 182, 16, 20);

		Label lblPayTableTik = new Label(grpTCPIP, SWT.NONE);
		lblPayTableTik.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.NORMAL));
		lblPayTableTik.setText("Pay@Table Mode");
		lblPayTableTik.setBounds(30, 236, 150, 30);

		btnPayTable = new Button(grpTCPIP, SWT.CHECK);
		btnPayTable.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (btnPayTable.getSelection()) {
					isPayAtTable = true;
					hideAllGroups();
				} else {
					isPayAtTable = false;
					hideAllGroups();
				}
			}
		});
		btnPayTable.setBounds(10, 233, 16, 20);

		// Request Message Group
		grpReqMsg = new Group(shlTestcom, SWT.NONE);
		grpReqMsg.setBounds(226, 10, 345, 703);
		grpReqMsg.setText("Request Message");
		grpReqMsg.setVisible(false);

		Label lblEposNum = new Label(grpReqMsg, SWT.NONE);
		lblEposNum.setText("EPoS Number");
		lblEposNum.setBounds(12, 25, 120, 20);

		ePosNumText = new Text(grpReqMsg, SWT.BORDER);
		ePosNumText.setText("01");
		ePosNumText.setBounds(152, 22, 180, 26);
		ePosNumText.setTextLimit(2);
		ePosNumText.addVerifyListener(new VerifyListener() {

			@Override
			public void verifyText(VerifyEvent e) {
				String string = e.text;
				char[] chars = new char[string.length()];
				string.getChars(0, chars.length, chars, 0);
				for (int i = 0; i < chars.length; i++) {
					if (!('0' <= chars[i] && chars[i] <= '9')) {
						e.doit = false;
						return;
					}
				}
			}
		});

		Label lblAmnt = new Label(grpReqMsg, SWT.NONE);
		lblAmnt.setText("Amount");
		lblAmnt.setBounds(12, 62, 120, 20);

		reqsAmntText = new Text(grpReqMsg, SWT.BORDER);
		reqsAmntText.addVerifyListener(new VerifyListener() {

			@Override
			public void verifyText(VerifyEvent e) {
				String string = e.text;
				char[] chars = new char[string.length()];
				string.getChars(0, chars.length, chars, 0);
				for (int i = 0; i < chars.length; i++) {
					if (!('0' <= chars[i] && chars[i] <= '9' || chars[i] == '.')) {
						e.doit = false;
						return;
					}
				}
			}
		});
		reqsAmntText.setText("6.18");
		reqsAmntText.setBounds(152, 59, 180, 26);
		reqsAmntText.setTextLimit(8);

		Label lblReturnRep = new Label(grpReqMsg, SWT.NONE);
		lblReturnRep.setText("Return REP");
		lblReturnRep.setBounds(12, 99, 120, 20);

		returnREPText = new Text(grpReqMsg, SWT.BORDER);
		returnREPText.setEnabled(false);
		returnREPText.setText("1");
		returnREPText.setBounds(152, 96, 180, 26);
		returnREPText.setTextLimit(1);

		Label lblPaymentMode = new Label(grpReqMsg, SWT.NONE);
		lblPaymentMode.setText("Payment Mode");
		lblPaymentMode.setBounds(12, 136, 120, 30);

		paymentModeText = new Text(grpReqMsg, SWT.BORDER);
		paymentModeText.setText("0");
		paymentModeText.setBounds(152, 133, 180, 26);
		paymentModeText.setTextLimit(1);
		paymentModeText.addVerifyListener(new VerifyListener() {

			@Override
			public void verifyText(VerifyEvent e) {
				String string = e.text;
				char[] chars = new char[string.length()];
				string.getChars(0, chars.length, chars, 0);
				for (int i = 0; i < chars.length; i++) {
					if (!('0' <= chars[i] && chars[i] <= '9')) {
						e.doit = false;
						return;
					}
				}
			}
		});

		Button btnCashback = new Button(grpReqMsg, SWT.NONE);
		btnCashback.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				popUp02 popsy02 = new popUp02(null);
				if (reqCashback != null) {
					popsy02.setCashback(reqCashback);
				}

				popsy02.open();
				popsy02.addDisposeListener(new DisposeListener() {
					public void widgetDisposed(DisposeEvent arg0) {
						reqCashback = popsy02.getCashBack();
					}
				});
			}
		});
		btnCashback.setText("Cashback");
		btnCashback.setBounds(127, 577, 94, 40);
		btnCashback.setVisible(false);

		Button btnAuthCode = new Button(grpReqMsg, SWT.CENTER);
		btnAuthCode.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.NORMAL));
		btnAuthCode.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				popUp03 popsy03 = new popUp03(null);
				if (reqAuthCode != null) {
					popsy03.setAuthCode(reqAuthCode);
				}

				popsy03.open();
				popsy03.addDisposeListener(new DisposeListener() {
					public void widgetDisposed(DisposeEvent arg0) {
						reqAuthCode = popsy03.getAuthCode();
					}
				});
			}
		});
		btnAuthCode.setText("AuthCode");
		btnAuthCode.setBounds(14, 577, 92, 40);
		btnAuthCode.setVisible(false);

		Label lblpaymentType = new Label(grpReqMsg, SWT.NONE);
		lblpaymentType.setText("Payment Type");
		lblpaymentType.setBounds(12, 173, 120, 20);

		paymentTypeText = new Combo(grpReqMsg, SWT.READ_ONLY);
		paymentTypeText.setItems(new String[] {"SALE", "REFUND", "HOTEL MODE PRE AUTH", "HOTEL MODE COMPLETION", "TAXFREE CREDIT REFUND", "TAXFREE CASH REFUND", "ACCCOUNT VERIFICATION", "REFERRAL CONFIRMATION"});
		paymentTypeText.setBounds(152, 170, 180, 28);
		paymentTypeText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				switch (paymentTypeText.getText()) {
				case "SALE":
					btnCashback.setVisible(true);
					btnAuthCode.setVisible(false);
					break;
				case "REFUND":
					btnCashback.setVisible(false);
					btnAuthCode.setVisible(false);
					break;
				case "HOTEL MODE COMPLETION":
					btnCashback.setVisible(false);
					btnAuthCode.setVisible(true);
					showAuthCodeForm();
					break;
				case "HOTEL MODE PRE AUTH":
					btnCashback.setVisible(false);
					btnAuthCode.setVisible(false);
					break;
				case "TAXFREE CREDIT REFUND":
					btnCashback.setVisible(false);
					btnAuthCode.setVisible(false);
					break;
				case "TAXFREE CASH REFUND":
					btnCashback.setVisible(false);
					btnAuthCode.setVisible(false);
					break;
				case "ACCOUNT VERIFICATION":
					btnCashback.setVisible(false);
					btnAuthCode.setVisible(false);
					break;
				case "REFERRAL CONFIRMATION":
					btnCashback.setVisible(false);
					btnAuthCode.setVisible(true);
					break;
				default:
					btnCashback.setVisible(false);
					btnAuthCode.setVisible(false);
					break;
				}
			}
		});

		Label lblCurrency = new Label(grpReqMsg, SWT.NONE);
		lblCurrency.setText("Currency");
		lblCurrency.setBounds(12, 210, 120, 20);
//		currencyText.setEnabled(false);

		currencyText = new Text(grpReqMsg, SWT.BORDER);
		currencyText.setText("826");
		currencyText.setBounds(152, 211, 180, 26);
		currencyText.setTextLimit(3);
		currencyText.setEnabled(false);

		Label lblPrivData = new Label(grpReqMsg, SWT.NONE);
		lblPrivData.setText("Private Data");
		lblPrivData.setBounds(12, 247, 120, 20);

		privDataText = new Text(grpReqMsg, SWT.BORDER);
		privDataText.setText("EXT01");
		privDataText.setBounds(150, 244, 180, 26);
		privDataText.setEnabled(false);

		Label lblImmediateAns = new Label(grpReqMsg, SWT.NONE);
		lblImmediateAns.setFont(SWTResourceManager.getFont("Segoe UI", 8, SWT.NORMAL));
		lblImmediateAns.setText("Immediate Answer");
		lblImmediateAns.setBounds(10, 284, 130, 25);

		Label lblForOnline = new Label(grpReqMsg, SWT.NONE);
		lblForOnline.setText("Force Online");
		lblForOnline.setBounds(12, 315, 120, 31);

		forOnlineText = new Text(grpReqMsg, SWT.BORDER);
		forOnlineText.setText("0");
		forOnlineText.setBounds(150, 318, 180, 26);
		forOnlineText.setEnabled(false);

		Label lblExtendValue = new Label(grpReqMsg, SWT.NONE);
		lblExtendValue.setText("Extended Value");
		lblExtendValue.setBounds(10, 358, 120, 20);
//		extendedValueText.setEnabled(false);

		extendedValueText = new Text(grpReqMsg, SWT.BORDER);
		extendedValueText.setText("0000000000");
		extendedValueText.setBounds(150, 355, 180, 26);
		extendedValueText.setEnabled(false);

		btnSendRequestMsg = new Button(grpReqMsg, SWT.CENTER);
		btnSendRequestMsg.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {				
				
				try {
					if (isConnected) {
						BigDecimal amount = new BigDecimal(
								!reqsAmntText.getText().matches("^[0-9]\\d*(\\.\\d+)?$") ? "0"
										: reqsAmntText.getText());
						if (ePosNumText.getText().isEmpty() || reqsAmntText.getText().isEmpty()
								|| paymentModeText.getText().isEmpty() || paymentTypeText.getText() == "")
							JOptionPane.showMessageDialog(null, "PLEASE FILL THE FOLLOWING ENABLED TEXT BOXES",
									"SDK Information", JOptionPane.ERROR_MESSAGE);
						else {
							if (amount.compareTo(BigDecimal.ZERO) <= 0)
								JOptionPane.showMessageDialog(null, "TRANSACTION AMOUNT IS INVALID");
							else {
								dResponse = null;
								response = null;
								rResponse = null;

								if (reqTableNumber != "" && reqTableNumber != null) {
									ReqMsgCall.setTableNumber(reqTableNumber);
									isTableNumber = true;
									isCashback = false;
									isAuthCode = false;
								} else if (reqCashback != "" && reqCashback != null) {
									ReqMsgCall.setCashbackAmount(reqCashback);
									isCashback = true;
									isTableNumber = false;
									isAuthCode = false;
								} else if (reqAuthCode != "" && reqAuthCode != null) {
									ReqMsgCall.setAuthCode(reqAuthCode);
									isAuthCode = true;
									isTableNumber = false;
									isCashback = false;
								}

								new Thread(new Runnable() {
									@Override
									public void run() {
										Display.getDefault().asyncExec(new Runnable() {
											@Override
											public void run() {
												displayInProgress();
											}
										});
									}
								}).start();

								clear();
								ReqMsgCall.set_ePosNumText(ePosNumText.getText());
								ReqMsgCall.set_reqsAmntText(reqsAmntText.getText());
								ReqMsgCall.set_returnREPText(returnREPText.getText());
								ReqMsgCall.set_paymentModeText(paymentModeText.getText());
								ReqMsgCall.set_paymentTypeText(paymentTypeText.getSelectionIndex());
								ReqMsgCall.set_currencyText(currencyText.getText());
								ReqMsgCall.set_privDataText(privDataText.getText());
								ReqMsgCall.set_immediateAnsText(immediateAns.getText());
								ReqMsgCall.set_forOnlineText(forOnlineText.getText());
								ReqMsgCall.set_extendedValueText(extendedValueText.getText());
								ReqMsgCall.set_AutoTicketEnabled(chkEnableAutoTicket.getSelection());
								
								new Thread(new Runnable() {
									@Override
									public void run() {		
										response = ReqMsgCall.sendReqMsg(response, device, isCashback, isTableNumber);
										
										Display.getDefault().asyncExec(new Runnable() {
											@Override
											public void run() {
												
												inProgress.close();
												if (response != null)
													displayResponse(response, device);

											}
										});
									}

								}).start();
							}
						}
					} else {
						JOptionPane.showMessageDialog(null, "PLEASE OPEN CONNECTION");
					}					
				} catch (Exception ex) {
					JOptionPane.showMessageDialog(null, ex.getMessage());
				}
			}
		});
		btnSendRequestMsg.setFont(SWTResourceManager.getFont("Tahoma", 7, SWT.NORMAL));
		btnSendRequestMsg.setText("Send Request Message");
		btnSendRequestMsg.setBounds(162, 392, 155, 40);

		Label lblRawRequest = new Label(grpReqMsg, SWT.NONE);
		lblRawRequest.setText("Raw Request");
		lblRawRequest.setBounds(12, 456, 120, 20);

		rawReqText = new Text(grpReqMsg, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		rawReqText.setBounds(150, 435, 180, 70);

		btntableNum = new Button(grpReqMsg, SWT.NONE);
		btntableNum.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent e) {
				popUp01 popsy01 = new popUp01(null);
				if (reqTableNumber != null) {
					popsy01.setTableNumber(reqTableNumber);
				}

				popsy01.open();
				popsy01.addDisposeListener(new DisposeListener() {
					public void widgetDisposed(DisposeEvent arg0) {
						reqTableNumber = popsy01.getTableNumber();
					}
				});
			}
		});
		btntableNum.setText("Table #");
		btntableNum.setBounds(12, 511, 94, 40);

		btnReverse = new Button(grpReqMsg, SWT.NONE);
		btnReverse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!isConnected)
					JOptionPane.showMessageDialog(null, "PORT NOT OPEN");
				else {
					clear();
					int input = JOptionPane.showOptionDialog(null, "WOULD YOU LIKE TO REVERSE A SPECIFIC TRANSACTION?",
							"SDK Information", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null,
							null);

					if (input == -1)
						return;

					if (ePosNumText.getText() == "" || reqsAmntText.getText() == "" || paymentModeText.getText() == "")
						JOptionPane.showMessageDialog(null, "PLEASE FILL THE FOLLOWING ENABLED TEXT BOXES");
					else {
						new Thread(new Runnable() {
							@Override
							public void run() {
								Display.getDefault().asyncExec(new Runnable() {
									@Override
									public void run() {
										displayInProgress();
									}
								});
							}
						}).start();

						if (input == JOptionPane.YES_OPTION) {
							popUp04 popsy04 = new popUp04(null);
							popsy04.setupDevice(dResponse, device, window);
							popsy04.setAmount(reqsAmntText.getText());
							popsy04.setReferenceNumber(ePosNumText.getText());
							popsy04.open();
							popsy04.addDisposeListener(new DisposeListener() {
								public void widgetDisposed(DisposeEvent arg0) {
									inProgress.close();
									reqTransCode = popsy04.getTransCode();
								}
							});
						} else if (input == JOptionPane.NO_OPTION) {

							ReqMsgCall.set_ePosNumText(ePosNumText.getText());
							ReqMsgCall.set_reqsAmntText(reqsAmntText.getText());
							ReqMsgCall.set_AutoTicketEnabled(chkEnableAutoTicket.getSelection());
							new Thread(new Runnable() {
								@Override
								public void run() {

									dResponse = ReqMsgCall.reverse(input, dResponse, "");

									Display.getDefault().asyncExec(new Runnable() {
										@Override
										public void run() {
											inProgress.close();
											if (dResponse != null) {
												displayResponse(((ITerminalResponse) dResponse), device);
											}
										}
									});
								}
							}).start();
						}
					}
				}
			}
		});
		btnReverse.setText("Reverse");
		btnReverse.setBounds(127, 511, 94, 40);

		btbDuplicate = new Button(grpReqMsg, SWT.NONE);
		btbDuplicate.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!isConnected)
					JOptionPane.showMessageDialog(null, "PORT NOT OPEN");
				else {
					clear();

					if (ePosNumText.getText().isEmpty() || reqsAmntText.getText().isEmpty()
							|| paymentTypeText.getText().isEmpty()) {
						JOptionPane.showMessageDialog(null, "PLEASE FILL THE FOLLOWING ENABLED TEXT BOXES");
					} else {
						ReqMsgCall.set_ePosNumText(ePosNumText.getText());
						ReqMsgCall.set_reqsAmntText(reqsAmntText.getText());

						new Thread(new Runnable() {
							@Override
							public void run() {
								Display.getDefault().asyncExec(new Runnable() {
									@Override
									public void run() {
										displayInProgress();
									}
								});
							}
						}).start();

						ReqMsgCall.set_AutoTicketEnabled(chkEnableAutoTicket.getSelection());
						
						new Thread(new Runnable() {
							@Override
							public void run() {
								dResponse = ReqMsgCall.duplicate(response);

								Display.getDefault().asyncExec(new Runnable() {
									@Override
									public void run() {
										inProgress.close();
										if (dResponse != null) {
											displayResponse((ITerminalResponse) dResponse, device);
										}
									}
								});
							}
						}).start();
					}
				}
			}
		});
		btbDuplicate.setText("Duplicate");
		btbDuplicate.setBounds(240, 511, 94, 40);

		Button btnClear = new Button(grpReqMsg, SWT.NONE);
		btnClear.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent e) {
				clear();
			}
		});
		btnClear.setText("Clear");
		btnClear.setBounds(240, 577, 94, 40);

		immediateAns = new Text(grpReqMsg, SWT.BORDER);
		immediateAns.setEditable(false);
		immediateAns.setEnabled(false);
		immediateAns.setText("0");
		immediateAns.setBounds(150, 276, 180, 26);
		//region Response Message Group
		grpRespmsg = new Group(shlTestcom, SWT.NONE);
		grpRespmsg.setBounds(577, 10, 330, 721);
		grpRespmsg.setText("Response Message");
		grpRespmsg.setVisible(false);

		Label lblEposNum2 = new Label(grpRespmsg, SWT.NONE);
		lblEposNum2.setText("EPoS Number");
		lblEposNum2.setBounds(10, 23, 120, 20);

		ePosNumText2 = new Text(grpRespmsg, SWT.BORDER);
		ePosNumText2.setBounds(130, 20, 180, 26);

		Label lblTransacStatus = new Label(grpRespmsg, SWT.NONE);
		lblTransacStatus.setFont(SWTResourceManager.getFont("Tahoma", 7, SWT.NORMAL));
		lblTransacStatus.setText("Transaction Status");
		lblTransacStatus.setBounds(10, 55, 120, 20);

		transacStatusText = new Text(grpRespmsg, SWT.BORDER);
		transacStatusText.setBounds(130, 50, 180, 26);

		Label lblAmount2 = new Label(grpRespmsg, SWT.NONE);
		lblAmount2.setText("Amount");
		lblAmount2.setBounds(10, 81, 120, 20);

		respAmntText = new Text(grpRespmsg, SWT.BORDER);
		respAmntText.setBounds(130, 80, 180, 26);

		Label lblPayment = new Label(grpRespmsg, SWT.NONE);
		lblPayment.setText("Payment");
		lblPayment.setBounds(10, 111, 120, 20);

		paymentText = new Text(grpRespmsg, SWT.BORDER);
		paymentText.setBounds(130, 110, 180, 26);

		Label lblRespData = new Label(grpRespmsg, SWT.NONE);
		lblRespData.setText("Response Data");
		lblRespData.setBounds(10, 143, 120, 20);

		respDataText = new Text(grpRespmsg, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		respDataText.setBounds(130, 140, 180, 49);

		Label lblCurrency2 = new Label(grpRespmsg, SWT.NONE);
		lblCurrency2.setText("Currency");
		lblCurrency2.setBounds(10, 198, 120, 20);

		currency2Text = new Text(grpRespmsg, SWT.BORDER);
		currency2Text.setBounds(130, 193, 180, 26);

		Label lblprivData2 = new Label(grpRespmsg, SWT.NONE);
		lblprivData2.setText("Private Data");
		lblprivData2.setBounds(10, 224, 120, 20);

		privDataText2 = new Text(grpRespmsg, SWT.BORDER);
		privDataText2.setBounds(130, 223, 180, 26);

		Label lblRepFld = new Label(grpRespmsg, SWT.NONE);
		lblRepFld.setText("REP Fields:");
		lblRepFld.setBounds(10, 368, 120, 20);

		Label lblAuthCode = new Label(grpRespmsg, SWT.NONE);
		lblAuthCode.setFont(SWTResourceManager.getFont("Tahoma", 7, SWT.NORMAL));
		lblAuthCode.setText("Authorization Code");
		lblAuthCode.setBounds(10, 391, 120, 20);

		authCodetext = new Text(grpRespmsg, SWT.BORDER);
		authCodetext.setFont(SWTResourceManager.getFont("Tahoma", 9, SWT.NORMAL));
		authCodetext.setBounds(130, 385, 180, 26);

		Label lblCashbackAmount = new Label(grpRespmsg, SWT.NONE);
		lblCashbackAmount.setText("Cashback Amount");
		lblCashbackAmount.setFont(SWTResourceManager.getFont("Tahoma", 7, SWT.NORMAL));
		lblCashbackAmount.setBounds(10, 421, 120, 26);

		cashBackText = new Text(grpRespmsg, SWT.BORDER);
		cashBackText.setFont(SWTResourceManager.getFont("Tahoma", 9, SWT.NORMAL));
		cashBackText.setBounds(130, 415, 180, 26);

		Label lblGratuityAmount = new Label(grpRespmsg, SWT.NONE);
		lblGratuityAmount.setText("Gratuity Amount");
		lblGratuityAmount.setFont(SWTResourceManager.getFont("Tahoma", 7, SWT.NORMAL));
		lblGratuityAmount.setBounds(10, 451, 120, 20);

		gratuityText = new Text(grpRespmsg, SWT.BORDER);
		gratuityText.setFont(SWTResourceManager.getFont("Tahoma", 9, SWT.NORMAL));
		gratuityText.setBounds(130, 445, 180, 26);

		Label lblFinalTxnAmounrt = new Label(grpRespmsg, SWT.NONE);
		lblFinalTxnAmounrt.setText("Final Txn Amounrt");
		lblFinalTxnAmounrt.setFont(SWTResourceManager.getFont("Tahoma", 7, SWT.NORMAL));
		lblFinalTxnAmounrt.setBounds(10, 481, 120, 20);

		ftxnAmntTxt = new Text(grpRespmsg, SWT.BORDER);
		ftxnAmntTxt.setFont(SWTResourceManager.getFont("Tahoma", 9, SWT.NORMAL));
		ftxnAmntTxt.setBounds(130, 475, 180, 26);

		Label lblAvailableAmount = new Label(grpRespmsg, SWT.NONE);
		lblAvailableAmount.setText("Available Amount");
		lblAvailableAmount.setFont(SWTResourceManager.getFont("Tahoma", 7, SWT.NORMAL));
		lblAvailableAmount.setBounds(10, 511, 120, 20);

		availAmntText = new Text(grpRespmsg, SWT.BORDER);
		availAmntText.setFont(SWTResourceManager.getFont("Tahoma", 9, SWT.NORMAL));
		availAmntText.setBounds(130, 505, 180, 26);

		Label lblPaymentMethod = new Label(grpRespmsg, SWT.NONE);
		lblPaymentMethod.setText("Payment Method");
		lblPaymentMethod.setFont(SWTResourceManager.getFont("Tahoma", 7, SWT.NORMAL));
		lblPaymentMethod.setBounds(10, 535, 120, 20);

		payMethodText = new Text(grpRespmsg, SWT.BORDER);
		payMethodText.setFont(SWTResourceManager.getFont("Tahoma", 9, SWT.NORMAL));
		payMethodText.setBounds(130, 529, 180, 26);

		Label lblTxnSubtype = new Label(grpRespmsg, SWT.NONE);
		lblTxnSubtype.setText("Txn Sub-Type");
		lblTxnSubtype.setFont(SWTResourceManager.getFont("Tahoma", 7, SWT.NORMAL));
		lblTxnSubtype.setBounds(10, 565, 120, 20);

		txnSubTypeText = new Text(grpRespmsg, SWT.BORDER);
		txnSubTypeText.setFont(SWTResourceManager.getFont("Tahoma", 9, SWT.NORMAL));
		txnSubTypeText.setBounds(130, 560, 180, 26);

		Label lblSplitSaleAmount = new Label(grpRespmsg, SWT.NONE);
		lblSplitSaleAmount.setText("Split Sale Amount");
		lblSplitSaleAmount.setFont(SWTResourceManager.getFont("Tahoma", 7, SWT.NORMAL));
		lblSplitSaleAmount.setBounds(10, 595, 120, 20);

		sSaleAmntText = new Text(grpRespmsg, SWT.BORDER);
		sSaleAmntText.setFont(SWTResourceManager.getFont("Tahoma", 9, SWT.NORMAL));
		sSaleAmntText.setBounds(130, 590, 180, 26);

		Label lblDccAmount = new Label(grpRespmsg, SWT.NONE);
		lblDccAmount.setText("DCC Amount");
		lblDccAmount.setFont(SWTResourceManager.getFont("Tahoma", 7, SWT.NORMAL));
		lblDccAmount.setBounds(10, 625, 120, 20);

		dccAmnt = new Text(grpRespmsg, SWT.BORDER);
		dccAmnt.setFont(SWTResourceManager.getFont("Tahoma", 9, SWT.NORMAL));
		dccAmnt.setBounds(130, 620, 180, 26);

		Label lblDccCurrency = new Label(grpRespmsg, SWT.NONE);
		lblDccCurrency.setText("DCC Currency");
		lblDccCurrency.setFont(SWTResourceManager.getFont("Tahoma", 7, SWT.NORMAL));
		lblDccCurrency.setBounds(10, 658, 120, 20);

		dccCurrency = new Text(grpRespmsg, SWT.BORDER);
		dccCurrency.setFont(SWTResourceManager.getFont("Tahoma", 9, SWT.NORMAL));
		dccCurrency.setBounds(130, 652, 180, 26);

		Label lblDccStatus = new Label(grpRespmsg, SWT.NONE);
		lblDccStatus.setText("DCC Status");
		lblDccStatus.setFont(SWTResourceManager.getFont("Tahoma", 7, SWT.NORMAL));
		lblDccStatus.setBounds(10, 684, 120, 20);

		dccStatus = new Text(grpRespmsg, SWT.BORDER);
		dccStatus.setFont(SWTResourceManager.getFont("Tahoma", 9, SWT.NORMAL));
		dccStatus.setBounds(130, 684, 180, 26);

		respMsgText = new Text(grpRespmsg, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		respMsgText.setBounds(130, 253, 180, 70);

		Label lblRawResponse = new Label(grpRespmsg, SWT.NONE);
		lblRawResponse.setText("Raw Response");
		lblRawResponse.setBounds(10, 256, 120, 20);
		
		btnReceipt = new Button(grpRespmsg, SWT.NONE);
		btnReceipt.setEnabled(false);
		btnReceipt.setBounds(160, 329, 122, 25);
		btnReceipt.setText("Receipt");
		
		chkEnableAutoTicket = new Button(grpReqMsg, SWT.CHECK);
		chkEnableAutoTicket.setBounds(10, 638, 180, 25);
		chkEnableAutoTicket.setText("Enable Auto Ticket");
		//endregion
		// Additional Data Group
		grpAddtnlData = new Group(shlTestcom, SWT.NONE);
		grpAddtnlData.setBounds(913, 10, 256, 306);
		grpAddtnlData.setText("Additional response data");

		addRespDataText = new Text(grpAddtnlData, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		addRespDataText.setBounds(10, 27, 231, 263);

		// Additional Data Update
		addRespDataText.setText(AddtnlDataCall.get_respMsgText(device, onBroadcastReceived));
		addRespDataText.setText(AddtnlDataCall.get_respDataText());

		grpReport = new Group(shlTestcom, SWT.NONE);
		grpReport.setBounds(913, 322, 256, 231);
		grpReport.setText("Report");

		Button btnEOD = new Button(grpReport, SWT.NONE);
		btnEOD.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!isConnected)
					JOptionPane.showMessageDialog(null, "PORT NOT OPEN");
				else {
					clear();
					new Thread(new Runnable() {
						@Override
						public void run() {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									displayInProgress();
								}
							});
						}
					}).start();

					new Thread(new Runnable() {
						@Override
						public void run() {
							rResponse = ReqMsgCall.reportEOD(rResponse);

							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									inProgress.close();

									if (rResponse != null) {
										displayResponse(((ITerminalResponse) rResponse), device);
									}
								}
							});
						}
					}).start();
				}
			}
		});
		btnEOD.setText("End of Day");
		btnEOD.setBounds(10, 21, 94, 40);

		Button btmReport = new Button(grpReport, SWT.NONE);
		btmReport.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!isConnected)
					JOptionPane.showMessageDialog(null, "PORT NOT OPEN");
				else {
					clear();
					new Thread(new Runnable() {
						@Override
						public void run() {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									displayInProgress();
								}
							});
						}
					}).start();

					new Thread(new Runnable() {
						@Override
						public void run() {
							rResponse = ReqMsgCall.xmlReport(rResponse);

							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									inProgress.close();
									if (rResponse != null) {
										addRespDataText.setText(rResponse.toString());
									}
								}
							});
						}
					}).start();
				}
			}
		});
		btmReport.setText("Report");
		btmReport.setBounds(150, 21, 94, 40);

		Button btnBanking = new Button(grpReport, SWT.NONE);
		btnBanking.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!isConnected)
					JOptionPane.showMessageDialog(null, "PORT NOT OPEN");
				else {
					clear();
					new Thread(new Runnable() {
						@Override
						public void run() {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									displayInProgress();
								}
							});
						}
					}).start();

					new Thread(new Runnable() {
						@Override
						public void run() {
							rResponse = ReqMsgCall.reportBanking(rResponse);

							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									inProgress.close();

									if (rResponse != null) {
										displayResponse(((ITerminalResponse) rResponse), device);
									}
								}
							});
						}
					}).start();
				}
			}
		});
		btnBanking.setText("Banking");
		btnBanking.setBounds(10, 71, 94, 40);

		Button btbSpltRprt = new Button(grpReport, SWT.NONE);
		btbSpltRprt.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!isConnected)
					JOptionPane.showMessageDialog(null, "PORT NOT OPEN");
				else {
					clear();
					new Thread(new Runnable() {
						@Override
						public void run() {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									displayInProgress();
								}
							});
						}
					}).start();

					new Thread(new Runnable() {
						@Override
						public void run() {
							rResponse = ReqMsgCall.xmlSplitr(rResponse);

							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									inProgress.close();
									if (rResponse != null) {
										addRespDataText.setText(rResponse.toString());
									}
								}
							});
						}
					}).start();
				}
			}
		});
		btbSpltRprt.setText("Split Report");
		btbSpltRprt.setBounds(150, 121, 94, 40);

		Button btnXBal = new Button(grpReport, SWT.NONE);
		btnXBal.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!isConnected)
					JOptionPane.showMessageDialog(null, "PORT NOT OPEN");
				else {
					clear();
					new Thread(new Runnable() {
						@Override
						public void run() {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									displayInProgress();
								}
							});
						}
					}).start();

					new Thread(new Runnable() {
						@Override
						public void run() {
							rResponse = ReqMsgCall.reportXBal(rResponse);

							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									inProgress.close();
									if (rResponse != null) {
										displayResponse(((ITerminalResponse) rResponse), device);
									}
								}
							});
						}
					}).start();
				}
			}
		});
		btnXBal.setText("X Balance");
		btnXBal.setBounds(10, 121, 94, 40);

		Button btbTaxFree = new Button(grpReport, SWT.NONE);
		btbTaxFree.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!isConnected)
					JOptionPane.showMessageDialog(null, "PORT NOT OPEN");
				else {
					clear();
					new Thread(new Runnable() {
						@Override
						public void run() {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									displayInProgress();
								}
							});
						}
					}).start();

					new Thread(new Runnable() {
						@Override
						public void run() {
							rResponse = ReqMsgCall.xmlTaxfree(rResponse);

							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									inProgress.close();
									if (rResponse != null) {
										addRespDataText.setText(rResponse.toString());
									}
								}
							});
						}
					}).start();
				}
			}
		});
		btbTaxFree.setFont(SWTResourceManager.getFont("Segoe UI", 7, SWT.NORMAL));
		btbTaxFree.setText("Tax Free \r\nReceipt");
		btbTaxFree.setBounds(150, 171, 94, 40);

		Button btnZBal = new Button(grpReport, SWT.NONE);
		btnZBal.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!isConnected)
					JOptionPane.showMessageDialog(null, "PORT NOT OPEN");
				else {
					clear();
					new Thread(new Runnable() {
						@Override
						public void run() {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									displayInProgress();
								}
							});
						}
					}).start();

					new Thread(new Runnable() {
						@Override
						public void run() {
							rResponse = ReqMsgCall.reportZBal(rResponse);

							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									inProgress.close();
									if (rResponse != null) {
										displayResponse(((ITerminalResponse) rResponse), device);
									}
								}
							});
						}
					}).start();
				}
			}
		});
		btnZBal.setText("Z Balance");
		btnZBal.setBounds(10, 171, 94, 40);

		Button btnTicket = new Button(grpReport, SWT.NONE);
		btnTicket.setBounds(150, 71, 94, 40);
		btnTicket.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!isConnected)
					JOptionPane.showMessageDialog(null, "PORT NOT OPEN");
				else {
					clear();
					new Thread(new Runnable() {
						@Override
						public void run() {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									displayInProgress();
								}
							});
						}
					}).start();

					new Thread(new Runnable() {
						@Override
						public void run() {
							rResponse = ReqMsgCall.xmlTicket(rResponse);

							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									inProgress.close();
									if (rResponse != null) {
										addRespDataText.setText(rResponse.toString());
									}
								}
							});
						}
					}).start();
				}
			}
		});
		btnTicket.setText("Ticket");

		grpTerminalConfig = new Group(shlTestcom, SWT.NONE);
		grpTerminalConfig.setText("Terminal Configuration");
		grpTerminalConfig.setBounds(913, 559, 256, 172);

		Button btnCallTms = new Button(grpTerminalConfig, SWT.NONE);
		btnCallTms.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!isConnected)
					JOptionPane.showMessageDialog(null, "PORT NOT OPEN");
				else {
					clear();
					new Thread(new Runnable() {
						@Override
						public void run() {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									displayInProgress();
								}
							});
						}
					}).start();

					new Thread(new Runnable() {
						@Override
						public void run() {
							dResponse = ReqMsgCall.callTMS(dResponse);

							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									inProgress.close();
									if (dResponse != null) {
										displayResponse((ITerminalResponse) dResponse, device);
									}
								}
							});
						}
					}).start();
				}
			}
		});
		btnCallTms.setText("CALL TMS");
		btnCallTms.setBounds(10, 21, 94, 40);

		Button btnLogon = new Button(grpTerminalConfig, SWT.NONE);
		btnLogon.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!isConnected)
					JOptionPane.showMessageDialog(null, "PORT NOT OPEN");
				else {
					clear();
					new Thread(new Runnable() {
						@Override
						public void run() {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									displayInProgress();
								}
							});
						}
					}).start();
					
					ReqMsgCall.set_AutoTicketEnabled(chkEnableAutoTicket.getSelection());
					
					new Thread(new Runnable() {
						@Override
						public void run() {
							dResponse = ReqMsgCall.logOn(dResponse);

							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									inProgress.close();
									if (dResponse != null) {
										displayResponse((ITerminalResponse) dResponse, device);
									}
								}
							});
						}
					}).start();
				}
			}
		});
		btnLogon.setText("LOGON");
		btnLogon.setBounds(150, 21, 94, 40);
//		btnLogon.setEnabled(true);

		Button btnReset = new Button(grpTerminalConfig, SWT.NONE);
		btnReset.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!isConnected)
					JOptionPane.showMessageDialog(null, "PORT NOT OPEN");
				else {
					clear();
					new Thread(new Runnable() {
						@Override
						public void run() {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									displayInProgress();
								}
							});
						}
					}).start();

					new Thread(new Runnable() {
						@Override
						public void run() {
							dResponse = ReqMsgCall.reset(dResponse);

							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									inProgress.close();
									if (dResponse != null) {
										displayResponse((ITerminalResponse) dResponse, device);
									}
								}
							});
						}
					}).start();
				}
			}
		});
		btnReset.setText("RESET");
		btnReset.setBounds(10, 71, 94, 40);
//		btnReset.setEnabled(false);

		Button btnPosIdentifier = new Button(grpTerminalConfig, SWT.NONE);
		btnPosIdentifier.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!isConnected)
					JOptionPane.showMessageDialog(null, "PORT NOT OPEN");
				else {
					clear();
					new Thread(new Runnable() {
						@Override
						public void run() {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									displayInProgress();
								}
							});
						}
					}).start();

					new Thread(new Runnable() {
						@Override
						public void run() {
							dResponse = ReqMsgCall.posIdentifier(dResponse);

							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									inProgress.close();
									if (dResponse != null) {
										displayResponse((ITerminalResponse) dResponse, device);
									}
								}
							});
						}
					}).start();
				}
			}
		});
		btnPosIdentifier.setText("POS Identifier");
		btnPosIdentifier.setBounds(48, 125, 164, 40);
//		btnPosIdentifier.setEnabled(false);

		Button btnState = new Button(grpTerminalConfig, SWT.NONE);
		btnState.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!isConnected)
					JOptionPane.showMessageDialog(null, "PORT NOT OPEN");
				else {
					clear();
					new Thread(new Runnable() {
						@Override
						public void run() {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									displayInProgress();
								}
							});
						}
					}).start();

					new Thread(new Runnable() {
						@Override
						public void run() {
							dResponse = ReqMsgCall.state(dResponse);

							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									inProgress.close();
									if (dResponse != null) {
										displayResponse((ITerminalResponse) dResponse, device);
									}
								}
							});
						}
					}).start();
				}
			}
		});
		btnState.setText("STATE");
		btnState.setBounds(150, 71, 94, 40);
//		btnState.setEnabled(false);
		
		btnReceipt.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				JTextArea msg = new JTextArea(20,50);
				msg.setText(receipt);
				msg.setLineWrap(true);
				msg.setWrapStyleWord(true);				

				JScrollPane scrollPane = new JScrollPane(msg);

				JOptionPane.showMessageDialog(null, scrollPane, "Receipt", JOptionPane.PLAIN_MESSAGE);
			}
		});

		Group grpSettings = new Group(shlTestcom, SWT.SHADOW_IN);
		grpSettings.setText("Settings");
		grpSettings.setBounds(20, 546, 200, 66);

		txtTimeout = new Text(grpSettings, SWT.BORDER);
		txtTimeout.setText("65000");
		txtTimeout.setBounds(10, 25, 78, 26);

		Label lblTimeout = new Label(grpSettings, SWT.NONE);
		lblTimeout.setText("Timeout (ms)");
		lblTimeout.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.NORMAL));
		lblTimeout.setBounds(95, 28, 95, 20);
		
		grpPAT = new Composite(shlTestcom, SWT.BORDER);
		grpPAT.setBounds(241, 35, 922, 699);
		grpPAT.setVisible(false);
		
		Composite grpPATqueue = new Composite(grpPAT, SWT.NONE);
		grpPATqueue.setBounds(0, 10, 263, 675);
		
		Label lblConnectedDevicesString = new Label(grpPATqueue, SWT.NONE);
		lblConnectedDevicesString.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.BOLD));
		lblConnectedDevicesString.setBounds(10, 10, 178, 37);
		lblConnectedDevicesString.setText("Connected Devices:");
		
		lblConnectedDevices = new Label(grpPATqueue, SWT.NONE);
		lblConnectedDevices.setAlignment(SWT.CENTER);
		lblConnectedDevices.setText("0");
		lblConnectedDevices.setLocation(189, 10);
		lblConnectedDevices.setSize(38, 28);
		lblConnectedDevices.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.BOLD));
		
		Label lblNewLabel = new Label(grpPATqueue, SWT.NONE);
		lblNewLabel.setAlignment(SWT.CENTER);
		lblNewLabel.setFont(SWTResourceManager.getFont("Segoe UI", 13, SWT.BOLD));
		lblNewLabel.setBounds(10, 53, 233, 43);
		lblNewLabel.setText("Command Queues");
		
		listAddress = new List(grpPATqueue, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		listAddress.setBounds(0, 102, 119, 563);
		
		listCommand = new List(grpPATqueue, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		listCommand.setBounds(119, 102, 144, 563);
		
		grpPATTransact = new Composite(grpPAT, SWT.NONE);
		grpPATTransact.setBounds(269, 10, 381, 675);
		
		groupSplitSale = new Composite(grpPATTransact, SWT.NONE);
		groupSplitSale.setVisible(false);
		groupSplitSale.setBounds(0, 0, 380, 675);
		
		Label lblNewLabel_1_3_1_1 = new Label(groupSplitSale, SWT.NONE);
		lblNewLabel_1_3_1_1.setText("Pay@Table XML Data Request");
		lblNewLabel_1_3_1_1.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		lblNewLabel_1_3_1_1.setAlignment(SWT.CENTER);
		lblNewLabel_1_3_1_1.setBounds(10, 0, 360, 53);
		
		btnSplitDone = new Button(groupSplitSale, SWT.NONE);
		btnSplitDone.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				socketCLient = patRequestList.get(0).getSocket();
				isAdditionalMessage = btnSplitAdditionalMessage.getSelection();
				try {
					device.payAtTableResponse(socketCLient)
							.withPATResponseType(PATResponseType.CONF_OK)
							.withAmount(reqAmount)
							.withPATPaymentMode(isAdditionalMessage ? PATPaymentMode.USE_ADDITIONAL_MSG
									: PATPaymentMode.NO_ADDITIONAL_MSG)
							.withCurrencyCode("826").execute();
					hidePatTransactions();
					hideAllPatTransaction();
				} catch (ApiException e1) {
					e1.printStackTrace();
				}
				ongoing = false;
			}
		});
		btnSplitDone.setText("DONE");
		btnSplitDone.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		btnSplitDone.setBounds(10, 612, 360, 53);
		
		lblNewLabel_2_3_1_1 = new Label(groupSplitSale, SWT.WRAP | SWT.CENTER);
		lblNewLabel_2_3_1_1.setText("The terminal request a Split Sale Report confirmation response");
		lblNewLabel_2_3_1_1.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.BOLD));
		lblNewLabel_2_3_1_1.setBounds(10, 80, 360, 81);
		
		txtSplitAmount = new Text(groupSplitSale, SWT.BORDER | SWT.READ_ONLY);
		txtSplitAmount.setBounds(179, 242, 181, 43);
		
		Label lblNewLabel_4_1 = new Label(groupSplitSale, SWT.NONE);
		lblNewLabel_4_1.setText("Amount:");
		lblNewLabel_4_1.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		lblNewLabel_4_1.setBounds(10, 242, 163, 43);
		
		txtSplitRawData = new Text(groupSplitSale, SWT.BORDER | SWT.READ_ONLY | SWT.WRAP | SWT.V_SCROLL);
		txtSplitRawData.setBounds(10, 345, 350, 192);
		
		Label lblNewLabel_4_1_1 = new Label(groupSplitSale, SWT.NONE);
		lblNewLabel_4_1_1.setText("Raw Data:");
		lblNewLabel_4_1_1.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		lblNewLabel_4_1_1.setBounds(10, 297, 153, 43);
		
		btnSplitAdditionalMessage = new Button(groupSplitSale, SWT.CHECK);
		btnSplitAdditionalMessage.setText("Additional Message");
		btnSplitAdditionalMessage.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.BOLD));
		btnSplitAdditionalMessage.setBounds(10, 546, 203, 25);
		
		groupTransferData = new Composite(grpPATTransact, SWT.NONE);
		groupTransferData.setVisible(false);
		groupTransferData.setBounds(0, 0, 380, 675);
		
		Label lblNewLabel_1_3_1 = new Label(groupTransferData, SWT.NONE);
		lblNewLabel_1_3_1.setText("Pay@Table XML Data Request");
		lblNewLabel_1_3_1.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		lblNewLabel_1_3_1.setAlignment(SWT.CENTER);
		lblNewLabel_1_3_1.setBounds(10, 0, 360, 53);
		
		btnTransferDone = new Button(groupTransferData, SWT.NONE);
		btnTransferDone.setText("DONE");
		btnTransferDone.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		btnTransferDone.setBounds(10, 612, 360, 53);
		
		Label lblNewLabel_2_3_1 = new Label(groupTransferData, SWT.WRAP | SWT.CENTER);
		lblNewLabel_2_3_1.setText("The terminal request a Transfer Data confirmation response");
		lblNewLabel_2_3_1.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.BOLD));
		lblNewLabel_2_3_1.setBounds(10, 80, 360, 81);
		
		txtTransferAmount = new Text(groupTransferData, SWT.BORDER | SWT.READ_ONLY);
		txtTransferAmount.setBounds(179, 242, 181, 43);
		
		Label lblNewLabel_4 = new Label(groupTransferData, SWT.NONE);
		lblNewLabel_4.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		lblNewLabel_4.setBounds(10, 242, 163, 43);
		lblNewLabel_4.setText("Amount:");
		
		Button btnTransfer = new Button(groupTransferData, SWT.CHECK);
		btnTransfer.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.BOLD));
		btnTransfer.setBounds(10, 358, 203, 43);
		btnTransfer.setText("Additional Message");
		
		groupAddDetails = new Composite(grpPATTransact, SWT.NONE);
		groupAddDetails.setVisible(false);
		groupAddDetails.setBounds(0, 0, 380, 675);
		
		Label lblNewLabel_1_3 = new Label(groupAddDetails, SWT.NONE);
		lblNewLabel_1_3.setText("Pay@Table XML Data Request");
		lblNewLabel_1_3.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		lblNewLabel_1_3.setAlignment(SWT.CENTER);
		lblNewLabel_1_3.setBounds(10, 0, 360, 53);
		
		Button btnTableListDone_1 = new Button(groupAddDetails, SWT.NONE);
		btnTableListDone_1.setText("DONE");
		btnTableListDone_1.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		btnTableListDone_1.setBounds(10, 612, 360, 53);
		btnTableListDone_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!fileLocation.isEmpty()) {
					if (!fileLocation.contains(".xml" )) {
						JOptionPane.showMessageDialog(null, "File must be in XML Document.");
						isValidXML = false;
						return;
					} else {
						socketCLient = patRequestList.get(0).getSocket();
						display.asyncExec(new Runnable() {
							@Override
							public void run() {
								try {
									socketCLient = patRequestList.get(0).getSocket();
									device.payAtTableResponse(socketCLient).withXML(getFileLocation()).execute();
									ongoing = false;
									setFileLocation("");
									hidePatTransactions();
									hideAllPatTransaction();
								} catch (ApiException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						});
					}					
				} else {
					JOptionPane.showMessageDialog(null, "File location cannot be empty");
					isValidXML = false;
				}
			}
		});
		
		Label lblNewLabel_2_3 = new Label(groupAddDetails, SWT.WRAP | SWT.CENTER);
		lblNewLabel_2_3.setText("The terminal request a Additional Message XML response");
		lblNewLabel_2_3.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.BOLD));
		lblNewLabel_2_3.setBounds(10, 80, 360, 81);
		
		btnTableListBrowse_1 = new Button(groupAddDetails, SWT.NONE);
		btnTableListBrowse_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {			
				FileDialog dialog = new FileDialog(display.getActiveShell(), SWT.OPEN);
				String fileName = dialog.open();

				if (fileName != null) {
					Display.getDefault().asyncExec(new Runnable() {

						@Override
						public void run() {
//							btnTableListBrowse_1.setEnabled(false);
							File file = new File(fileName);
							fileLocation = file.getAbsolutePath();
	
							lblAddDetailsPath.setText(fileLocation);
							lblAddDetailsPath.setVisible(true);
						}

					});
				}	
			}
		});
		btnTableListBrowse_1.setText("Browse File");
		btnTableListBrowse_1.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		btnTableListBrowse_1.setBounds(10, 324, 162, 36);
		
		lblAddDetailsPath = new Label(groupAddDetails, SWT.WRAP);
		lblAddDetailsPath.setText("Path:");
		lblAddDetailsPath.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
		lblAddDetailsPath.setBounds(10, 377, 360, 44);
		
		groupTableList = new Composite(grpPATTransact, SWT.NONE);
		groupTableList.setBounds(0, 0, 380, 675);
		groupTableList.setVisible(false);
		
		lblNewLabel_1 = new Label(groupTableList, SWT.NONE);
		lblNewLabel_1.setAlignment(SWT.CENTER);
		lblNewLabel_1.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		lblNewLabel_1.setBounds(10, 0, 360, 53);
		lblNewLabel_1.setText("Pay@Table XML Data Request");
		
		Button btnTableListDone = new Button(groupTableList, SWT.NONE);
		btnTableListDone.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!fileLocation.isEmpty()) {
					if (!fileLocation.contains(".xml" )) {
						JOptionPane.showMessageDialog(null, "File must be in XML Document.");
						isValidXML = false;
						return;
					} else {
						socketCLient = patRequestList.get(0).getSocket();
						display.asyncExec(new Runnable() {
							@Override
							public void run() {
								try {
									socketCLient = patRequestList.get(0).getSocket();
									device.payAtTableResponse(socketCLient).withXML(getFileLocation()).execute();
									ongoing = false;
									setFileLocation("");
									hidePatTransactions();
									hideAllPatTransaction();
								} catch (ApiException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						});
					}					
				} else {
					JOptionPane.showMessageDialog(null, "File location cannot be empty");
					isValidXML = false;
				}
			}
		});
		btnTableListDone.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		btnTableListDone.setBounds(10, 612, 360, 53);
		btnTableListDone.setText("DONE");
		
		lblNewLabel_2 = new Label(groupTableList, SWT.WRAP | SWT.CENTER);
		lblNewLabel_2.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.BOLD));
		lblNewLabel_2.setText("The terminal request a Table List XML response");
		lblNewLabel_2.setBounds(10, 80, 360, 81);
		
		Label lblNewLabel_3 = new Label(groupTableList, SWT.NONE);
		lblNewLabel_3.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		lblNewLabel_3.setBounds(10, 201, 126, 28);
		lblNewLabel_3.setText("Waiter ID:");
		
		txtWaiterIDTableList = new Text(groupTableList, SWT.BORDER | SWT.READ_ONLY);
		txtWaiterIDTableList.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
		txtWaiterIDTableList.setEditable(false);
		txtWaiterIDTableList.setBounds(153, 197, 199, 28);
		
		Label lblNewLabel_3_2 = new Label(groupTableList, SWT.NONE);
		lblNewLabel_3_2.setText("Table ID:");
		lblNewLabel_3_2.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		lblNewLabel_3_2.setBounds(10, 235, 126, 28);
		
		txtTableID = new Text(groupTableList, SWT.BORDER | SWT.READ_ONLY);
		txtTableID.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
		txtTableID.setEditable(false);
		txtTableID.setBounds(153, 231, 199, 28);
		
		Label lblNewLabel_3_2_1 = new Label(groupTableList, SWT.NONE);
		lblNewLabel_3_2_1.setText("Terminal Currency:");
		lblNewLabel_3_2_1.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblNewLabel_3_2_1.setBounds(10, 303, 142, 28);
		
		txtTerminalid = new Text(groupTableList, SWT.BORDER | SWT.READ_ONLY);
		txtTerminalid.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
		txtTerminalid.setEditable(false);
		txtTerminalid.setBounds(153, 265, 199, 28);
		
		btnTableListBrowse = new Button(groupTableList, SWT.NONE);
		btnTableListBrowse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {			
				FileDialog dialog = new FileDialog(display.getActiveShell(), SWT.OPEN);
				String fileName = dialog.open();

				if (fileName != null) {
//					btnTableListBrowse.setEnabled(false);
					Display.getDefault().asyncExec(new Runnable() {

						@Override
						public void run() {
							File file = new File(fileName);
							fileLocation = file.getAbsolutePath();

							lblPath.setText(fileLocation);
							lblPath.setVisible(true);
						}
					});
				}	
			}
		});
		btnTableListBrowse.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		btnTableListBrowse.setBounds(10, 426, 162, 44);
		btnTableListBrowse.setText("Browse File");
		
		lblPath = new Label(groupTableList, SWT.WRAP);
		lblPath.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
		lblPath.setBounds(10, 501, 360, 53);
		lblPath.setText("Path:");
		
		Label lblNewLabel_3_3 = new Label(groupTableList, SWT.NONE);
		lblNewLabel_3_3.setText("Terminal ID:");
		lblNewLabel_3_3.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		lblNewLabel_3_3.setBounds(10, 269, 126, 28);
		
		txtTerminalCurr = new Text(groupTableList, SWT.BORDER | SWT.READ_ONLY);
		txtTerminalCurr.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
		txtTerminalCurr.setEditable(false);
		txtTerminalCurr.setBounds(153, 303, 199, 28);
		
		groupTableLock = new Composite(grpPATTransact, SWT.NONE);
		groupTableLock.setVisible(false);
		groupTableLock.setBounds(0, 0, 380, 675);
		
		Label lblNewLabel_1_2 = new Label(groupTableLock, SWT.NONE);
		lblNewLabel_1_2.setText("Pay@Table Request");
		lblNewLabel_1_2.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		lblNewLabel_1_2.setAlignment(SWT.CENTER);
		lblNewLabel_1_2.setBounds(10, 0, 360, 53);
		
		btnTableLockAccept = new Button(groupTableLock, SWT.NONE);
		btnTableLockAccept.setText("Accept");
		btnTableLockAccept.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		btnTableLockAccept.setBounds(10, 612, 174, 53);
		
		btnTableLockDecline = new Button(groupTableLock, SWT.NONE);
		btnTableLockDecline.setText("Decline");
		btnTableLockDecline.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		btnTableLockDecline.setBounds(196, 612, 174, 53);
		
		Label lblNewLabel_2_1 = new Label(groupTableLock, SWT.WRAP | SWT.CENTER);
		lblNewLabel_2_1.setAlignment(SWT.CENTER);
		lblNewLabel_2_1.setText("The terminal request a Table Lock\r\n confirmation response");
		lblNewLabel_2_1.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.BOLD));
		lblNewLabel_2_1.setBounds(10, 74, 360, 82);
		
		Label lblNewLabel_3_1 = new Label(groupTableLock, SWT.NONE);
		lblNewLabel_3_1.setText("Waiter ID:");
		lblNewLabel_3_1.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		lblNewLabel_3_1.setBounds(10, 201, 144, 23);
		
		txtWaiterID = new Text(groupTableLock, SWT.BORDER);
		txtWaiterID.setEditable(false);
		txtWaiterID.setBounds(160, 197, 210, 27);
		
		Label lblNewLabel_3_1_1 = new Label(groupTableLock, SWT.NONE);
		lblNewLabel_3_1_1.setText("Table ID:");
		lblNewLabel_3_1_1.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		lblNewLabel_3_1_1.setBounds(10, 239, 144, 27);
		
		txtTableNumber = new Text(groupTableLock, SWT.BORDER | SWT.READ_ONLY);
		txtTableNumber.setEditable(false);
		txtTableNumber.setBounds(160, 231, 210, 27);
		
		Label lblNewLabel_3_1_1_1 = new Label(groupTableLock, SWT.NONE);
		lblNewLabel_3_1_1_1.setText("Terminal ID:");
		lblNewLabel_3_1_1_1.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		lblNewLabel_3_1_1_1.setBounds(10, 272, 134, 23);
		
		txtTerminalID = new Text(groupTableLock, SWT.BORDER | SWT.READ_ONLY);
		txtTerminalID.setEditable(false);
		txtTerminalID.setBounds(160, 264, 210, 31);
		
		Label lblNewLabel_3_1_1_1_1 = new Label(groupTableLock, SWT.NONE);
		lblNewLabel_3_1_1_1_1.setText("Currency:");
		lblNewLabel_3_1_1_1_1.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		lblNewLabel_3_1_1_1_1.setBounds(10, 301, 124, 27);
		
		txtTerminalCurrency = new Text(groupTableLock, SWT.BORDER | SWT.READ_ONLY);
		txtTerminalCurrency.setEditable(false);
		txtTerminalCurrency.setBounds(160, 301, 210, 29);
		
		Label lblNewLabel_3_1_1_1_1_1 = new Label(groupTableLock, SWT.NONE);
		lblNewLabel_3_1_1_1_1_1.setText("Amount:");
		lblNewLabel_3_1_1_1_1_1.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		lblNewLabel_3_1_1_1_1_1.setBounds(10, 334, 144, 31);
		
		txtAmount = new Text(groupTableLock, SWT.BORDER);
		txtAmount.setBounds(160, 336, 210, 31);
		
		groupReceipt = new Composite(grpPATTransact, SWT.NONE);
		groupReceipt.setVisible(false);
		groupReceipt.setBounds(0, 0, 380, 675);
		
		Label lblNewLabel_1_1 = new Label(groupReceipt, SWT.NONE);
		lblNewLabel_1_1.setText("Pay@Table XML Data Request");
		lblNewLabel_1_1.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		lblNewLabel_1_1.setAlignment(SWT.CENTER);
		lblNewLabel_1_1.setBounds(10, 0, 360, 53);
		
		Button btnNewButton_2 = new Button(groupReceipt, SWT.NONE);
		btnNewButton_2.setText("DONE");
		btnNewButton_2.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		btnNewButton_2.setBounds(10, 612, 360, 53);
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!fileLocation.isEmpty()) {
					if (!fileLocation.contains(".xml" )) {
						JOptionPane.showMessageDialog(null, "File must be in XML Document.");
						isValidXML = false;
						return;
					} else {
						socketCLient = patRequestList.get(0).getSocket();
						display.asyncExec(new Runnable() {
							@Override
							public void run() {
								try {
									socketCLient = patRequestList.get(0).getSocket();
									device.payAtTableResponse(socketCLient).withXML(getFileLocation()).execute();
									ongoing = false;
									setFileLocation("");
									hidePatTransactions();
									hideAllPatTransaction();
								} catch (ApiException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						});
					}					
				} else {
					JOptionPane.showMessageDialog(null, "File location cannot be empty");
					isValidXML = false;
				}
			}
		});
		
		Label lblNewLabel_2_2 = new Label(groupReceipt, SWT.WRAP | SWT.CENTER);
		lblNewLabel_2_2.setAlignment(SWT.CENTER);
		lblNewLabel_2_2.setText("The terminal request a Receipt Message XML response");
		lblNewLabel_2_2.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.BOLD));
		lblNewLabel_2_2.setBounds(10, 66, 360, 94);
		
		btnBrowseReceipt = new Button(groupReceipt, SWT.NONE);
		btnBrowseReceipt.setText("Browse File");
		btnBrowseReceipt.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		btnBrowseReceipt.setBounds(10, 324, 162, 36);
		btnBrowseReceipt.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {			
				FileDialog dialog = new FileDialog(display.getActiveShell(), SWT.OPEN);
				String fileName = dialog.open();

				if (fileName != null) {
					Display.getDefault().asyncExec(new Runnable() {
						@Override
						public void run() {
//							btnBrowseReceipt.setEnabled(false);
							File file = new File(fileName);
							fileLocation = file.getAbsolutePath();

							lblReceiptPath.setText(fileLocation);
							lblReceiptPath.setVisible(true);
						}

					});
				}	
			}
		});
		
		lblReceiptPath = new Label(groupReceipt, SWT.WRAP);
		lblReceiptPath.setText("Path:");
		lblReceiptPath.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
		lblReceiptPath.setBounds(10, 377, 360, 44);
		
		groupTransactionOutcome = new Composite(grpPATTransact, SWT.NONE);
		groupTransactionOutcome.setBounds(0, 0, 380, 675);
		groupTransactionOutcome.setVisible(false);
		
		Label lblNewLabel_1_2_1 = new Label(groupTransactionOutcome, SWT.NONE);
		lblNewLabel_1_2_1.setText("Pay@Table Request");
		lblNewLabel_1_2_1.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		lblNewLabel_1_2_1.setAlignment(SWT.CENTER);
		lblNewLabel_1_2_1.setBounds(10, 0, 360, 53);
		
		btnTransactionOutcomeAccept = new Button(groupTransactionOutcome, SWT.NONE);
		btnTransactionOutcomeAccept.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				socketCLient = patRequestList.get(0).getSocket();
				isAdditionalMessage = btnTransAdditionalData.getSelection();
				try {
					device.payAtTableResponse(socketCLient)
					.withPATResponseType(PATResponseType.CONF_OK)
					.withAmount(reqAmount)
					.withPATPaymentMode(isAdditionalMessage ? PATPaymentMode.USE_ADDITIONAL_MSG
							: PATPaymentMode.NO_ADDITIONAL_MSG)
					.execute();
					ongoing = false;
					hidePatTransactions();
					hideAllPatTransaction();
				} catch (ApiException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}		
			}
		});
		btnTransactionOutcomeAccept.setText("Accept");
		btnTransactionOutcomeAccept.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		btnTransactionOutcomeAccept.setBounds(10, 612, 174, 53);
		
		btnTransactionOutcomeDecline = new Button(groupTransactionOutcome, SWT.NONE);
		btnTransactionOutcomeDecline.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				socketCLient = patRequestList.get(0).getSocket();
				isAdditionalMessage = btnTransAdditionalData.getSelection();
				try {
					device.payAtTableResponse(socketCLient)
					.withPATResponseType(PATResponseType.CONF_NOK)
					.withAmount(reqAmount)
					.withPATPaymentMode(isAdditionalMessage ? PATPaymentMode.USE_ADDITIONAL_MSG
							: PATPaymentMode.NO_ADDITIONAL_MSG)
					.execute();
					ongoing = false;
					hidePatTransactions();
					hideAllPatTransaction();
				} catch (ApiException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}		
			}
		});
		btnTransactionOutcomeDecline.setText("Decline");
		btnTransactionOutcomeDecline.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.BOLD));
		btnTransactionOutcomeDecline.setBounds(196, 612, 174, 53);
		
		
		Label lblNewLabel_2_1_1 = new Label(groupTransactionOutcome, SWT.WRAP | SWT.CENTER);
		lblNewLabel_2_1_1.setText("The terminal request a Transaction Outcome confirmation response");
		lblNewLabel_2_1_1.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.BOLD));
		lblNewLabel_2_1_1.setAlignment(SWT.CENTER);
		lblNewLabel_2_1_1.setBounds(10, 54, 360, 90);
		
		Label lblNewLabel_3_1_2 = new Label(groupTransactionOutcome, SWT.NONE);
		lblNewLabel_3_1_2.setText("Transaction Status:");
		lblNewLabel_3_1_2.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		lblNewLabel_3_1_2.setBounds(10, 201, 174, 23);
		
		txtTransStatus = new Text(groupTransactionOutcome, SWT.BORDER | SWT.WRAP);
		txtTransStatus.setEditable(false);
		txtTransStatus.setBounds(196, 197, 174, 27);
		
		Label lblNewLabel_3_1_1_2 = new Label(groupTransactionOutcome, SWT.NONE);
		lblNewLabel_3_1_1_2.setText("Transaction Outcome:");
		lblNewLabel_3_1_1_2.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		lblNewLabel_3_1_1_2.setBounds(10, 239, 360, 27);
		
		txtTransOutCome = new Text(groupTransactionOutcome, SWT.BORDER | SWT.WRAP);
		txtTransOutCome.setEditable(false);
		txtTransOutCome.setBounds(10, 273, 360, 84);
		
		Label lblNewLabel_3_1_2_1 = new Label(groupTransactionOutcome, SWT.NONE);
		lblNewLabel_3_1_2_1.setText("Amount:");
		lblNewLabel_3_1_2_1.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		lblNewLabel_3_1_2_1.setBounds(10, 367, 86, 23);
		
		txtTransAmount = new Text(groupTransactionOutcome, SWT.BORDER);
		txtTransAmount.setEditable(false);
		txtTransAmount.setBounds(102, 363, 268, 34);
		
		Label lblNewLabel_3_1_2_1_1 = new Label(groupTransactionOutcome, SWT.NONE);
		lblNewLabel_3_1_2_1_1.setText("Currency:");
		lblNewLabel_3_1_2_1_1.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		lblNewLabel_3_1_2_1_1.setBounds(10, 408, 83, 27);
		
		txtTransCurrency = new Text(groupTransactionOutcome, SWT.BORDER);
		txtTransCurrency.setEditable(false);
		txtTransCurrency.setBounds(102, 403, 268, 34);
		
		btnTransactionOutcome = new Button(groupTransactionOutcome, SWT.NONE);
		btnTransactionOutcome.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		btnTransactionOutcome.setBounds(10, 472, 360, 47);
		btnTransactionOutcome.setText("Transaction Outcome");
		
		btnTransAdditionalData = new Button(groupTransactionOutcome, SWT.CHECK);
		btnTransAdditionalData.setBounds(10, 533, 174, 47);
		btnTransAdditionalData.setText("Additional Data");
		
		groupRawPAT = new Composite(grpPAT, SWT.NONE);
		groupRawPAT.setEnabled(true);
		groupRawPAT.setBounds(656, 10, 252, 675);
		groupRawPAT.setVisible(true);
		
		txtRawMessages = new Text(groupRawPAT, SWT.BORDER | SWT.READ_ONLY | SWT.CENTER);
		txtRawMessages.setFont(SWTResourceManager.getFont("Segoe UI", 14, SWT.BOLD));
		txtRawMessages.setText("Raw Messages");
		txtRawMessages.setBounds(0, 0, 252, 36);
		
		txtRawMessagesPAT = new Text(groupRawPAT, SWT.BORDER | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL);
		txtRawMessagesPAT.setBounds(0, 45, 252, 620);	
		

		clickListeners(conf);
		
	}

	@Override
	public void update(Observable o, Object arg) {

	}
	

	public void clear() {
		rawReqText.setText("");
		ePosNumText2.setText("");
		transacStatusText.setText("");
		respAmntText.setText("");
		paymentText.setText("");
		currency2Text.setText("");
		respDataText.setText("");
		privDataText2.setText("");
		authCodetext.setText("");
		cashBackText.setText("");
		gratuityText.setText("");
		ftxnAmntTxt.setText("");
		availAmntText.setText("");
		payMethodText.setText("");
		txnSubTypeText.setText("");
		sSaleAmntText.setText("");
		dccAmnt.setText("");
		dccCurrency.setText("");
		dccStatus.setText("");
		addRespDataText.setText("");
		respMsgText.setText("");
		reqCashback = "";
		reqTableNumber = "";
		reqAuthCode = "";
		reqTransCode = "";
		appendBroadcast = "";
	}

	public void displayResponse(ITerminalResponse response, IDeviceInterface device) {
		if (response == null) {
			return;
		}

		// to get response message
//		response.getReferenceNumber(); // epos number
//		response.getStatus(); // transaction status
//		response.getTransactionAmount(); // transaction amount
//		response.getPaymentMode(); // transaction payment mode
//		response.getCurrencyCode(); // transaction currency code
//		response.getPrivateData(); // private data
//		
//		// to get all response data fields
//					response.getAuthorizationCode(); // authorization code
//					response.getCashBackAmount(); // cashback amount
//					response.getTipAmount(); // gratuity amount
//					response.getFinalTransactionAmount(); // final transaction amount
//					response.getBalanceAmount(); // available amount
//					response.getPaymentMethod(); // payment method
//					response.getTransactionSubType(); //transaction sub type
//					response.getSplitSaleAmount(); // split sale amount
//					response.getDynamicCurrencyCodeAmount(); // dcc amount
//					response.getDynamicCurrencyCode(); // dcc currency
//					response.getDynamicCurrencyCodeStatus(); // dcc operated status

//		respMsgText.setText(response.toString());
//		ePosNumText2.setText(((IngenicoBaseResponse) response).getReferenceNumber() == null ? ""
//				: ((IngenicoBaseResponse) response).getReferenceNumber());
//		transacStatusText.setText(response.getStatus() == null ? "" : response.getStatus());
//		respAmntText.setText(response.getTransactionAmount() == null ? "" : response.getTransactionAmount().toString());
//		paymentText.setText(((IngenicoTerminalResponse) response).getPaymentMode() == null ? ""
//				: ((IngenicoTerminalResponse) response).getPaymentMode().toString());
//		respDataText.setText("");
//		currency2Text.setText(((IngenicoTerminalResponse) response).getCurrencyCode() == null ? ""
//				: ((IngenicoTerminalResponse) response).getCurrencyCode());
//		privDataText2.setText(((IngenicoTerminalResponse) response).getPrivateData() == null ? ""
//				: ((IngenicoTerminalResponse) response).getPrivateData());

		respMsgText.setText(response.toString());
		ePosNumText2.setText(response.getReferenceNumber() == null ? "" : response.getReferenceNumber());
		transacStatusText.setText(response.getStatus() == null ? "" : response.getStatus());
		respAmntText.setText(response.getTransactionAmount() == null ? "" : response.getTransactionAmount().toString());
		paymentText.setText(response.getPaymentMode() == null ? "" : response.getPaymentMode().toString());
		respDataText.setText("");
		currency2Text.setText(response.getCurrencyCode() == null ? "" : response.getCurrencyCode());
		privDataText2.setText(response.getPrivateData() == null ? "" : response.getPrivateData());

		if(((IngenicoBaseResponse) response).getReceipt() == null) {
			btnReceipt.setEnabled(false);
		}else {
			btnReceipt.setEnabled(true);
			receipt = ((IngenicoBaseResponse) response).getReceipt();
		}
		
		displayRespData(response);
		reqCashback = "";
		reqTableNumber = "";
		reqAuthCode = "";
		isCashback = false;
		isTableNumber = false;
	}

	private void displayRespData(ITerminalResponse response) {
		if (rawReqText.getText().contains("PID")) {
			addRespDataText.setText(
					String.format("SERIAL NUMBER: \n%s", ((IInitializeResponse) response).getSerialNumber().trim()));
		} else if (rawReqText.getText().contains("STATE")) {
			StringBuilder s = new StringBuilder();
			s.append("Terminal Status:\n" + ((TerminalStateResponse) response).getTerminalStatus().toString());
			s.append("\n\nSales Mode:\n" + ((TerminalStateResponse) response).getSalesMode().toString());
			s.append("\n\nTerminal Capabilities:\n" + ((TerminalStateResponse) response).getTerminalCapabilities());
			s.append("\n\nAdditional Terminal Capabilities:\n"
					+ ((TerminalStateResponse) response).getAdditionalTerminalCapabilities());
			s.append("\n\nApplication Version:\n" + ((TerminalStateResponse) response).getAppVersion());
			s.append("\n\nHandset Number:\n" + ((TerminalStateResponse) response).getHandsetNumber());
			s.append("\n\nTerminal Id:\n" + ((TerminalStateResponse) response).getTerminalId());
			addRespDataText.setText(s.toString());
		} else {
			authCodetext.setText((response.getAuthorizationCode() != null ? response.getAuthorizationCode() : ""));
			cashBackText.setText((response.getCashBackAmount() != null ? response.getCashBackAmount().toString() : ""));
			gratuityText.setText(
					(response.getTipAmount() != null ? String.format("%s", response.getTipAmount().toString()) : ""));
			ftxnAmntTxt.setText(
					response.getFinalTransactionAmount() != null ? response.getFinalTransactionAmount().toString()
							: "");
			availAmntText.setText((response.getBalanceAmount() != null ? response.getBalanceAmount().toString() : ""));
			payMethodText.setText(response.getPaymentMethod() != null ? response.getPaymentMethod() : "");
			txnSubTypeText.setText("");
			sSaleAmntText.setText("");
			dccAmnt.setText("");
			dccCurrency.setText(response.getDynamicCurrencyCode() != null ? response.getDynamicCurrencyCode() : "");
			dccStatus.setText("");

//			authCodetext.setText((response.getAuthorizationCode() != null ? response.getAuthorizationCode() : ""));
//			cashBackText.setText((response.getCashBackAmount() != null ? response.getCashBackAmount().toString() : ""));
//			gratuityText.setText(
//					(response.getTipAmount() != null ? String.format("%s", response.getTipAmount().toString()) : ""));
//			ftxnAmntTxt.setText((((IngenicoTerminalResponse) response).getFinalTransactionAmount() != null
//					? ((IngenicoTerminalResponse) response).getFinalTransactionAmount().toString()
//					: ""));
//			availAmntText.setText((response.getBalanceAmount() != null ? response.getBalanceAmount().toString() : ""));
//			payMethodText.setText(response.getPaymentType().toString());
//			txnSubTypeText.setText("");
//			sSaleAmntText.setText("");
//			dccAmnt.setText("");
//			dccCurrency.setText((((IngenicoTerminalResponse) response).getDccCurrency() != null
//					? ((IngenicoTerminalResponse) response).getDccCurrency()
//					: ""));
//			dccStatus.setText("");
		}
	}

	private void showAuthCodeForm() {
		popUp03 popsy03 = new popUp03(null);
		if (reqAuthCode != null) {
			popsy03.setAuthCode(reqAuthCode);
		}

		popsy03.open();
		popsy03.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent arg0) {
				reqAuthCode = popsy03.getAuthCode();
			}
		});
	}

	private void displayInProgress() {
		transactionInProgress inProgress = new transactionInProgress(null);
		inProgress.setDevice(device, dResponse, window);
		inProgress.setReferenceNumber(ePosNumText.getText());
		inProgress.setAmount(reqsAmntText.getText());
		inProgress.open();
		this.inProgress = inProgress.OK();
	}

	private void patConfirmation(ConfirmationResponse conf) {
		socketCLient = patRequestList.get(0).getSocket();
		try {
			if (ongoing) {
				device.payAtTableResponse(socketCLient)
				.withPATResponseType(conf.getConfirmation() ? PATResponseType.CONF_OK : PATResponseType.CONF_NOK)
				.withAmount(reqAmount)
				.withPATPaymentMode(conf.getUseAdditionalMsg() ? PATPaymentMode.USE_ADDITIONAL_MSG
						: PATPaymentMode.NO_ADDITIONAL_MSG)
				.withCurrencyCode("826")
				.execute();
				ongoing = false;
			}	
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}

	private void payAtTableResponse(PATRequest request) {
		try {		
			if (!ongoing) {
			display.asyncExec(new Runnable() {
				@Override
				public void run() {
					PATRequestType requestType = request.getRequestType();
																										
					conf = new ConfirmationResponse(null);
					switch (requestType.getValue()) {
					case 1:
						//Table Lock
						ongoing = true;						
						Display.getDefault().asyncExec(new Runnable() {
							@Override
							public void run() {
//								hideAllPatTransaction();
								groupTableLock.setVisible(true);	
								txtAmount.addVerifyListener(new VerifyListener() {
									public void verifyText(VerifyEvent e) {
										String string = e.text;
										char[] chars = new char[string.length()];
										string.getChars(0, chars.length, chars, 0);
										for (int i = 0; i < chars.length; i++) {
											if (!('0' <= chars[i] && chars[i] <= '9' || chars[i] == '.')) {
												e.doit = false;
												return;
											} 
										}
									}
								});
							}
						});
				
						Display.getDefault().asyncExec(new Runnable() {
							@Override
							public void run() {
								txtAmount.setText("");
								if ((request.getWaiterId() != null)) {
									txtWaiterID.setText(request.getWaiterId());
									waiterID = request.getWaiterId();
								}	
								if ((request.getTableId() != null)) {
									txtTableNumber.setText(request.getTableId());
									tableID = request.getTableId();
								}	
								if ((request.getTerminalCurrency() != null)) {
									txtTerminalCurrency.setText(request.getTerminalCurrency());
									terminalCurrency = request.getTerminalCurrency();
								}	
								if ((request.getTerminalId() != null)) {
									txtTerminalID.setText(request.getTerminalId());
									terminalID = request.getTerminalId();
								}	
							}
						});	
						
						conf.setTableId(request.getTableId()); 
						conf.setWaiterId(request.getWaiterId());
						conf.setTerminalId(request.getTerminalId());
						conf.setTerminalCurrency(request.getTerminalCurrency());
						conf.displayAdditionalMsg(false);				
						
						break;
					case 2:
						ongoing = true;
						hideAllPatTransaction();
		
						new Thread(new Runnable() {
							@Override
							public void run() {
								if (!patRequestList.isEmpty()) {
									try {					
										ongoing = false;
										Thread.sleep(1000);
										reqAmount = null;
										setFileLocation("");
										if (!ongoing) {					
											payAtTableResponse(patRequestList.get(0));	
										}	
									} catch (InterruptedException e) {
										e.printStackTrace();
									}
								} else {
									ongoing = false;
								}
							}
						}).start();
						break;
					case 3:
						ongoing = true;
						Display.getDefault().asyncExec(new Runnable() {
							@Override
							public void run() {
								hideAllPatTransaction();
								groupTableList.setVisible(true);
								lblPath.setText("");
								lblPath.setVisible(false);
								btnTableListBrowse.setEnabled(true);
								lblNewLabel_2.setText("The terminal request a Receipt Message XML response");
							}
						});	
						txtWaiterIDTableList.setText(waiterID != null ? waiterID : "");
						txtTableID.setText(tableID != null ? tableID : "");
						txtTerminalCurr.setText(terminalCurrency != null ? terminalCurrency : "");
						txtTerminalid.setText(terminalID != null ? terminalID : "");
						
//						txtWaiterIDTableList.setText("");
//						txtTableID.setText("");
//						txtTerminalCurr.setText("");
//						txtTerminalid.setText("");	
//						xml.setTitle("Pay@Table XML Data Request");
//						xml.setMessage("The terminal request a Receipt Message XML response");
//						xml.open();
//
//						xml.addDisposeListener(new DisposeListener() {
//
//							@Override
//							public void widgetDisposed(DisposeEvent arg0) {
//								if (xml.getIsDone()) {
//									try {
//										device.payAtTableResponse().withXML(xml.getFileLocation()).execute();
//										ongoing = false;
//									} catch (ApiException e) {
//										e.printStackTrace();
//									}
//								}
//							}
//
//						});

						break;
					case 4:
						ongoing = true;		
						Display.getDefault().asyncExec(new Runnable() {
							@Override
							public void run() {
								hideAllPatTransaction();
								groupTableList.setVisible(true);
								lblPath.setText("");
								lblPath.setVisible(false);
								btnTableListBrowse.setEnabled(true);
								lblNewLabel_2.setText("The terminal request a Table List XML respons");
							}
						});				
						
						if (request.getWaiterId() != null) {
							txtWaiterIDTableList.setText(request.getWaiterId());	
						}
						
						if (request.getTableId() != null) {
							txtTableID.setText(request.getTableId());
						}	
						
						if (request.getTerminalCurrency() != null) {
							txtTerminalCurr.setText(request.getTerminalCurrency());	
						}
						
						if (request.getTerminalId() != null) {
							txtTerminalid.setText(request.getTerminalId());	
						}
					
//						xml.setTitle("Pay@Table XML Data Request");
//						xml.setMessage("The terminal request a Table List XML response");
//						xml.open();
//
//						xml.addDisposeListener(new DisposeListener() {
//
//							@Override
//							public void widgetDisposed(DisposeEvent arg0) {
//								// TODO Auto-generated method stub
//								if (xml.getIsDone()) {
//									try {
//										device.payAtTableResponse().withXML(xml.getFileLocation()).execute();
//										ongoing = false;
//									} catch (ApiException e) {
//										e.printStackTrace();
//									}
//								}
//							}
//
//						});

						break;
					case 5:				
						ongoing = true;
						btnTransAdditionalData.setSelection(false);
						Display.getDefault().asyncExec(new Runnable() {
							@Override
							public void run() {
								hideAllPatTransaction();
								groupTransactionOutcome.setVisible(true);
								btnTransactionOutcome.setEnabled(true);
							}
						});
						new Thread(new Runnable() {
							@Override
							public void run() {							
								Display.getDefault().asyncExec(new Runnable() {
									@Override
									public void run() {									
										if ((request.getTransactionOutcome().getTransactionStatus().name() != null)) {
											txtTransStatus.setText(request.getTransactionOutcome().getTransactionStatus().name());
										}
										if ((request.getTransactionOutcome() != null)) {
											txtTransOutCome.setText(request.getTransactionOutcome().toString());
										}
										if ((request.getTransactionOutcome().getCurrencyCode() != null)) {
											txtTransCurrency.setText(request.getTransactionOutcome().getCurrencyCode());
										}
										txtTransAmount.setText(reqAmount.toString());
										
										if (btnTransAdditionalData.getSelection() == true) {
											isAdditionalMessage = true;
										} else {
											isAdditionalMessage = false;
										}											
									}
								});								
							}
						}).start();	
						
						transOut = request.getTransactionOutcome();

//						TransactionOutcome transOut = request.getTransactionOutcome();
//						conf.setTitle("Pay@Table Request");
//						conf.setMessage("The terminal request a Transaction Outcome confirmation response");
//						conf.setTransactionOutcomeStatus(transOut.getTransactionStatus().name());
//						conf.setRawData(transOut.toString());
//						conf.displayAdditionalMsg(true);
//						conf.setAmount(reqAmount);
//						conf.open();
//
//						conf.addDisposeListener(new DisposeListener() {
//
//							@Override
//							public void widgetDisposed(DisposeEvent arg0) {
//								isAdditionalMsg = conf.getUseAdditionalMsg();
//								patConfirmation(conf);
//
						
//								Display.getDefault().asyncExec(new Runnable() {
//
//									@Override
//									public void run() {
//										ePosNumText2.setText("00");
//										transacStatusText.setText(transOut.getTransactionStatus().toString());
//										respAmntText.setText(transOut.getAmount());
//										paymentText.setText("0");
//										currency2Text.setText(transOut.getCurrencyCode());
//										privDataText2.setText(transOut.getPrivateData());
//										respMsgText.setText(transOut.toString());
//
//										// response field
//										DataResponse dataResponse = transOut.getRepFields();
//										authCodetext.setText(dataResponse.getAuthorizationCode());
//										cashBackText.setText(dataResponse.getCashbackAmount() != null
//												? dataResponse.getCashbackAmount().toString()
//												: "");
//										gratuityText.setText(dataResponse.getGratuityAmount() != null
//												? dataResponse.getGratuityAmount().toString()
//												: "");
//										ftxnAmntTxt.setText(dataResponse.getFinalAmount() != null
//												? dataResponse.getFinalAmount().toString()
//												: "");
//										availAmntText.setText(dataResponse.getAvailableAmount() != null
//												? dataResponse.getAvailableAmount().toString()
//												: "");
//										payMethodText.setText(dataResponse.getPaymentMethod() != null
//												? dataResponse.getPaymentMethod().toString()
//												: "");
//										txnSubTypeText.setText(dataResponse.getTransactionSubType() != null
//												? dataResponse.getTransactionSubType().toString()
//												: "");
//										sSaleAmntText.setText(dataResponse.getSplitSaleAmount() != null
//												? dataResponse.getSplitSaleAmount().toString()
//												: "");
//										dccAmnt.setText(dataResponse.getDccAmount() != null
//												? dataResponse.getDccAmount().toString()
//												: "");
//										dccCurrency.setText(
//												dataResponse.getDccCode() != null ? dataResponse.getDccCode() : "");
//										dccStatus.setText(dataResponse.getDccStatus() != null
//												? dataResponse.getDccStatus().toString()
//												: "");
//									}
//
//								});
//								
//								ongoing = false;
//							}
//
//						});
						break;
					case 6:
						ongoing = true;
						Display.getDefault().asyncExec(new Runnable() {
							@Override
							public void run() {
								hideAllPatTransaction();
								groupTableList.setVisible(true);
								lblPath.setText("");
								lblPath.setVisible(false);
								btnTableListBrowse.setEnabled(true);
								lblNewLabel_2.setText("The terminal request a Additional Message XML response");
							}
						});	
						txtWaiterIDTableList.setText(waiterID != null ? waiterID : "");
						txtTableID.setText(tableID != null ? tableID : "");
						txtTerminalCurr.setText(terminalCurrency != null ? terminalCurrency : "");
						txtTerminalid.setText(terminalID != null ? terminalID : "");
												
//						xml.setTitle("Pay@Table XML Data Request");
//						xml.setMessage("The terminal request a Additional Message XML response");
//						xml.open();
//						
//						xml.addDisposeListener(new DisposeListener() {
//
//							@Override
//							public void widgetDisposed(DisposeEvent arg0) {
//								// TODO Auto-generated method stub
//								if (xml.getIsDone()) {
//									try {
//										device.payAtTableResponse().withXML(xml.getFileLocation()).execute();
//										ongoing = false;
//									} catch (ApiException e) {
//										e.printStackTrace();
//									}
//								}
//							}
//						});
						break;
					case 7:
						ongoing = true;
						Display.getDefault().asyncExec(new Runnable() {
							@Override
							public void run() {
								hideAllPatTransaction();
								groupTransferData.setVisible(true);								
							}
						});
						
						txtTransferAmount.setText(reqAmount.toString());
						
//						conf.setTitle("Pay@Table Request");
//						conf.setMessage("The terminal request a Transfer Data confirmation response");
//						conf.setAmount(reqAmount);
//						conf.checkAdditionalMsg(isAdditionalMsg);
//						conf.enableAdditionalMsg(false);
//						conf.open();
//
//						conf.addDisposeListener(new DisposeListener() {
//
//							@Override
//							public void widgetDisposed(DisposeEvent arg0) {
//								patConfirmation(conf);
//								ongoing = false;
//							}
//						});
						break;
					case 8:
						ongoing = true;
						Display.getDefault().asyncExec(new Runnable() {
							@Override
							public void run() {
								hideAllPatTransaction();
								groupSplitSale.setVisible(true);	
								lblReceiptPath.setVisible(false);
								lblReceiptPath.setText("");
								txtSplitAmount.setText(reqAmount.toString());
								txtSplitRawData.setText(request.getXMLData());
								btnSplitAdditionalMessage.setVisible(false);
//								btnSplitAdditionalMessage.setSelection(false);
								lblNewLabel_2_3_1_1.setText("The terminal request a Split Sale Report confirmation response");
							}
						});
						
//						conf.setTitle("Pay@Table Request");
//						conf.setMessage("The terminal request a Split Sale Report confirmation response");
//						conf.setAmount(reqAmount);
//						conf.setRawData(request.getXMLData());
//						conf.checkAdditionalMsg(isAdditionalMsg);
//						conf.enableAdditionalMsg(false);
//						conf.open();
//
//						conf.addDisposeListener(new DisposeListener() {
//
//							@Override
//							public void widgetDisposed(DisposeEvent arg0) {
//								patConfirmation(conf);
//								ongoing = false;
//							}
//
//						});
						break;
					case 9:
						ongoing = true;
						Display.getDefault().asyncExec(new Runnable() {
							@Override
							public void run() {
								hideAllPatTransaction();
								groupSplitSale.setVisible(true);	
								txtSplitAmount.setText(reqAmount.toString());
								txtSplitRawData.setText(request.getXMLData());
								btnSplitAdditionalMessage.setVisible(false);
								lblNewLabel_2_3_1_1.setText("The terminal request a Ticket confirmation response");
							}
						});
//						conf.setTitle("Pay@Table Request");
//						conf.setMessage("The terminal request a Ticket confirmation response");
//						conf.setAmount(reqAmount);
//						conf.setRawData(request.getXMLData());
//						conf.checkAdditionalMsg(isAdditionalMsg);
//						conf.enableAdditionalMsg(false);
//						conf.open();
//
//						conf.addDisposeListener(new DisposeListener() {
//
//							@Override
//							public void widgetDisposed(DisposeEvent arg0) {
//								patConfirmation(conf);
//								ongoing = false;
//							}
//
//						});
						break;
					case 10:
						ongoing = true;
						Display.getDefault().asyncExec(new Runnable() {
							@Override
							public void run() {
								hideAllPatTransaction();
								groupSplitSale.setVisible(true);	
								lblReceiptPath.setVisible(false);
								lblReceiptPath.setText("");
							}
						});
//						conf.setTitle("Pay@Table Request");
//						conf.setMessage("The terminal request a End of Day Report confirmation response");
//						conf.setAmount(reqAmount);
//						conf.setRawData(request.getXMLData());
//						conf.checkAdditionalMsg(isAdditionalMsg);
//						conf.enableAdditionalMsg(false);
//						conf.open();
//
//						conf.addDisposeListener(new DisposeListener() {
//
//							@Override
//							public void widgetDisposed(DisposeEvent arg0) {
//								patConfirmation(conf);
//								ongoing = false;
//							}
//
//						});
						break;
					default:
						JOptionPane.showMessageDialog(null, "Request type sent by the terminal cannot recognized");
						break;
					}
					
				}
			});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initializeSocket() {
		if (device != null && isSerial) {
			JOptionPane.showMessageDialog(null, "SIMULATOR IS CONNECTED VIA SERIAL");
		} else {
			if (device == null) {
				TcpCall.set_ipPortText(ipPortText.getText());
				TcpCall.setTimeout(txtTimeout.getText());
				new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							device = TcpCall.startLstn(device, isPayAtTable);
							
						} catch (Exception e) {
							e.printStackTrace();
						}
						
						Display.getDefault().asyncExec(new Runnable() {
							@Override
							public void run() {
								if (isPayAtTable) {
									showPATGroups();
									
								} else {
									showNonPATGroups();
								}
							}
						});
					
						new Thread(new Runnable() {
							@Override
							public void run() {
								while (device != null) {
									processRequest();
								}
							}
						}).start();
						
						new Thread(new Runnable() {
							@Override
							public void run() {
								updateConnectionStatus();
							}
						}).start();
						
						
						if (device != null) {							
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									try {
//										test.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
//										test.setText("CONNECTED");
										isConnected = true;
										isTcp = true;
										ReqMsgCall = new reqMsg(device, null);								
									JOptionPane.showMessageDialog(null, "IP LISTEN OK");	
										device.setOnMessageSent(new IMessageSentInterface() {
											public void messageSent(String message) {
												Display.getDefault().asyncExec(new Runnable() {
													@Override
													public void run() {															
														rawReqText.setText(message);
														_rawPATtMessage = "";
														_rawPATtMessage += message;
														txtRawMessagesPAT.append(_rawPATtMessage);
														txtRawMessagesPAT.append("\n");															
													}
												});
											}
										});
//										Thread.sleep(3000 * 10000);
									} catch (NumberFormatException | ApiException e1) {
										e1.printStackTrace();
									}
									
								}
							});
						}
					}
				}).start();
			} else {}
				JOptionPane.showMessageDialog(null, "IP LISTEN OK");
		}
	}
	
	public void processRequest() {
		device.setOnPayAtTableRequest(new IPayAtTableRequestInterface() {
			@Override
			public void onPayAtTableRequest(PATRequest request) {
				Display.getDefault().asyncExec(new Runnable() {
					@Override
					public void run() {
						if (request != null) {
													
							String IPAddress = "";
							int patSize = patRequestList.size();
							int i = 0;
							
							groupRawPAT.setVisible(true);
							showPatTransactions();
							_rawPATtMessage = "";
							
							if (request.getSocket().isConnected()) {
								SocketAddress socketAddress = request.getSocket().getRemoteSocketAddress();
								InetAddress inetAddress = ((InetSocketAddress)socketAddress).getAddress();
								IPAddress = inetAddress.toString().split("/")[1];																						
							}
													
							patSize = patRequestList.size();
							lastCommand = "";
							currentSocket = null;
							for (i = 0 ; i < patRequestList.size() ; i++) {
								_socket = patRequestList.get(i).getSocket();						
								if (request.getSocket().equals(_socket)) {
									listAddress.setItem(i, IPAddress);
									listCommand.setItem(i, request.getRequestType().toString());								
									patRequestList.set(i, request);		
									lastCommand = request.getRequestType().toString();
									if (!ongoing) {
										currentSocket = patRequestList.get(0).getSocket();
										payAtTableResponse(patRequestList.get(0));										
									}

									return;
									
								} else {
									if (device.socketSize() > patRequestList.size()) {
										patRequestList.add(request);
										listAddress.add(IPAddress);
										listCommand.add(request.getRequestType().toString());
										Display.getDefault().asyncExec(new Runnable() {
											@Override
											public void run() {
												_rawPATtMessage += request.getRawData();
												txtRawMessagesPAT.append(_rawPATtMessage);	
											}
										});
									
										return;
									}	
									return;
								}
								
							}		
							
							patRequestList.add(request);
							listAddress.add(IPAddress);
							listCommand.add(request.getRequestType().toString());	
							currentSocket = patRequestList.get(0).getSocket();
							if (!ongoing) {
								payAtTableResponse(patRequestList.get(0));
							}
							
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									_rawPATtMessage += request.getRawData();
									txtRawMessagesPAT.append(_rawPATtMessage);	
									txtRawMessagesPAT.append("\n");	
								}
							});
							
						}
					}
				});			
			}
		});
	}
	
	public String getFileLocation() {
		return fileLocation;
	}
	
	public String setFileLocation(String filelocation) {
		return fileLocation = filelocation;
	}
	
	private void validate() throws Exception {	
		if (txtAmount.getText().isEmpty()) {
			throw new Exception("Amount cannot be empty.");
		} else if (txtAmount.getText().equals(".")) {
			throw new Exception("Amount can have decimal value but cannot be sent as decimal only.");
		}
		
		BigDecimal amount = new BigDecimal(txtAmount.getText());
		if (amount.compareTo(BigDecimal.ZERO) <= 0) {
			throw new Exception("Amount must be greater than zero.");
		} else if (amount.compareTo(new BigDecimal("1000000")) >= 0) {
			throw new Exception("Amount cannot be exceeded from 999999.");
		}
	}
	
	public void clickListeners(ConfirmationResponse conf) {
		btnTableLockAccept.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {	
				System.out.println("AFTER:");
				try {									
					if (txtAmount.getText().equals("")) {
						JOptionPane.showMessageDialog(null, "Amount should not be empty.");											
					} else {
						reqAmount = new BigDecimal(txtAmount.getText());
						conf.setAmount(reqAmount);
						conf.setConfirmation(true);
						patConfirmation(conf);
						hidePatTransactions();
						hideAllPatTransaction();									
					}				
				} catch (Exception e1) {
					e1.printStackTrace();
					
				}															
			}							
		});
		
		btnTableLockDecline.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (txtAmount.getText().equals("")) {
					JOptionPane.showMessageDialog(null, "Amount should not be empty.");
				} else {
				reqAmount = new BigDecimal(txtAmount.getText());
				conf.setAmount(reqAmount);
				conf.setConfirmation(false);
					
				patConfirmation(conf);
				hidePatTransactions();
				hideAllPatTransaction();	
				}	
			}
		});
		
		btnTransactionOutcome.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {							
				TransactionWindow transWindow = new TransactionWindow(null, transOut);
				transWindow.open();				
			}
		});
	}
	
	public void updateConnectionStatus() {	
		while (device != null) {
			if (!previousDeviceCount.equals(device.socketSize())) {
				previousDeviceCount = device.socketSize();
				Display.getDefault().asyncExec(new Runnable() {
					@Override
					public void run() {					
						try {
						
							int j = 0;
							Socket removedSocket = null;
							if (isConnected == true) {
								Thread.sleep(1000);	
								
								if (device.socketSize().equals(0)) {
									lblConnectedDevices.setText("0");
									test.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
									test.setText("CONNECTING...");																
								} else {
									lblConnectedDevices.setText(device.socketSize().toString());
									test.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
									test.setText("CONNECTED");
								}							
								for (j = 0 ; j < patRequestList.size() ; j++) {
//									Thread.sleep(1000);
									removedSocket = patRequestList.get(j).getSocket();
									if (removedSocket.isClosed()) {
										if (patRequestList.get(0).getRequestType() == PATRequestType.TABLE_UNLOCK) {
											Display.getDefault().asyncExec(new Runnable() {
												@Override
												public void run() {														
													MessageDialog.openInformation(display.getActiveShell(), "Confirm", "Table unlock request was sent by the terminal -- simulator and the terminal is now in idle mode");													
													ongoing = false;
												}
											});
										}
										
										if (patRequestList.size() == 1) {
											hideAllPatTransaction();
											txtRawMessagesPAT.setText("");
											ongoing = false;
										}
										
										if (removedSocket.equals(patRequestList.get(0).getSocket())) {
											setFileLocation("");
											ongoing = false;
										}
										if (patRequestList.size() > 1) {										
											if (removedSocket == patRequestList.get(0).getSocket()) {											
												payAtTableResponse(patRequestList.get(1));										
										}
											
										} 
										listAddress.remove(j);
										listCommand.remove(j);
										patRequestList.remove(j);	
										
										waiterID = "";
										tableID = "";
										terminalID = "";
										terminalCurrency = "";
										
										System.out.println("ONGOING?:" + ongoing);
//										if (!patRequestList.isEmpty()) {
//											if (!ongoing) {
//												payAtTableResponse(patRequestList.get(0));
//											}
//										}
									}	
								}												
							} else {
								test.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
								test.setText("CONNECTING...");
								
								lblConnectedDevices.setText("0");
								hidePatTransactions();
								hideAllPatTransaction();
								
							}
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				});
			}
		}
	}
}
