package TestApp01_call;

import javax.swing.JOptionPane;

import com.global.api.entities.enums.ConnectionModes;
import com.global.api.entities.enums.DeviceType;
import com.global.api.entities.exceptions.ApiException;
import com.global.api.services.DeviceService;
import com.global.api.terminals.ConnectionConfig;
import com.global.api.terminals.abstractions.IDeviceInterface;

public class tcpIP {

	public tcpIP() {

	}

	String _ipPortText;
	String timeOut;

	public String get_ipPortText() {
		return _ipPortText;
	}

	public void set_ipPortText(String _ipPortText) {
		this._ipPortText = _ipPortText;
	}
	
	public void setTimeout(String timeOut) {
		this.timeOut = timeOut;
	}

	public IDeviceInterface startLstn(IDeviceInterface device, boolean isPayAtTable) throws Exception {
		try {
			if (device != null) {
				throw new Exception("Port already opened");
			}
			timeOut = (timeOut == "" ? "65000" : timeOut);
			if (isPayAtTable) {
				timeOut = "0";
			}
			ConnectionConfig connectionConfig = new ConnectionConfig();
			connectionConfig.setDeviceType(DeviceType.INGENICO_L3000);
			connectionConfig.setConnectionMode(isPayAtTable ? ConnectionModes.PAY_AT_TABLE : ConnectionModes.TCP_IP_SERVER);
			connectionConfig.setPort(get_ipPortText());
			connectionConfig.setTimeout(Integer.parseInt(timeOut));
			
			return device = DeviceService.create(connectionConfig);
		} catch (ApiException e) {
			e.printStackTrace();
		}

		return device;
	}

	public IDeviceInterface stopLstn(IDeviceInterface device) {
		if (device != null) {
			
			device.dispose();
			device = null;
			JOptionPane.showMessageDialog(null, "IP LISTEN CLOSE");
		}

		return device;
	}

	public void dspBrodMsg() {
		return;
	}

	public void dspBrodMsgoff() {

		return;
	}

	public void payTable() {

		return;
	}

	public void payTableoff() {

		return;
	}
}
