package TestApp01_call;

import java.math.BigDecimal;

import javax.swing.JOptionPane;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;

public class ConfirmationResponse extends Shell {
	private Text txtRaw;
	private Text txtAmount;
	private Text txtCurrency;
	
	private String labelTitle = "";
	private String labelMsg = "";
	private String waiterId = "";
	private String tableId = "";
	private String terminalId = "";
	private String terminalCurrency = "";
	private BigDecimal amount = new BigDecimal("0");
	private String currencyCode = "";
	private String rawData = "";
	private String transactionOutcomeStatus = "";
	
	private boolean isAdditionalMsg;
	private boolean isConfirmed;
	private boolean hasDecimal;
	
	private Label lblTitle;
	private Label lblMessage;
	private Button checkAdditional;
	private Label lblWaiterIdVal;
	private Label lblTableIdVal;
	private Label lblTIDVal;
	private Label lblTerminalCurrencyVal;
	private Label lblStatusVal;
	

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

	/**
	 * Create the shell.
	 * @param display
	 */
	public ConfirmationResponse(Display display) {
		super(display, SWT.SHELL_TRIM);
		
		lblTitle = new Label(this, SWT.NONE);
		lblTitle.setBounds(93, 22, 404, 15);
		lblTitle.setText(labelTitle);
		
		Label lblWaiterId = new Label(this, SWT.NONE);
		lblWaiterId.setText("Waiter Id:");
		lblWaiterId.setBounds(57, 95, 55, 15);
		
		Label lblTableId = new Label(this, SWT.NONE);
		lblTableId.setText("Table Id:");
		lblTableId.setBounds(57, 132, 55, 15);
		
		Label lblTerminalId = new Label(this, SWT.NONE);
		lblTerminalId.setText("Terminal Id:");
		lblTerminalId.setBounds(266, 95, 67, 15);
		
		Label lblTerminalCurrency = new Label(this, SWT.NONE);
		lblTerminalCurrency.setText("Terminal Currency:");
		lblTerminalCurrency.setBounds(266, 132, 111, 15);
		
		Label lblAmount = new Label(this, SWT.NONE);
		lblAmount.setText("Amount:");
		lblAmount.setBounds(93, 206, 55, 15);
		
		Label lblCurrency = new Label(this, SWT.NONE);
		lblCurrency.setText("Currency:");
		lblCurrency.setBounds(93, 238, 55, 15);
		
		Label lblTransactionOutcome = new Label(this, SWT.NONE);
		lblTransactionOutcome.setText("Transaction Outcome:");
		lblTransactionOutcome.setBounds(93, 353, 166, 15);
		
		Label lblStatus = new Label(this, SWT.NONE);
		lblStatus.setText("Status:");
		lblStatus.setBounds(93, 384, 55, 15);
		
		lblMessage = new Label(this, SWT.NONE);
		lblMessage.setText(labelMsg);
		lblMessage.setBounds(93, 53, 404, 15);
		
		Button btnDecline = new Button(this, SWT.NONE);
//		btnDecline.addSelectionListener(new SelectionAdapter() {
//			@Override
//			public void widgetSelected(SelectionEvent e) {
//				try {
//					isConfirmed = false;
//					validate();
//					amount = new BigDecimal(txtAmount.getText());
//					close();	
//				} catch (Exception ex) {
//					JOptionPane.showMessageDialog(null, ex.getMessage());
//				}
//			}
//		});
		btnDecline.setText("DECLINE");
		btnDecline.setBounds(392, 419, 186, 69);
		
		Button btnAccept = new Button(this, SWT.NONE);
//		btnAccept.addSelectionListener(new SelectionAdapter() {
//			@Override
//			public void widgetSelected(SelectionEvent e) {
//				try {
//					validate();
//					isConfirmed = true;
//					amount = new BigDecimal(txtAmount.getText());
//					close();	
//				} catch (Exception ex) {
//					JOptionPane.showMessageDialog(null, ex.getMessage());
//				}
//			}
//		});
		btnAccept.setText("ACCEPT");
		btnAccept.setBounds(164, 419, 186, 69);
		
		txtRaw = new Text(this, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		txtRaw.setText("");
		txtRaw.setBounds(489, 92, 231, 312);
		
		txtAmount = new Text(this, SWT.BORDER);
		txtAmount.setText("6.18");
		txtAmount.setBounds(154, 203, 166, 21);
		txtAmount.addVerifyListener(new VerifyListener() {

			@Override
			public void verifyText(VerifyEvent e) {
				String string = e.text;
				
				if (string.isEmpty()) {
					hasDecimal = false;
				}
				
				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')) {
						if (chars[i] == '.' && !hasDecimal) {
							hasDecimal = true;
							return;
						}
						
						e.doit = false;
						return;
					}
				}
			}
		});
		
