package net.northking.cctp.device.bus.receiver;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.northking.cctp.device.api.activity.service.DeviceActivityApiService;
import net.northking.cctp.device.db.entity.CdDeviceToken;
import net.northking.cctp.device.db.service.CdDeviceTokenService;
import net.northking.cctp.device.dto.device.CdMobileDeviceExitDto;
import net.northking.cctp.device.dto.pcDevice.CdPcDeviceExitDto;
import net.northking.cctp.device.util.DeviceStatusUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class DeviceMQReceiver {

    @Autowired
    private DeviceStatusUtil statusUtil;

    @Autowired
    private CdDeviceTokenService cdDeviceTokenService;

    @Autowired
    private DeviceActivityApiService activityService;

    @Autowired
    private ObjectMapper om;


    private final static Logger logger = LoggerFactory.getLogger(DeviceMQReceiver.class);

    @RabbitListener(queuesToDeclare = {@Queue(value = "execute.engine.release")})
    public void DeviceMQReceiver(Message message){
        // 释放设备
        try{
            byte[] body = message.getBody();
            List<String> list = om.readValue(body, new TypeReference<List<String>>() {
            });
            for (String token : list) {
                CdDeviceToken tokenQuery = new CdDeviceToken();
                tokenQuery.setToken(token);
                //查询设备是否存在token
                List<CdDeviceToken> resultList = cdDeviceTokenService.query(tokenQuery);
                if (resultList != null && resultList.size() > 0) {
                    CdDeviceToken device = resultList.get(0);
                    switch (device.getDeviceType()) {
                        case "1":
                            CdMobileDeviceExitDto mobileDto = new CdMobileDeviceExitDto();
                            mobileDto.setDeviceId(device.getId());
                            mobileDto.setToken(token);
                            activityService.releaseMobDevice(mobileDto);
                            logger.info("队列释放移动设备 id -> {},token -> {}",device.getId(),token);
                            break;
                        case "2":
                            CdPcDeviceExitDto pcDto = new CdPcDeviceExitDto();
                            pcDto.setDeviceId(device.getId());
                            pcDto.setToken(token);
                            activityService.releasePcDevice(pcDto);
                            logger.info("队列释放pc设备 id -> {},token -> {}",device.getId(),token);
                            break;
                    }
                }
            }
        }catch (Exception e){
            logger.error("释放设备异常",e);
        }
    }

    /**
     *
     * @param message
     */
    @RabbitListener(queuesToDeclare = {@Queue(value = "execute.engine.release.id")})
    public void releaseById(Message message){
        try{
            byte[] body = message.getBody();
            Map<String, Object> mqParam = om.readValue(body, new TypeReference<Map<String, Object>>() {
            });

            activityService.releaseDeviceList(mqParam);
        }catch (Exception e){
            logger.error("释放设备异常",e);
        }
    }
}
