package at.procon.eventhub.camel;

import at.procon.eventhub.dto.EventHubEventDto;
import at.procon.eventhub.service.EventHubPackageKeyBuilder;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.springframework.stereotype.Component;

@Component
public class EventHubPackageKeyProcessor implements Processor {

    private final EventHubPackageKeyBuilder packageKeyBuilder;

    public EventHubPackageKeyProcessor(EventHubPackageKeyBuilder packageKeyBuilder) {
        this.packageKeyBuilder = packageKeyBuilder;
    }

    @Override
    public void process(Exchange exchange) {
        EventHubEventDto event = exchange.getMessage().getBody(EventHubEventDto.class);
        if (exchange.getMessage().getHeader(EventHubHeaders.PACKAGE_KEY) == null) {
            exchange.getMessage().setHeader(EventHubHeaders.PACKAGE_KEY, packageKeyBuilder.build(event));
        }
        if (exchange.getMessage().getHeader(EventHubHeaders.PACKAGE_INFO) == null && event.packageInfo() != null) {
            exchange.getMessage().setHeader(EventHubHeaders.PACKAGE_INFO, event.packageInfo());
        }
    }
}
