package imseProc.arduinoComm.swt;

import java.util.Timer;

import imseProc.arduinoComm.ArduinoCommHanger;

import oneLiners.OneLiners;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

public class SerialSWTGroup {
	public static final long KEEPALIVE_TIME_MS = 60000;
	private ArduinoCommHanger proc;

	private Group swtGroup;
	private Label statusLabel;
	private Combo portDeviceTextbox;
	private Combo baudRateTextbox;
	private Button connectButton;
	private Button disconnectButton;	
	private Text serialLogTextbox;
	private Text sendTextbox;
	private Button sendButton;
	private Button pingCheckbox;
	private Spinner watchdogTimeoutSpinner;
	
	public SerialSWTGroup(Composite parent, ArduinoCommHanger proc) {
		this.proc = proc;
		
        swtGroup = new Group(parent, SWT.BORDER);
        swtGroup.setText("Comms");
        swtGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        swtGroup.setLayout(new GridLayout(6, false));
        
        Label lS = new Label(swtGroup, SWT.NONE); lS.setText("Status:");
	    statusLabel = new Label(swtGroup, SWT.NONE);
        statusLabel.setText("Init");
        statusLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 5, 1));
       
        Label lPN = new Label(swtGroup, SWT.NONE); lPN.setText("Device:");
	    portDeviceTextbox = new Combo(swtGroup, SWT.MULTI);
	    portDeviceTextbox.setItems(new String[]{ "/dev/ttyS0", "/dev/ttyACM0" });
	    portDeviceTextbox.select(0);
	    portDeviceTextbox.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 1, 1));
	    
	    Label lB = new Label(swtGroup, SWT.NONE); lB.setText("Baud:");
	    baudRateTextbox = new Combo(swtGroup, SWT.MULTI);
	    baudRateTextbox.setItems(new String[]{ "115200", "9600" });
	    baudRateTextbox.select(0);
	    baudRateTextbox.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 1, 1));
	   
	   
	    connectButton = new Button(swtGroup, SWT.PUSH);
	    connectButton.setText("Connect");
	    connectButton.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 1, 1));
	    connectButton.addListener(SWT.Selection, new Listener() { @Override public void handleEvent(Event event) { connectButtonEvent(event); } });

	    disconnectButton = new Button(swtGroup, SWT.PUSH);
	    disconnectButton.setText("Disconnect");
	    disconnectButton.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 1, 1));
	    disconnectButton.addListener(SWT.Selection, new Listener() { @Override public void handleEvent(Event event) { disconnectButtonEvent(event); } });

	    serialLogTextbox = new Text(swtGroup, SWT.MULTI | SWT.READ_ONLY | SWT.V_SCROLL);
	    serialLogTextbox.setText(" -- EMPTY -- \n\n\n\n\n\n\n");
	    serialLogTextbox.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 6, 1));
	    
	    sendTextbox = new Text(swtGroup, SWT.NONE );
	    sendTextbox.setText("");
	    sendTextbox.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 4, 1));
	    
	    sendButton = new Button(swtGroup, SWT.PUSH);
	    sendButton.setText("Send");
	    sendButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 1, 1));
	    sendButton.addListener(SWT.Selection, new Listener() { @Override public void handleEvent(Event event) { sendButtonEvent(event); } });

	    pingCheckbox = new Button(swtGroup, SWT.CHECK);
	    pingCheckbox.setText("Ping");
	    pingCheckbox.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 1, 1));
	    pingCheckbox.addListener(SWT.Selection, new Listener() { @Override public void handleEvent(Event event) { pingCheckboxEvent(event); } });
	    
	    Label lWD = new Label(swtGroup, SWT.NONE); lWD.setText("Timeout/min: ");
	    watchdogTimeoutSpinner = new Spinner(swtGroup, SWT.NONE);
	    watchdogTimeoutSpinner.setValues(60, -1, Integer.MAX_VALUE, 0, 1, 10);
	    watchdogTimeoutSpinner.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 1, 1));
	    watchdogTimeoutSpinner.addListener(SWT.Selection, new Listener() { @Override public void handleEvent(Event event) { watchdogSpinnerEvent(event); } });
	  
}
	

	private void connectButtonEvent(Event event) {
		proc.serialComm().connect(portDeviceTextbox.getText(), 
						OneLiners.mustParseInt(baudRateTextbox.getText()));
	}


	private void disconnectButtonEvent(Event event) {
		proc.serialComm().disconnect();
	}
	
	private void sendButtonEvent(Event event) {
		proc.serialComm().send(sendTextbox.getText());
		sendTextbox.setText("");
	}
	
	private void pingCheckboxEvent(Event event) {		
		proc.setKeepAliveTimeMS(pingCheckbox.getSelection() ? KEEPALIVE_TIME_MS : -1);		
	}
	
	private void watchdogSpinnerEvent(Event event) {		
		proc.setWatchdogTimeout(watchdogTimeoutSpinner.getSelection() * 60000);		
	}
	

	public void doUpdate() {
		statusLabel.setText(proc.serialComm().getStatus() 
				+ (proc.seqCtrl().isRemoteActive() ? ", ** SEQUENCE ACTIVE **" : ", No Sequence"));
		
		String str = proc.serialComm().getSerialLog().toString();
		int startPos = serialLogTextbox.getCharCount();
		String appendString = str.substring(startPos);
		serialLogTextbox.append(appendString);
		
	}	
}
