package TestApp01_call;

import javax.swing.JOptionPane;

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

import com.global.api.entities.exceptions.ApiException;
import com.global.api.terminals.abstractions.IDeviceInterface;
import com.global.api.terminals.abstractions.IDeviceResponse;
import com.global.api.terminals.abstractions.ITerminalResponse;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;

public class transactionInProgress extends Shell {
	protected IDeviceInterface device;
	protected IDeviceResponse response;
	protected testapp01_call mainForm;
	protected String refNumber;
	protected String amount;
	protected reqMsg req;
	protected static transactionInProgress shell = null;
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			shell = new transactionInProgress(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * @param display
	 */
	public transactionInProgress(Display display) {
		super(display, SWT.CLOSE);
	    
		Label lblTransactionInProgress = new Label(this, SWT.NONE);
		lblTransactionInProgress.setFont(SWTResourceManager.getFont("Segoe UI Semibold", 13, SWT.NORMAL));
		lblTransactionInProgress.setBounds(47, 21, 232, 28);
		lblTransactionInProgress.setText("TRANSACTION IN PROGRESS");
		
		Button btnCancel = new Button(this, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					req = new reqMsg(device, null);
					req.set_ePosNumText(refNumber);
					req.set_reqsAmntText(amount);
					
					new Thread(new Runnable() {
						@Override
						public void run() {
							response = req.cancel(response);
							
							if (response != null) {
								Display.getDefault().asyncExec(new Runnable() {
									@Override
									public void run() {
										req.displayResponse(mainForm, ((ITerminalResponse)response), device);
									}
								});
							}
						}
					}).start();
					
					//close();
				} catch (NumberFormatException | ApiException e1) {
					e1.printStackTrace();
				}
			}
		});
		btnCancel.setBounds(103, 55, 117, 39);
		btnCancel.setText("CANCEL");
		createContents(display);
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents(Display display) {
//		setText("SWT Application");
		setSize(330, 149);
		setLocation(800, 420);
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
	public void setDevice(IDeviceInterface device, IDeviceResponse response, testapp01_call mainForm) {
		this.device = device;
		this.response = response;
		this.mainForm = mainForm;
	}
	
	public void setReferenceNumber(String refNumber) {
		this.refNumber = refNumber;
	}
	
	public void setAmount(String amount) {
		this.amount = amount;
	}
	
	public transactionInProgress OK() {
		return this;
	}
}
