package com.mortals.xhx.daemon.applicationservice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.springcloud.service.IApplicationService;
import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.common.utils.SendTaskThreadPool;
import com.mortals.xhx.common.utils.SmsQueueManager;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.module.alarm.model.AlarmSmsSendEntity;
import com.mortals.xhx.module.platform.model.PlatformQuery;
import com.mortals.xhx.module.platform.service.PlatformService;
import com.mortals.xhx.module.product.model.ProductQuery;
import com.mortals.xhx.module.product.service.ProductService;
import com.mortals.xhx.queue.TbQueueMsg;
import com.mortals.xhx.utils.IotThreadFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static com.mortals.xhx.common.key.RedisKey.*;

@Component
@Slf4j
public class DeviceStartService implements IApplicationStartedService {

    @Autowired
    private SendTaskThreadPool sendTaskThreadPool;

    protected Boolean stopped = false;
    @Autowired
    private ICacheService cacheService;
    @Autowired
    private ISiteFeign siteFeign;
    @Autowired
    private PlatformService platformService;
    @Autowired
    private ProductService productService;

    @Override
    public void start() {
        log.info("初始化发送线程数量");
        sendTaskThreadPool.init(20);
        //启动短信发送响应更新线程

        sendTaskThreadPool.execute(() -> {
            int waitTime = 1000;
            while (!stopped) {
                try {
                    AlarmSmsSendEntity message = SmsQueueManager.pollRespQueue();

                    if (!ObjectUtils.isEmpty(message)) {
                        // TODO: 2022/7/5 更新响应
                    }
                } catch (Exception e) {
                    if (!stopped) {
                        try {
                            Thread.sleep(waitTime);
                        } catch (InterruptedException e2) {
                        }
                    }
                }
            }
        });


        //初始化站点缓存

        String resp = siteFeign.list(new SitePdu());
        JSONObject jsonObject = JSONObject.parseObject(resp);
        if (jsonObject.getInteger("code") == 1) {
            List<SitePdu> sitePduList = jsonObject.getJSONObject("data").getObject("data", new TypeReference<List<SitePdu>>() {
            });

            sitePduList.stream().forEach(sitePdu -> {
                cacheService.hset(KEY_SITE_CACHE, sitePdu.getId().toString(), sitePdu.getSiteName());
            });
        }

        productService.find(new ProductQuery()).stream().forEach(item -> {
            cacheService.hset(KEY_PRODUCT_CACHE, item.getProductCode(), item.getProductName());
        });

        platformService.find(new PlatformQuery()).stream().forEach(item -> {
            cacheService.hset(KEY_PLATFORM_CACHE, item.getPlatformSn(), item.getPlatformName());
        });

        log.info("开始服务..[配置已加载完成，但部分框架还未初始化，比如：Kafka]");
    }

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

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

}
