package com.ankurm.camel;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

/**
 * The terminal endpoints below are property placeholders, not literal
 * URIs - {{camel.route.priority-out}} and friends resolve from Spring
 * Boot configuration (camel-spring-boot wires Camel's property
 * placeholder resolver to the Spring Environment automatically). In
 * application.properties they point at real "log:" endpoints; a test's
 * application.properties overrides them to "mock:" endpoints instead.
 * The route logic itself never changes between the two - only
 * configuration does, which is the same shape of problem the
 * Spring-profile-switched CacheManager in the distributed-caching post
 * solved a different way.
 */
@Component
public class OrderRoutes extends RouteBuilder {

    private final OrderProcessor orderProcessor;

    public OrderRoutes(OrderProcessor orderProcessor) {
        this.orderProcessor = orderProcessor;
    }

    @Override
    public void configure() {
        onException(IllegalArgumentException.class)
                .maximumRedeliveries(2)
                .redeliveryDelay(0)
                .handled(true)
                .to("{{camel.route.dead-letter-out}}")
                .end();

        from("direct:orders")
                .routeId("order-intake")
                .choice()
                    .when(header("orderType").isEqualTo("PRIORITY"))
                        .bean(orderProcessor, "processOrder")
                        .to("{{camel.route.priority-out}}")
                    .otherwise()
                        .bean(orderProcessor, "processOrder")
                        .to("{{camel.route.standard-out}}")
                .end();
    }
}
