
package org.apache.camel.impl.console;

import java.util.Map;

import org.apache.camel.spi.PropertiesComponent;
import org.apache.camel.spi.annotations.DevConsole;
import org.apache.camel.util.json.JsonObject;

@DevConsole("properties")
public class PropertiesDevConsole extends AbstractDevConsole {

    public PropertiesDevConsole() {
        super("camel", "properties", "Properties", "Displays the properties loaded by Camel");
    }

    @Override
    protected String doCallText(Map<String, Object> options) {
        StringBuilder sb = new StringBuilder();

        PropertiesComponent pc = getCamelContext().getPropertiesComponent();
        String loc = String.join(", ", pc.getLocations());
        sb.append(String.format("Properties loaded from locations: %s", loc));
        sb.append("\n");
        for (Map.Entry<Object, Object> entry : pc.loadProperties().entrySet()) {
            Object k = entry.getKey();
            Object v = entry.getValue();
            sb.append(String.format("\n    %s = %s", k, v));
        }
        sb.append("\n");

        return sb.toString();
    }

    @Override
    protected JsonObject doCallJson(Map<String, Object> options) {
        JsonObject root = new JsonObject();

        PropertiesComponent pc = getCamelContext().getPropertiesComponent();
        root.put("locations", pc.getLocations());
        JsonObject props = new JsonObject();
        root.put("properties", props);
        for (Map.Entry<Object, Object> entry : pc.loadProperties().entrySet()) {
            String k = entry.getKey().toString();
            Object v = entry.getValue();
            props.put(k, v);
        }

        return root;
    }
}
