package leigh.esp.kabe.http;

import leigh.esp.kabe.KabeServer;
import leigh.esp.kabe.ServiceBus;
import leigh.mecha.fabric.LiteralMessageSubscription;
import leigh.mecha.http.server.WebPipeline;
import leigh.mecha.http.server.WebServer;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;

import java.util.HashSet;

public class PublicHttpBus implements ServiceBus {
    private final static MechaLogger logger = MechaLoggerFactory.getLogger(PublicHttpBus.class);
    private final WebServer webserver;

    public PublicHttpBus(KabeServer kabe, int port) {
        webserver = new WebServer(port, 10000);

        // This must be last, as it binds to "/"
        setupLink23Door(kabe, webserver);
    }

    private void setupLink23Door(KabeServer kabe, WebServer webserver) {
        HashSet<String> prefixes = new HashSet<>();
        prefixes.add("/");
        Link23Door door = new Link23Door(prefixes, kabe);
        webserver.getWebPipeline().getServicePipeline().getSubscriptionBase().subscribe(
                new LiteralMessageSubscription<>(door, WebPipeline.KEY_SERVICE));
    }


    @Override
    public void start() {
        webserver.start();
    }
}
