package TestApp01_call;

import java.math.BigDecimal;

import javax.swing.JOptionPane;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;

import com.global.api.entities.exceptions.ApiException;
import com.global.api.terminals.ConnectionConfig;
import com.global.api.terminals.DeviceController;
import com.global.api.terminals.DeviceMessage;
import com.global.api.terminals.abstractions.IDeviceCommInterface;
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.IngenicoInterface;
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.PaymentMode;
import com.global.api.terminals.ingenico.variables.ReceiptType;
import com.global.api.terminals.ingenico.variables.ReportTypes;
import com.global.api.terminals.ingenico.variables.TaxFreeType;
import com.global.api.terminals.messaging.IMessageReceivedInterface;
import com.global.api.terminals.messaging.IMessageSentInterface;

public class reqMsg extends testapp01_call {
	String _ePosNumText;
	String _reqsAmntText;
	String _returnREPText;
	String _paymentModeText;
	int _paymentTypeText;
	String _currencyText;
	String _privDataText;
	String _immediateAnsText;
	String _forOnlineText;
	String _extendedValueText;
	String _rawReqText;
	String tableNumber;
	String cashBackAmount;
	String authCode = "";
	String transId;
	boolean isAutoTicketEnabled;
	IDeviceInterface _device;
	DeviceMessage _message;
	ConnectionConfig _config;
	IDeviceResponse dResponse;
	Exception exception;
	//region Getter and Setter
	public String getTransId() {
		return transId;
	}

	public void setTransId(String transId) {
		this.transId = transId;
	}

	public String getAuthCode() {
		return authCode;
	}

	public void setAuthCode(String authCode) {
		this.authCode = authCode;
	}

	public String getTableNumber() {
		return tableNumber;
	}

	public void setTableNumber(String tableNumber) {
		this.tableNumber = tableNumber;
	}

	public String getCashbackAmount() {
		return cashBackAmount;
	}

	public void setCashbackAmount(String cashBackAmount) {
		this.cashBackAmount = cashBackAmount;
	}

	public String get_ePosNumText() {
		return _ePosNumText;
	}

	public void set_ePosNumText(String _ePosNumText) {
		this._ePosNumText = _ePosNumText;
	}

	public String get_reqsAmntText() {
		return _reqsAmntText;
	}

	public void set_reqsAmntText(String _reqsAmntText) {
		this._reqsAmntText = _reqsAmntText;
	}

	public String get_returnREPText() {
		return _returnREPText;
	}

	public void set_returnREPText(String _returnREPText) {
		this._returnREPText = _returnREPText;
	}

	public String get_paymentModeText() {
		return _paymentModeText;
	}

	public void set_paymentTypeText(int _paymentTypeText) {
		this._paymentTypeText = _paymentTypeText;
	}

	public int get_paymentTypeText() {
		return _paymentTypeText;
	}

	public void set_paymentModeText(String _paymentModeText) {
		this._paymentModeText = _paymentModeText;
	}

	public String get_currencyText() {
		return _currencyText;
	}

	public void set_currencyText(String _currencyText) {
		this._currencyText = _currencyText;
	}

	public String get_privDataText() {
		return _privDataText;
	}

	public void set_privDataText(String _privDataText) {
		this._privDataText = _privDataText;
	}

	public String get_immediateAnsText() {
		return _immediateAnsText;
	}

	public void set_immediateAnsText(String _immediateAnsText) {
		this._immediateAnsText = _immediateAnsText;
	}

	public String get_forOnlineText() {
		return _forOnlineText;
	}

	public void set_forOnlineText(String _forOnlineText) {
		this._forOnlineText = _forOnlineText;
	}

	public String get_extendedValueText() {
		return _extendedValueText;
	}

	public void set_extendedValueText(String _extendedValueText) {
		this._extendedValueText = _extendedValueText;
	}

	public String get_rawReqText() {
		return _rawReqText;
	}

