package imseProc.arduinoComm.swt;

import imseProc.arduinoComm.ArduinoCommHanger;

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.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;

public class MotorSWTGroup {
	private ArduinoCommHanger proc;
	

	private Group swtGroup;
	private Button homeButton;
	private Spinner motorCurrentSpinner;
	private Spinner stepPeriodSpinner;
	private Button setCfgButton;
	private Button motorEnableCheckbox;
	private Spinner positionSpinner;
	private Button gotoPosButton;
	private Button getPosButton;	
	
	private boolean inhibitMotEnableListener = false;
	
	public MotorSWTGroup(Composite parent, ArduinoCommHanger proc) {
		this.proc = proc;
		
		swtGroup = new Group(parent, SWT.BORDER);
		swtGroup.setText("Motor");
		swtGroup.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 1, 1));
		swtGroup.setLayout(new GridLayout(6, false));

		motorEnableCheckbox = new Button(swtGroup, SWT.CHECK);
		motorEnableCheckbox.setText("Enable");
		motorEnableCheckbox.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 1, 1));
		motorEnableCheckbox.addListener(SWT.Selection, new Listener() { @Override public void handleEvent(Event event) { motorEnableButtonEvent(event); } });
	
		Label lMC = new Label(swtGroup, SWT.NONE); lMC.setText("Current/mA:");
		motorCurrentSpinner = new Spinner(swtGroup, SWT.NONE);
		motorCurrentSpinner.setValues(800, 0, 5000, 0, 100, 1000);
		motorCurrentSpinner.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 1, 1));
        
		Label lSP = new Label(swtGroup, SWT.NONE); lSP.setText("Step Period/µs:");
		stepPeriodSpinner = new Spinner(swtGroup, SWT.NONE);
		stepPeriodSpinner.setValues(2000, 0, 5000000, 0, 100, 1000);
		stepPeriodSpinner.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 1, 1));

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

		Label lP = new Label(swtGroup, SWT.NONE); lP.setText("Position:");
		positionSpinner = new Spinner(swtGroup, SWT.NONE);
		positionSpinner.setValues(2500, -100000, 100000, 0, 100, 1000);
		positionSpinner.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));

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

		getPosButton = new Button(swtGroup, SWT.PUSH);
		getPosButton.setText("Get");
		getPosButton.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 1, 1));
		getPosButton.addListener(SWT.Selection, new Listener() { @Override public void handleEvent(Event event) { motorGetPosButtonButtonEvent(event); } });
		
		homeButton = new Button(swtGroup, SWT.PUSH);
		homeButton.setText("Home");
		homeButton.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 1, 1));
		homeButton.addListener(SWT.Selection, new Listener() { @Override public void handleEvent(Event event) { motorHomeButtonEvent(event); } });
		
	}

	private void setMotorCfgButtonEvent(Event event) {
		proc.motorSetCurrent(motorCurrentSpinner.getSelection()); // in mA 
		proc.motorSetStepPeriod(stepPeriodSpinner.getSelection()); //already in µs
	}

	private void motorHomeButtonEvent(Event event) {
		proc.motorHome();
	}
	
	private void motorEnableButtonEvent(Event event){
		if(inhibitMotEnableListener)return;
		
		proc.motorSetEnable(motorEnableCheckbox.getSelection());
		inhibitMotEnableListener = true;
		motorEnableCheckbox.setSelection(proc.motorIsEnabled());
		inhibitMotEnableListener = false;
	}

	private void motorGotoButtonEvent(Event event) {
		proc.motorGotoPos(positionSpinner.getSelection());

	}
	
	private void motorGetPosButtonButtonEvent(Event event) {
		proc.motorRequestPos();
	}
	
	public void doUpdate() {
		inhibitMotEnableListener = true;
		motorEnableCheckbox.setSelection(proc.motorIsEnabled());
		inhibitMotEnableListener = false;
	}

	public int getStepPeriodUS() { return stepPeriodSpinner.getSelection(); }
}
