package parameter;

import javafx.scene.Node;
import javafx.scene.control.CheckBox;

public class ParameterCheckbox implements IParameter<Boolean> {
	
	private CheckBox myNode;
	private String myName, myLabel;
	
	public ParameterCheckbox(String name, String label, Boolean value) {
		
		myName = name;
		myLabel = label;
		
		myNode = new CheckBox();
		setValue(value);
	}

	@Override
	public Node getNode() {
		return myNode;
	}

	@Override
	public void setValue(Boolean value) {
		myNode.setSelected(value);	
	}

	@Override
	public Boolean constrainValue(Boolean minValue) {
		return minValue;
	}

	@Override
	public Boolean getValue() {
		return myNode.isSelected();
	}

	@Override
	public String getName() {
		return myName;
	}

	@Override
	public String getLabel() {
		return myLabel;
	}

	@Override
	public void onChange(IParameterListener<Boolean> listener) {
		myNode.selectedProperty().addListener(event -> {
			listener.onValueChanged(getValue());
		});
	}

	@Override
	public void loadFromString(String value) {
		try {
			Boolean val = Boolean.parseBoolean(value);
			setValue(val);
		} catch (Exception ex) {
			System.out.println(String.format("Could not parse bool from string: '%s'", value));
		}
	}
	
	@Override
	public String toString() {
		if (getValue()) {
			return "true";
		} else {
			return "false";
		}
	}

}
