package leigh.astro.indi;

import leigh.astro.indi.client.INDIDevice;
import leigh.astro.indi.client.INDIDeviceListener;
import leigh.astro.indi.client.INDIProperty;
import leigh.astro.indi.client.INDIPropertyListener;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;

/**
 * This class implements a debug listener for INDI devices, and simply outputs their properties and changes.
 *
 * @author Alex Leigh
 */
public class DebugListener implements INDIDeviceListener {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(DebugListener.class);
    private final INDIDevice dev;

    public DebugListener(INDIDevice dev) {
        this.dev = dev;
    }

    @Override
    public void newProperty(INDIDevice indiDevice, INDIProperty<?> indiProperty) {
        logger.info("newProp: [dev: {}] [name: {}] [values: {}]",
                indiDevice.getName(), indiProperty.getName(), indiProperty.getValuesAsString());
        indiProperty.addINDIPropertyListener(new INDIPropertyListener() {
            @Override
            public void propertyChanged(INDIProperty<?> property) {
                logger.info("Property changed. [dev: {}] [name: {}] [value: {}]",
                        indiDevice.getName(), property.getName(), property.getValuesAsString());
            }
        });
    }

    @Override
    public void removeProperty(INDIDevice indiDevice, INDIProperty<?> indiProperty) {
        logger.info("removeProperty: [device: {}] [name: {}]", indiDevice.getName(), indiProperty);

    }

    @Override
    public void messageChanged(INDIDevice indiDevice) {
        logger.info("Message: [dev: {}] [msg: {}]", indiDevice.getName(), indiDevice.getLastMessage());
    }
}