package leigh.conflux.nodegraph;

import leigh.eo.EO;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.mecha.nodegraph.Node;
import leigh.mecha.nodegraph.ProcessorState;
import leigh.mecha.nodegraph.UselessAttribute;

import java.io.Serializable;

/**
 * A Singleton node converts an anonymous value into a named value.
 *
 * @author Alex Leigh
 */
public class AttributeValueNode extends Node {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(AttributeValueNode.class);
    public static final String NODE_NAME = "leigh.conflux.v1.AttributeValue";
    public static final String KEY_OUTPUT_VALUE = "obj";
    public static final String KEY_INPUT_VALUE = "value";
    public static final String KEY_INPUT_NAME = "name";
    public static final String KEY_INPUT_DISPLAY_VALUE = "displayValue";
    public static final String KEY_INPUT_COLOR = "color";
    public static final String KEY_INPUT_FONTWEIGHT = "fontWeight";

    public AttributeValueNode(String id) {
        super(id);
        addOutput(KEY_OUTPUT_VALUE, new UselessAttribute() {
            public EO getObjectValue(ProcessorState state) throws Exception {
                String key = getInputSingleString(KEY_INPUT_NAME, state);
                Serializable value = getInputSingle(KEY_INPUT_VALUE, state);
                Serializable displayValue = getInputSingle(KEY_INPUT_DISPLAY_VALUE, state);
                Serializable color = getInputSingle(KEY_INPUT_COLOR, state);
                Serializable fontWeight = getInputSingle(KEY_INPUT_FONTWEIGHT, state);

                logger.debug("KEY_OUTPUT_VALUE: [k: {}] [v: {}] [d: {}] [c: {}]", key, value, displayValue, color);
                EO result = new EO();
                result.setValue(key, value);

                if (displayValue != null) result.setMetaValue(key, KEY_INPUT_DISPLAY_VALUE, displayValue, true);
                if (color != null) result.setMetaValue(key, KEY_INPUT_COLOR, color, true);
                if (fontWeight != null) result.setMetaValue(key, KEY_INPUT_FONTWEIGHT, fontWeight, true);

                logger.debug("Post processed object: {}", result);

                return result;
            }
        });
    }
}