	public void set_rawReqText(String _rawReqText) {
		this._rawReqText = _rawReqText;
	}

	public Boolean getIsAutoTicketEnabled() {
		return isAutoTicketEnabled;
	}

	public void set_AutoTicketEnabled(Boolean autoTicketEnabled) {
		this.isAutoTicketEnabled = autoTicketEnabled;		
	}
	//endregion
	public reqMsg(IDeviceInterface device, ConnectionConfig config) throws NumberFormatException, ApiException {
		// Constructor
		this._device = device;
		this._config = config;
		if (_device != null) {
//			sendReqMsg();
		}

	}

	public void setResponse(Text ePosNum) {

	}

	private ITerminalResponse sale(ITerminalResponse response, IDeviceInterface device, boolean isCashback,
			boolean isTableNumber) {
		try {
			if (isCashback)
				response = device.sale(new BigDecimal(_reqsAmntText))
						.withPaymentMode(PaymentMode.getEnumName(Integer.parseInt(_paymentModeText)))
						.withReferenceNumber(Integer.parseInt(_ePosNumText)).withCurrencyCode(_currencyText)
						.withCashBack(new BigDecimal(cashBackAmount))
						.withTicket(isAutoTicketEnabled).execute();
			else if (isTableNumber)
				response = device.sale(new BigDecimal(_reqsAmntText))
						.withPaymentMode(PaymentMode.getEnumName(Integer.parseInt(_paymentModeText)))
						.withReferenceNumber(Integer.parseInt(_ePosNumText)).withCurrencyCode(_currencyText)
						.withTableNumber(tableNumber)
						.withTicket(isAutoTicketEnabled).execute();
			else
				response = device.sale(new BigDecimal(_reqsAmntText))
						.withPaymentMode(PaymentMode.getEnumName(Integer.parseInt(_paymentModeText)))
						.withReferenceNumber(Integer.parseInt(_ePosNumText)).withCurrencyCode(_currencyText)
						.withTicket(isAutoTicketEnabled).execute();
		} catch (Exception e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}

	private ITerminalResponse refund(ITerminalResponse response, IDeviceInterface device, boolean isTableNumber) {
		try {
			if (isTableNumber)
				response = device.refund(new BigDecimal(_reqsAmntText))
						.withPaymentMode(PaymentMode.getEnumName(Integer.parseInt(_paymentModeText)))
						.withReferenceNumber(Integer.parseInt(_ePosNumText)).withCurrencyCode(_currencyText)
						.withTableNumber(tableNumber)
						.withTicket(isAutoTicketEnabled).execute();
			else
				response = device.refund(new BigDecimal(_reqsAmntText))
						.withPaymentMode(PaymentMode.getEnumName(Integer.parseInt(_paymentModeText)))
						.withReferenceNumber(Integer.parseInt(_ePosNumText)).withCurrencyCode(_currencyText)
						.withTicket(isAutoTicketEnabled).execute();
		} catch (Exception e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}

	private ITerminalResponse authorize(ITerminalResponse response, IDeviceInterface device, boolean isTableNumber) {
		try {
			if (isTableNumber)
				response = device.authorize(new BigDecimal(_reqsAmntText))
						.withPaymentMode(PaymentMode.getEnumName(Integer.parseInt(_paymentModeText)))
						.withReferenceNumber(Integer.parseInt(_ePosNumText)).withCurrencyCode(_currencyText)
						.withTableNumber(tableNumber)
						.withTicket(isAutoTicketEnabled).execute();
			else
				response = device.authorize(new BigDecimal(_reqsAmntText))
						.withPaymentMode(PaymentMode.getEnumName(Integer.parseInt(_paymentModeText)))
						.withReferenceNumber(Integer.parseInt(_ePosNumText)).withCurrencyCode(_currencyText)
						.withTicket(isAutoTicketEnabled).execute();
		} catch (Exception e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}

	private ITerminalResponse completion(ITerminalResponse response, IDeviceInterface device, boolean isTableNumber) {
		try {
			if (isTableNumber)
				response = device.capture(new BigDecimal(_reqsAmntText))
						.withReferenceNumber(Integer.parseInt(_ePosNumText)).withCurrencyCode(_currencyText)
						.withAuthCode(authCode)
						.withTicket(isAutoTicketEnabled).execute();			
			else
				response = device.capture(new BigDecimal(_reqsAmntText))
						.withReferenceNumber(Integer.parseInt(_ePosNumText)).withCurrencyCode(_currencyText)
						.withAuthCode(authCode)
						.withTicket(isAutoTicketEnabled).execute();
		} catch (Exception e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}

	private ITerminalResponse taxFreeCredit(ITerminalResponse response, IDeviceInterface device,
			boolean isTableNumber) {
		try {
			if (isTableNumber)
				response = device.refund(new BigDecimal(_reqsAmntText))
						.withPaymentMode(PaymentMode.getEnumName(Integer.parseInt(_paymentModeText)))
						.withReferenceNumber(Integer.parseInt(_ePosNumText)).withCurrencyCode(_currencyText)
						.withTaxFree(TaxFreeType.CREDIT).withTableNumber(tableNumber).execute();
			else
				response = device.refund(new BigDecimal(_reqsAmntText))
						.withPaymentMode(PaymentMode.getEnumName(Integer.parseInt(_paymentModeText)))
						.withReferenceNumber(Integer.parseInt(_ePosNumText)).withCurrencyCode(_currencyText)
						.withTaxFree(TaxFreeType.CREDIT).execute();
		} catch (Exception e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}

	private ITerminalResponse taxFreeCash(ITerminalResponse response, IDeviceInterface device, boolean isTableNumber) {
		try {
			if (isTableNumber)
				response = device.refund(new BigDecimal(_reqsAmntText))
						.withPaymentMode(PaymentMode.getEnumName(Integer.parseInt(_paymentModeText)))
						.withReferenceNumber(Integer.parseInt(_ePosNumText)).withCurrencyCode(_currencyText)
						.withTaxFree(TaxFreeType.CASH).withTableNumber(tableNumber).execute();
			else
				response = device.refund(new BigDecimal(_reqsAmntText))
						.withPaymentMode(PaymentMode.getEnumName(Integer.parseInt(_paymentModeText)))
						.withReferenceNumber(Integer.parseInt(_ePosNumText)).withCurrencyCode(_currencyText)
						.withTaxFree(TaxFreeType.CASH).execute();
		} catch (Exception e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}

	private ITerminalResponse verify(ITerminalResponse response, IDeviceInterface device) {
		try {
			response = device.verify().withPaymentMode(PaymentMode.getEnumName(Integer.parseInt(_paymentModeText)))
					.withCustomerCode("826").withReferenceNumber(Integer.parseInt(_ePosNumText))
					.withTicket(isAutoTicketEnabled).execute();
		} catch (Exception e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}
	
	private ITerminalResponse referral(ITerminalResponse response, IDeviceInterface device) {
		try {		
			response = device.referralConfirmation()
					.withCurrencyCode("826")
					.withAuthCode(authCode)
					.withReferenceNumber(Integer.parseInt(_ePosNumText))
					.withTicket(isAutoTicketEnabled)
					.execute();
		} catch (Exception e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}

	public ITerminalResponse sendReqMsg(ITerminalResponse response, IDeviceInterface device, boolean isCashback,
			boolean isTableNumber) {		
		try {
			switch (_paymentTypeText) {
			case 0:
				response = sale(response, device, isCashback, isTableNumber);
				break;
			case 1:
				response = refund(response, device, isTableNumber);
				break;
			case 2:
				response = authorize(response, device, isTableNumber);
				break;
			case 3:
				response = completion(response, device, isTableNumber);
				break;
			case 4:
				response = taxFreeCredit(response, device, isTableNumber);
				break;
			case 5:
				response = taxFreeCash(response, device, isTableNumber);
				break;
			case 6:
				response = verify(response, device);
				break;
			case 7:
				response = referral(response, device);
				break;
			default:
				JOptionPane.showMessageDialog(null, "PLEASE SELECT PAYMENT TYPE");
			}
		} catch (Exception e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}

	public void displayResponse(testapp01_call mainForm, ITerminalResponse response, IDeviceInterface device) {
		mainForm.displayResponse(response, device);
	}

	public IDeviceResponse reverse(int option, IDeviceResponse response, String transCode) {
		try {
			dResponse = response;
			if (option == JOptionPane.YES_OPTION) {
				response = _device.reverse(new BigDecimal(_reqsAmntText))
						.withReferenceNumber(Integer.parseInt(_ePosNumText)).withTransactionId(transCode)
						.withTicket(isAutoTicketEnabled).execute();
			} else
				response = _device.reverse(new BigDecimal(_reqsAmntText))
						.withReferenceNumber(Integer.parseInt(_ePosNumText))
						.withTicket(isAutoTicketEnabled).execute();
		} catch (NumberFormatException | ApiException e) {
			// TODO Auto-generated catch block
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}

	public IDeviceResponse duplicate(ITerminalResponse response) {
		try {
			response = (ITerminalResponse) _device.duplicate(isAutoTicketEnabled);
		} catch (NumberFormatException | ApiException e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}

	public ITerminalReport xmlReport(ITerminalReport response) {
		try {
			response = _device.getLastReceipt(ReceiptType.REPORT).execute();
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}
		return response;
	}

	public ITerminalReport xmlSplitr(ITerminalReport response) {
		try {
			response = _device.getLastReceipt(ReceiptType.SPLITR).execute();
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}
		return response;
	}

	public ITerminalReport xmlTaxfree(ITerminalReport response) {
		try {
			response = _device.getLastReceipt(ReceiptType.TAXFREE).execute();
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}
		return response;
	}

	public ITerminalReport xmlTicket(ITerminalReport response) {
		try {
			response = _device.getLastReceipt(ReceiptType.TICKET).execute();
		} catch (ApiException e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}

	public ITerminalReport reportEOD(ITerminalReport response) {
		try {
			response = _device.getReport(ReportTypes.EOD).execute();
			int i = 0;
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}
		return response;
	}

	public ITerminalReport reportBanking(ITerminalReport response) {
		try {
			response = _device.getReport(ReportTypes.BANKING).execute();
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}
		return response;
	}

	public ITerminalReport reportXBal(ITerminalReport response) {
		try {
			response = _device.getReport(ReportTypes.XBAL).execute();
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}
		return response;
	}

	public ITerminalReport reportZBal(ITerminalReport response) {
		try {
			response = _device.getReport(ReportTypes.ZBAL).execute();
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}
		return response;
	}

	public IDeviceResponse cancel(IDeviceResponse response) {
		try {
			response = _device.cancel();
		} catch (NumberFormatException | ApiException e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}

	public IDeviceResponse callTMS(IDeviceResponse response) {
		try {
			response = _device.getTerminalConfiguration();
		} catch (ApiException e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}

	public IDeviceResponse logOn(IDeviceResponse response) {
		try {
			response = _device.testConnection(isAutoTicketEnabled);
		} catch (ApiException e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}

	public IDeviceResponse reset(IDeviceResponse response) {
		try {
			response = _device.reboot();
			
			
		} catch (ApiException e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}

	public IInitializeResponse posIdentifier(IDeviceResponse response) {
		try {
			response = _device.initialize();
		} catch (ApiException e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return (IInitializeResponse) response;
	}

	public IDeviceResponse state(IDeviceResponse response) {
		try {
			response = _device.getTerminalStatus();
		} catch (ApiException e) {
			if (!e.getMessage().toLowerCase().contains("stopped"))
				JOptionPane.showMessageDialog(null, e.getMessage());
		}

		return response;
	}
}
