package org.ts.ddcs.main;


import lombok.extern.slf4j.Slf4j;
import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;


import org.ts.ddcs.config.MqQueueConfig;
import org.ts.ddcs.config.ServerConfig;
import org.ts.ddcs.kafka.KafkaClient;
import org.ts.ddcs.kafkaStream.KafkaStream;
import org.ts.ddcs.kafkaStreamHolder.KafkaStreamHolder;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.*;

/**
 * 数据处理主类
 *
 * @author dylan
 */
@Slf4j
@Component
public class DataProcess {
    private static ThreadFactory publicThreadFactory = new ThreadFactoryBuilder().setNameFormat("public-thread-pool-%d").build();
    /**
     * 公共线程池
     **/
    private static ExecutorService publicThreadPool = new ThreadPoolExecutor(1, 1,
            0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024), publicThreadFactory, new ThreadPoolExecutor.AbortPolicy());

    @Value("${spring.application.name}")
    public String serverName;

    @Value("${run-config.server.templates-dir}")
    public String templatesDir;

    @Value("${run-config.server.templates}")
    public String templates;

    @Value("${run-config.server.tcp-port}")
    public String tcpPort;

    @Value("${run-config.server.udp-port}")
    public String udpPort;


    @Autowired
    public ServerConfig config;

    @Autowired
    public MqQueueConfig queueConfig;

    @Autowired
    public RedisTemplate redisTemplate;





    public static ServerConfig config() {
        return DataProcessHolder.newInstance().dataProcess.config;
    }

    public static MqQueueConfig queueConfig() {
        return DataProcessHolder.newInstance().dataProcess.queueConfig;
    }

    public static List<byte[]> commandList = new LinkedList<>();

    private KafkaStream kafkaStream = new KafkaStream();

    @PostConstruct
    public void init() {
        DataProcessHolder.newInstance().dataProcess = this;
        KafkaStreamHolder.newInstance().setKafkaStream(kafkaStream);
        kafkaStream.start();
    }

}
