package com.mortals.xhx.daemon.applicationservice;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.xhx.base.framework.listener.SimpleDynamicListener;
import com.mortals.xhx.common.code.*;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.common.key.RedisKey;
import com.mortals.xhx.common.utils.SendTask;
import com.mortals.xhx.common.utils.SendTaskThreadPool;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.model.DeviceLogEntity;
import com.mortals.xhx.module.device.service.DeviceLogService;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.platform.model.PlatformEntity;
import com.mortals.xhx.module.platform.service.PlatformService;
import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.service.ProductService;
import com.mortals.xhx.queue.DefaultTbCoreConsumerService;
import com.mortals.xhx.queue.TbQueueConsumer;
import com.mortals.xhx.queue.TbQueueMsg;
import com.mortals.xhx.queue.TopicPartitionInfo;
import com.mortals.xhx.utils.IotThreadFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.mortals.xhx.common.model.MessageHeader.DEVICECODE;
import static com.mortals.xhx.common.model.MessageHeader.MESSAGETYPE;


//@Component
@Slf4j
public class RabbitMsgComsumerStartedService implements IApplicationStartedService {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private ProductService productService;
    @Autowired
    private PlatformService platformService;
    @Autowired
    private SimpleDynamicListener simpleDynamicListener;
    @Autowired
    private DirectMessageListenerContainer directMessageListenerContainer;

    @Autowired
    @Qualifier("simpleMessageListenerContainer")
    private SimpleMessageListenerContainer simpleContainer;


    protected Boolean stopped = false;

    @Override
    public void start() {
        log.info("服务端消费消息服务开始..");
        String[] queues = deviceService.find(new DeviceEntity())
                .stream()
                .filter(f -> !ObjectUtils.isEmpty(platformService.get(f.getPlatformId())))
                .filter(f -> !ObjectUtils.isEmpty(productService.get(f.getProductId())))
                .map(item -> Constant.UPLOAD_TOPIC + item.getDeviceCode())
                .distinct()
                .toArray(String[]::new);
        simpleContainer.addQueueNames(queues);
        simpleContainer.start();
        simpleContainer.addQueueNames("123");
        simpleContainer.setMessageListener(simpleDynamicListener);

    }

    @Override
    public void stop() {
        log.info("停止服务..");
    }

    @Override
    public int getOrder() {
        return 10;
    }

}
