package com.lingniu.ingest.protocol.jsatl12.config;

import com.lingniu.ingest.core.dispatcher.Dispatcher;
import com.lingniu.ingest.identity.VehicleIdentityResolver;
import com.lingniu.ingest.protocol.jsatl12.handler.Jsatl12ArchiveEventHandler;
import com.lingniu.ingest.protocol.jsatl12.inbound.Jsatl12AttachmentTracker;
import com.lingniu.ingest.protocol.jsatl12.inbound.Jsatl12JtMessageBridge;
import com.lingniu.ingest.protocol.jsatl12.inbound.Jsatl12NettyServer;
import com.lingniu.ingest.protocol.jt808.codec.Jt808MessageDecoder;
import com.lingniu.ingest.protocol.jt808.config.Jt808AutoConfiguration;
import com.lingniu.ingest.sink.archive.ArchiveStore;
import com.lingniu.ingest.sink.archive.config.SinkArchiveAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

@AutoConfiguration(after = {SinkArchiveAutoConfiguration.class, Jt808AutoConfiguration.class})
@EnableConfigurationProperties(Jsatl12Properties.class)
@ConditionalOnProperty(prefix = "lingniu.ingest.jsatl12", name = "enabled", havingValue = "true")
@ConditionalOnBean({ArchiveStore.class, Jt808MessageDecoder.class})
public class Jsatl12AutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public Jsatl12AttachmentTracker jsatl12AttachmentTracker() {
        return new Jsatl12AttachmentTracker();
    }

    @Bean
    @ConditionalOnMissingBean
    public Jsatl12JtMessageBridge jsatl12JtMessageBridge(Jt808MessageDecoder decoder,
                                                         VehicleIdentityResolver identityResolver,
                                                         Jsatl12AttachmentTracker attachmentTracker,
                                                         Dispatcher dispatcher) {
        return new Jsatl12JtMessageBridge(decoder, identityResolver, attachmentTracker, dispatcher::dispatch);
    }

    @Bean
    @ConditionalOnMissingBean
    public Jsatl12ArchiveEventHandler jsatl12ArchiveEventHandler() {
        return new Jsatl12ArchiveEventHandler();
    }

    @Bean
    @ConditionalOnMissingBean
    public Jsatl12NettyServer jsatl12NettyServer(Jsatl12Properties props,
                                                 ArchiveStore archive,
                                                 Jsatl12JtMessageBridge jtMessageBridge,
                                                 Jsatl12AttachmentTracker attachmentTracker,
                                                 VehicleIdentityResolver identityResolver,
                                                 Dispatcher dispatcher) {
        return new Jsatl12NettyServer(props.getPort(), props.getWorkerThreads(),
                props.getMaxFrameLength(), archive, jtMessageBridge, attachmentTracker, identityResolver, dispatcher::dispatch);
    }
}