		txtCurrency = new Text(this, SWT.BORDER);
		txtCurrency.setText("826");
		txtCurrency.setBounds(154, 235, 166, 21);
		txtCurrency.setEnabled(false);
		
		checkAdditional = new Button(this, SWT.CHECK);
		checkAdditional.setBounds(93, 268, 178, 16);
		checkAdditional.setText("Additional Message");
		
		lblWaiterIdVal = new Label(this, SWT.NONE);
		lblWaiterIdVal.setText("");
		lblWaiterIdVal.setBounds(118, 95, 142, 15);
		
		lblTableIdVal = new Label(this, SWT.NONE);
		lblTableIdVal.setText("");
		lblTableIdVal.setBounds(118, 132, 142, 15);
		
		lblTIDVal = new Label(this, SWT.NONE);
		lblTIDVal.setText("");
		lblTIDVal.setBounds(339, 95, 142, 15);
		
		lblTerminalCurrencyVal = new Label(this, SWT.NONE);
		lblTerminalCurrencyVal.setText("");
		lblTerminalCurrencyVal.setBounds(383, 132, 100, 15);
		
		lblStatusVal = new Label(this, SWT.NONE);
		lblStatusVal.setText("");
		lblStatusVal.setBounds(154, 384, 142, 15);
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setSize(746, 551);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
	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 setConfirmation(Boolean isConfirmation) {
		isConfirmed = isConfirmation;
	}
	
	public void setTitle(String title) {
		lblTitle.setText(title);
	}
	
	public void setMessage(String message) {
		lblMessage.setText(message);
	}
	
	public void setWaiterId(String waiterId) {
		waiterId = waiterId == null ? "" : waiterId;
		lblWaiterIdVal.setText(waiterId);
	}
	
	public void setTableId(String tableId) {
		tableId = tableId == null ? "" : tableId;
		lblTableIdVal.setText(tableId);
	}
	
	public void setRawData(String raw) {
		raw = raw == null ? "" : raw;
		txtRaw.setText(raw);
	}
	
	public void setTerminalId(String terminalId) {
		terminalId = terminalId == null ? "" : terminalId;
		lblTIDVal.setText(terminalId);
	}
	
	public void setTerminalCurrency(String terminalCurrency) {
		terminalCurrency = terminalCurrency == null ? "" : terminalCurrency;
		lblTerminalCurrencyVal.setText(terminalCurrency);
	}
	
	public BigDecimal getAmount() {
		return amount;
	}
	
	public String getCurrency() {
		return currencyCode;
	}
	
	public String getRawData() {
		return rawData;
	}
	
	public void setTransactionOutcomeStatus(String status) {
		status = status == null ? "" : status;
		lblStatusVal.setText(status);
	}
	
	public boolean getConfirmation() {
		return isConfirmed;
	}
	
	public boolean getUseAdditionalMsg() {
		return checkAdditional.getSelection();
	}
	
	public void displayAdditionalMsg(boolean val) {
		checkAdditional.setVisible(val);
	}
	
	public void enableAdditionalMsg(boolean val) {
		checkAdditional.setEnabled(val);
	}
	
	public void checkAdditionalMsg(boolean val) {
		checkAdditional.setSelection(val);
	}
	
	public void setAmount(BigDecimal amount) {
		String _amount = amount.toString();
		_amount = _amount == null ? new BigDecimal("6.18").toString() : _amount;
//		txtAmount.setText(_amount.toString());
//		txtAmount.setEnabled(false);
	}
}
