package TestApp01_call;

import java.math.BigDecimal;

import javax.swing.JOptionPane;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

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.Button;
import org.eclipse.swt.widgets.Composite;
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 TestApp01_call.reqMsg;

public class popUp04 extends Shell {
	private Text text;
	private String transCode = "";
	protected static Shell shell = null;
	protected static Composite composite;
	private static popUp04 popUp = null;
	protected String refNumber;
	protected String amount;
	protected testapp01_call mainForm;
	private reqMsg req;
	protected IDeviceResponse dResponse;
	protected IDeviceInterface device;
	private transactionInProgress inProgress;

	public void setupDevice(IDeviceResponse dResponse, IDeviceInterface device, testapp01_call mainForm) {
		this.dResponse = dResponse;
		this.device = device;
		this.mainForm = mainForm;
	}

	public void setReferenceNumber(String refNumber) {
		this.refNumber = refNumber;
	}

	public void setAmount(String amount) {
		this.amount = amount;
	}
	
	public String getTransCode() {
		return transCode;
	}

	private static synchronized popUp04 getInstance() {
		try {
			if (popUp == null)
				popUp = (popUp04) Class.forName("popUp04").newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return popUp;
	}

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

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public popUp04(Display display) {
		super(display, SWT.DIALOG_TRIM);

		text = new Text(this, SWT.BORDER | SWT.CENTER);
		text.setBounds(40, 37, 130, 26);
		text.setTextLimit(4);

		Label lblTransactionId = new Label(this, SWT.NONE);
		lblTransactionId.setBounds(22, 16, 81, 15);
		lblTransactionId.setText("Transaction Id:");

		text.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;
					}
				}
			}
		});
		createContents();
	}

	/**
	 * Create contents of the shell.
	 * 
	 * @param _popsy05
	 */
	protected void createContents() {
		setText("");
		setSize(205, 139);

		Button btnOk = new Button(this, SWT.NONE);
		btnOk.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (text.getText() != "") {
					try {
						transCode = text.getText();
						req = new reqMsg(device, null);
						req.set_ePosNumText(refNumber);
						req.set_reqsAmntText(amount);
						req.setTransId(transCode);
						close();
						
						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 = req.reverse(0, dResponse, transCode);
								
								Display.getDefault().asyncExec(new Runnable() {
									@Override
									public void run() {
										inProgress.close();
										if (dResponse != null) {
											req.displayResponse(mainForm, ((ITerminalResponse) dResponse), device);
										}
									}
								});
							}
						}).start();
					} catch (NumberFormatException | ApiException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} else
					JOptionPane.showMessageDialog(null, "TRANSACTION NUMBER CAN'T BE EMPTY", " ",
							JOptionPane.INFORMATION_MESSAGE, null);
			}
		});
		btnOk.setBounds(73, 70, 74, 30);
		btnOk.setText("OK");
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
	private void displayInProgress() {
		transactionInProgress inProgress = new transactionInProgress(null);
		inProgress.setDevice(device, dResponse, mainForm);
		inProgress.setReferenceNumber(refNumber);
		inProgress.setAmount(amount);
		inProgress.open();
		this.inProgress = inProgress.OK();
	}
}
