package com.ecep.contract.util;

import org.controlsfx.control.ToggleSwitch;

import javafx.beans.property.Property;
import javafx.util.StringConverter;
import javafx.util.converter.BooleanStringConverter;

public class BooleanConfig extends AbstractConfigBounder<Boolean> {
    public BooleanConfig(String key) {
        super(key);
    }

    @Override
    public ToggleSwitch getControl() {
        return (ToggleSwitch) super.getControl();
    }

    @Override
    public StringConverter<Boolean> getConverter() {
        StringConverter<Boolean> converter = super.getConverter();
        if (converter == null) {
            converter = new BooleanStringConverter();
            setConverter(converter);
        }
        return converter;
    }

    @Override
    protected Property<Boolean> createProperty(Boolean value) {
        // fixbug when value is null
        value = value == null ? false : value;
        return super.createProperty(value);
    }

    @Override
    void bindBidirectional(Property<Boolean> property) {
        getControl().selectedProperty().bindBidirectional(property);
    }
}
