package TestApp01_call;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.global.api.terminals.ingenico.pat.TransactionOutcome;
import com.global.api.terminals.ingenico.responses.DataResponse;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class TransactionWindow extends Shell {
	private Text txtEpos;
	private Text txtTransStatus;
	private Text txtAmount;
	private Text txtPayment;
	private Text txtResponseData;
	private Text txtCurrency;
	private Text txtPrivateData;
	private Text txtRawResponse;
	private Text txtAuthCode;
	private Text txtCashback;
	private Text txtTxnAmount;
	private Text txtGratuity;
	private Text txtAvailableAmount;
	private Text txtPaymentMethod;
	private Text txtSubType;
	private Text txtSplitSaleAmount;
	private Text txtDCCAmount;
	private Text txtDCCCurrency;
	private Text txtDCCStatus;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			TransactionWindow shell = new TransactionWindow(display, null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * @param display
	 */
	public TransactionWindow(Display display, TransactionOutcome transOut) {
		super(display, SWT.SHELL_TRIM);
		
		Composite composite = new Composite(this, SWT.BORDER);
		composite.setBounds(10, 10, 384, 502);
		
		Label lblEposNumber = new Label(composite, SWT.NONE);
		lblEposNumber.setAlignment(SWT.RIGHT);
		lblEposNumber.setBounds(10, 13, 139, 20);
		lblEposNumber.setText("EPoS Number:");
		
		txtEpos = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		txtEpos.setEnabled(false);
		txtEpos.setBounds(155, 10, 215, 26);
		
		Label lblTransactionStatus = new Label(composite, SWT.NONE);
		lblTransactionStatus.setAlignment(SWT.RIGHT);
		lblTransactionStatus.setText("Transaction Status:");
		lblTransactionStatus.setBounds(10, 49, 139, 20);
		
		txtTransStatus = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		txtTransStatus.setEnabled(false);
		txtTransStatus.setBounds(155, 46, 215, 26);
		
		Label lblAmount = new Label(composite, SWT.NONE);
		lblAmount.setText("Amount:");
		lblAmount.setAlignment(SWT.RIGHT);
		lblAmount.setBounds(10, 81, 139, 20);
		
		txtAmount = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		txtAmount.setEnabled(false);
		txtAmount.setBounds(155, 78, 215, 26);
		
		Label lblPayment = new Label(composite, SWT.NONE);
		lblPayment.setText("Payment:");
		lblPayment.setAlignment(SWT.RIGHT);
		lblPayment.setBounds(10, 114, 139, 20);
		
		txtPayment = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		txtPayment.setEnabled(false);
		txtPayment.setBounds(155, 111, 215, 26);
		
		Label lblResponseWindow = new Label(composite, SWT.NONE);
		lblResponseWindow.setText("Response Data:");
		lblResponseWindow.setAlignment(SWT.RIGHT);
		lblResponseWindow.setBounds(10, 146, 139, 20);
		
		txtResponseData = new Text(composite, SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL);
		txtResponseData.setEnabled(false);
		txtResponseData.setBounds(155, 143, 215, 139);
		
		Label lblCurrency = new Label(composite, SWT.NONE);
		lblCurrency.setText("Currency:");
		lblCurrency.setAlignment(SWT.RIGHT);
		lblCurrency.setBounds(10, 291, 139, 20);
		
		txtCurrency = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		txtCurrency.setEnabled(false);
		txtCurrency.setBounds(155, 288, 215, 26);
		
		Label lblPrivateData = new Label(composite, SWT.NONE);
		lblPrivateData.setText("Private Data:");
		lblPrivateData.setAlignment(SWT.RIGHT);
		lblPrivateData.setBounds(10, 323, 139, 20);
		
		txtPrivateData = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		txtPrivateData.setEnabled(false);
		txtPrivateData.setBounds(155, 320, 215, 26);
		
		Label lblRawResponse = new Label(composite, SWT.NONE);
		lblRawResponse.setText("Raw Response:");
		lblRawResponse.setAlignment(SWT.RIGHT);
		lblRawResponse.setBounds(10, 355, 139, 20);
		
		txtRawResponse = new Text(composite, SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL);
		txtRawResponse.setEnabled(false);
		txtRawResponse.setBounds(155, 352, 215, 139);
		
		Composite composite_1 = new Composite(this, SWT.BORDER);
		composite_1.setBounds(400, 10, 384, 502);
		
		Label lblRepFields = new Label(composite_1, SWT.NONE);
		lblRepFields.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.BOLD));
		lblRepFields.setAlignment(SWT.CENTER);
		lblRepFields.setBounds(10, 0, 359, 26);
		lblRepFields.setText("REP Fields");
		
		Label lblAuthorizationCode = new Label(composite_1, SWT.NONE);
		lblAuthorizationCode.setText("Authorization Code:");
		lblAuthorizationCode.setAlignment(SWT.RIGHT);
		lblAuthorizationCode.setBounds(10, 35, 139, 20);
		
		txtAuthCode = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY);
		txtAuthCode.setEnabled(false);
		txtAuthCode.setBounds(155, 32, 215, 26);
		
		Label lblAuthorizationCode_1 = new Label(composite_1, SWT.NONE);
		lblAuthorizationCode_1.setText("Cashback Amount:");
		lblAuthorizationCode_1.setAlignment(SWT.RIGHT);
		lblAuthorizationCode_1.setBounds(10, 77, 139, 20);
		
		txtCashback = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY);
		txtCashback.setEnabled(false);
		txtCashback.setBounds(155, 74, 215, 26);
		
		Label lblGratuityAmount = new Label(composite_1, SWT.NONE);
		lblGratuityAmount.setText("Gratuity Amount:");
		lblGratuityAmount.setAlignment(SWT.RIGHT);
		lblGratuityAmount.setBounds(10, 123, 139, 20);
		
		Label lblAuthorizationCode_1_1 = new Label(composite_1, SWT.NONE);
		lblAuthorizationCode_1_1.setText("Final Txn Amount:");
		lblAuthorizationCode_1_1.setAlignment(SWT.RIGHT);
		lblAuthorizationCode_1_1.setBounds(10, 166, 139, 20);
		
		txtTxnAmount = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY);
		txtTxnAmount.setEnabled(false);
		txtTxnAmount.setBounds(155, 163, 215, 26);
		
		txtGratuity = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY);
		txtGratuity.setEnabled(false);
		txtGratuity.setBounds(155, 120, 215, 26);
		
		Label lblAvailableAmount = new Label(composite_1, SWT.NONE);
		lblAvailableAmount.setText("Available Amount:");
		lblAvailableAmount.setAlignment(SWT.RIGHT);
		lblAvailableAmount.setBounds(10, 208, 139, 20);
		
		txtAvailableAmount = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY);
		txtAvailableAmount.setEnabled(false);
		txtAvailableAmount.setBounds(155, 205, 215, 26);
		
		Label lblAuthorizationCode_1_2 = new Label(composite_1, SWT.NONE);
		lblAuthorizationCode_1_2.setText("Payment Method:");
		lblAuthorizationCode_1_2.setAlignment(SWT.RIGHT);
		lblAuthorizationCode_1_2.setBounds(10, 253, 139, 20);
		
		txtPaymentMethod = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY);
		txtPaymentMethod.setEnabled(false);
		txtPaymentMethod.setBounds(155, 250, 215, 26);
		
		Label lblAuthorizationCode_2_1 = new Label(composite_1, SWT.NONE);
		lblAuthorizationCode_2_1.setText("Txn Sub-Type:");
		lblAuthorizationCode_2_1.setAlignment(SWT.RIGHT);
		lblAuthorizationCode_2_1.setBounds(10, 297, 139, 20);
		
		txtSubType = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY);
		txtSubType.setEnabled(false);
		txtSubType.setBounds(155, 294, 215, 26);
		
		Label lblAuthorizationCode_1_1_1 = new Label(composite_1, SWT.NONE);
		lblAuthorizationCode_1_1_1.setText("Split Sale Amount:");
		lblAuthorizationCode_1_1_1.setAlignment(SWT.RIGHT);
		lblAuthorizationCode_1_1_1.setBounds(9, 340, 139, 20);
		
		txtSplitSaleAmount = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY);
		txtSplitSaleAmount.setEnabled(false);
		txtSplitSaleAmount.setBounds(154, 337, 215, 26);
		
		Label lblDccAmount = new Label(composite_1, SWT.NONE);
		lblDccAmount.setText("DCC Amount:");
		lblDccAmount.setAlignment(SWT.RIGHT);
		lblDccAmount.setBounds(9, 383, 139, 20);
		
		txtDCCAmount = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY);
		txtDCCAmount.setEnabled(false);
		txtDCCAmount.setBounds(154, 380, 215, 26);
		
		Label lblAuthorizationCode_1_3 = new Label(composite_1, SWT.NONE);
		lblAuthorizationCode_1_3.setText("DCC Currency:");
		lblAuthorizationCode_1_3.setAlignment(SWT.RIGHT);
		lblAuthorizationCode_1_3.setBounds(10, 425, 139, 20);
		
		txtDCCCurrency = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY);
		txtDCCCurrency.setEnabled(false);
		txtDCCCurrency.setBounds(155, 422, 215, 26);
		
		Label lblAuthorizationCode_2_2 = new Label(composite_1, SWT.NONE);
		lblAuthorizationCode_2_2.setText("DCC Status:");
		lblAuthorizationCode_2_2.setAlignment(SWT.RIGHT);
		lblAuthorizationCode_2_2.setBounds(9, 465, 139, 20);
		
		txtDCCStatus = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY);
		txtDCCStatus.setEnabled(false);
		txtDCCStatus.setBounds(154, 462, 215, 26);
		createContents();
		showData(transOut);
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("Transaction Outcome");
		setSize(810, 558);

	}
	
	public void showData(TransactionOutcome transOut) {
		Display.getDefault().asyncExec(new Runnable() {
			@Override
			public void run() {
				txtEpos.setText("00");
				txtTransStatus.setText(transOut.getTransactionStatus().toString());
				txtAmount.setText(transOut.getAmount());
				txtPayment.setText("0");
				txtCurrency.setText(transOut.getCurrencyCode());
				txtPrivateData.setText(transOut.getPrivateData());
				txtResponseData.setText(transOut.toString());
				
				// response field
				DataResponse dataResponse = transOut.getRepFields();
				txtAuthCode.setText(dataResponse.getAuthorizationCode());
				txtCashback.setText(dataResponse.getCashbackAmount() != null
						? dataResponse.getCashbackAmount().toString()
						: "");
				txtGratuity.setText(dataResponse.getGratuityAmount() != null
						? dataResponse.getGratuityAmount().toString()
						: "");
				txtTxnAmount.setText(dataResponse.getFinalAmount() != null
						? dataResponse.getFinalAmount().toString()
						: "");
				txtAvailableAmount.setText(dataResponse.getAvailableAmount() != null
						? dataResponse.getAvailableAmount().toString()
						: "");
				txtPaymentMethod.setText(dataResponse.getPaymentMethod() != null
						? dataResponse.getPaymentMethod().toString()
						: "");
				txtSubType.setText(dataResponse.getTransactionSubType() != null
						? dataResponse.getTransactionSubType().toString()
						: "");
				txtSplitSaleAmount.setText(dataResponse.getSplitSaleAmount() != null
						? dataResponse.getSplitSaleAmount().toString()
						: "");
				txtDCCAmount.setText(dataResponse.getDccAmount() != null
						? dataResponse.getDccAmount().toString()
						: "");
				txtDCCCurrency.setText(
						dataResponse.getDccCode() != null ? dataResponse.getDccCode() : "");
				txtDCCStatus.setText(dataResponse.getDccStatus() != null
						? dataResponse.getDccStatus().toString()
						: "");
			}
		});
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
