package com.multictrl.modules.business.q20.service.impl;

import com.multictrl.common.utils.CacheUtils;
import com.multictrl.modules.business.q20.handler.Q20Constant;
import com.multictrl.modules.business.q20.service.Q20DeviceService;
import com.multictrl.modules.business.q20.vo.Q20DeviceStatusVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Q20飞机设备服务实现
 *
 * @author 938693313@qq.com
 * @since 1.0.0 2026/5/20
 */
@Service
@RequiredArgsConstructor
public class Q20DeviceServiceImpl implements Q20DeviceService {

    @Override
    public Q20DeviceStatusVO getOnlineStatus(String deviceSn) {
        Q20DeviceStatusVO vo = new Q20DeviceStatusVO();
        vo.setDeviceSn(deviceSn);
        boolean online = CacheUtils.get(Q20Constant.Q20_ONLINE + deviceSn) != null;
        vo.setOnline(online);
        if (online) {
            vo.setOsd(CacheUtils.get(Q20Constant.Q20_OSD + deviceSn));
            vo.setRtk(CacheUtils.get(Q20Constant.Q20_RTK + deviceSn));
            vo.setRc(CacheUtils.get(Q20Constant.Q20_RC + deviceSn));
            vo.setObs(CacheUtils.get(Q20Constant.Q20_OBS + deviceSn));
            vo.setMount(CacheUtils.get(Q20Constant.Q20_MOUNT + deviceSn));
            Object batteries = CacheUtils.get(Q20Constant.Q20_BATTERY + deviceSn);
            if (batteries instanceof Map) {
                vo.setBatteries(new ArrayList<>(((Map<?, ?>) batteries).values()));
            }
        }
        return vo;
    }

    @Override
    public List<Q20DeviceStatusVO> getOnlineStatusBatch(List<String> deviceSnList) {
        if (CollectionUtils.isEmpty(deviceSnList)) {
            return Collections.emptyList();
        }
        return deviceSnList.stream().map(this::getOnlineStatus).collect(Collectors.toList());
    }
}
