package com.yihu.hos.arbiter.routers;

import com.yihu.hos.arbiter.configuration.ActivemqConfiguration;
import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.model.ModelCamelContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.ConnectionFactory;

/**
 * @created Airhead 2016/8/1.
 */
@Component
public class ServiceFlowEventRouter extends RouteBuilder {
    @Autowired
    private ActivemqConfiguration activemqConfiguration;

    @Override
    public void configure() throws Exception {
        ModelCamelContext context = this.getContext();
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                activemqConfiguration.getUser(), activemqConfiguration.getPassword(), activemqConfiguration.getBrokerURL());
        // Note we can explicit name the component
        context.addComponent("service.flow.event", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
        from(ServiceFlowConstant.FLOW_EVENT_ENDPOINT)
                .choice()
                .when(header("tenant").isNotNull()).to("bean:serviceFlowService?method=proxy")
                .when(header("event").isEqualTo(ServiceFlowConstant.SERVICE_FLOW_STARTED)).to("bean:serviceFlowService?method=serviceFlowStarted")
                .when(header("event").isEqualTo(ServiceFlowConstant.SERVICE_FLOW_STOPPED)).to("bean:serviceFlowService?method=serviceFlowStopped")
                .when(header("event").isEqualTo(ServiceFlowConstant.SERVICE_FLOW_ADDED)).to("bean:serviceFlowService?method=serviceFlowAdd")
                .when(header("event").isEqualTo(ServiceFlowConstant.SERVICE_FLOW_MODIFIED_ADD)).to("bean:serviceFlowService?method=serviceFlowModifyAdd")
                .when(header("event").isEqualTo(ServiceFlowConstant.SERVICE_FLOW_MODIFIED_REDUCE)).to("bean:serviceFlowService?method=serviceFlowModifyReduce")
                .when(header("event").isEqualTo(ServiceFlowConstant.SERVICE_FLOW_DELETED)).to("bean:serviceFlowService?method=serviceFlowDelete")
                .when(header("event").isEqualTo(ServiceFlowConstant.BROKER_SERVER_ON)).to("bean:serviceFlowService?method=brokerServerOn")
                .when(header("event").isEqualTo(ServiceFlowConstant.BROKER_SERVER_OFF)).to("bean:brokerServerService?method=brokerServerOff")
                .endChoice();
    }
}
