package io.vertx.up.plugin.stomp.websocket;

import io.vertx.ext.bridge.PermittedOptions;
import io.vertx.ext.stomp.BridgeOptions;
import io.vertx.up.atom.worker.Remind;
import io.vertx.up.extension.router.AresGrid;
import io.vertx.up.util.Ut;

import java.util.Set;

/**
 * @author <a href="http://www.origin-x.cn">Lang</a>
 */
public class BridgeStomp {

    public static BridgeOptions wsOptionBridge() {
        final Set<Remind> wsSock = AresGrid.wsSecure();
        final BridgeOptions bridgeOptions = new BridgeOptions();
        /*
         * To enable the bridge you need to configure the inbound and outbound addresses.
         */
        wsSock.forEach(remind -> {
            if (Ut.isNotNil(remind.getAddress()) && Ut.isNotNil(remind.getSubscribe())) {
                /*
                 * From Stomp to EventBus
                 * Inbound addresses are STOMP destination that are transferred to the event bus.
                 * The STOMP destination is used as the event bus address.
                 * @Subscribe -> @Address
                 *
                 * Here the address is event bus
                 */
                final PermittedOptions inbound = new PermittedOptions();
                inbound.setAddress(remind.getAddress());

                /*
                 * From EventBus to Stomp
                 * Outbound addresses are event bus addresses that are transferred to STOMP.
                 * @Address -> @Subscribe
                 *
                 * Here the address is stomp address
                 */
                final PermittedOptions outbound = new PermittedOptions();
                outbound.setAddress(remind.getSubscribe());
                bridgeOptions.addInboundPermitted(inbound).addOutboundPermitted(outbound);
            }
        });
        return bridgeOptions;
    }
}
