package org.ts.ddcs.worker;

import org.ts.ddcs.config.ServerConfig;
import org.ts.ddcs.entity.WarningInfo;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.rabbitmq.RabbitQueueConfig;
import org.ts.ddcs.service.RtuService;
import org.ts.ddcs.service.WarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.concurrent.*;

/**
 * @author dylan
 */
@Component
public class DataProcess {

    private ExecutorService rtuOnlineCheckThreadPool = Executors.newFixedThreadPool(1);
    private ExecutorService rtuStatusCheckThreadPool = Executors.newFixedThreadPool(1);
    private ExecutorService warningNotifyThreadPool = Executors.newFixedThreadPool(1);
    private ExecutorService warningProcessThreadPool = Executors.newFixedThreadPool(1);

    @Autowired
    public ServerConfig config;
    @Autowired
    public RabbitQueueConfig queueConfig;
    @Autowired
    public RedisTemplate redisTemplate;
    @Autowired
    public WarningService warningService;
    @Autowired
    public RtuService rtuService;
    @Autowired
    public KafkaTemplate kafkaTemplate;

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

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

    @PostConstruct
    public void init() {
        DataProcessHolder.newInstance().dataProcess = this;
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
    }

    public void submitOnlineCheckTask(Date checkTime) {
        try {
            RtuOnlineCheckTask task = new RtuOnlineCheckTask(checkTime);
            FutureTask<Integer> futureTask = new FutureTask<>(task);
            this.rtuOnlineCheckThreadPool.submit(futureTask);
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    public void submitRtuStatusCheckTask(String jsonBody) {
        try {
            RtuStatusCheckTask task = new RtuStatusCheckTask(jsonBody);
            FutureTask<Integer> futureTask = new FutureTask<>(task);
            this.rtuStatusCheckThreadPool.submit(futureTask);
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    public void submitWarningNotifyTask(WarningInfo warningInfo) {
        try {
            if (this.config.isMessageNotify()) {
                WarningNotifyTask task = new WarningNotifyTask(warningInfo);
                FutureTask<Integer> futureTask = new FutureTask<>(task);
                this.warningNotifyThreadPool.submit(futureTask);
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    public void submitWarningProcessTask(String jsonBody) {
        try {
            WarningProcessTask task = new WarningProcessTask(jsonBody);
            FutureTask<Integer> futureTask = new FutureTask<>(task);
            this.warningProcessThreadPool.submit(futureTask);
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
