package com.ruoyi.device.service.impl;

import com.ruoyi.device.domain.entity.DeviceUpgradePackage;
import com.ruoyi.device.mapper.DeviceUpgradePackageMapper;
import com.ruoyi.device.mqtt.domain.encoder.upgrade.SendApkListRequest;
import com.ruoyi.device.mqtt.domain.encoder.upgrade.SendApkUrlRequest;
import com.ruoyi.device.mqtt.domain.decoder.upgrade.GetApkUrlMessage;
import com.ruoyi.device.mqtt.domain.decoder.upgrade.QueryApkListMessage;
import com.ruoyi.device.mqtt.enums.DeviceUpgradePackageTypeEnum;
import com.ruoyi.device.mqtt.enums.MsgTypeEnum;
import com.ruoyi.device.mqtt.handler.HandlerManager;
import com.ruoyi.device.mqtt.handler.encoder.IEncoder;
import com.ruoyi.device.mqtt.util.MsgHandlerUtil;
import com.ruoyi.device.service.IDeviceUpgradePackageService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备升级包服务实现
 */
@Service
public class DeviceUpgradePackageServiceImpl implements IDeviceUpgradePackageService
{
    private static final Logger log = LoggerFactory.getLogger(DeviceUpgradePackageServiceImpl.class);

    @Resource
    private DeviceUpgradePackageMapper deviceUpgradePackageMapper;

    @Resource
    private HandlerManager handlerManager;

    @Override
    public List<DeviceUpgradePackage> selectDeviceUpgradePackageList(DeviceUpgradePackage query)
    {
        return deviceUpgradePackageMapper.selectDeviceUpgradePackageList(query);
    }

    @Override
    public void handleQueryApkList(QueryApkListMessage queryApkListMessage)
    {
        try
        {
            log.info("查询固件升级包：{}", queryApkListMessage);

            DeviceUpgradePackage query = new DeviceUpgradePackage();
            query.setType(DeviceUpgradePackageTypeEnum.PROD_PACKAGE.getType());
            query.setDeviceType(queryApkListMessage.getTargetDeviceType());
            query.setFirmwareType(queryApkListMessage.getFirmwareType());
            List<DeviceUpgradePackage> packageList = selectDeviceUpgradePackageList(query);
            List<String> versionList = new ArrayList<>();
            for (DeviceUpgradePackage item : packageList)
            {
                versionList.add(item.getAppVersion());
            }

            SendApkListRequest sendApkListRequest = new SendApkListRequest();
            sendApkListRequest.setDeviceSn(queryApkListMessage.getDeviceSn());
            sendApkListRequest.setDeviceType(queryApkListMessage.getDeviceType());
            sendApkListRequest.setTargetDeviceType(queryApkListMessage.getTargetDeviceType());
            sendApkListRequest.setFirmwareType(queryApkListMessage.getFirmwareType());
            sendApkListRequest.setVersionList(versionList);

            encode(MsgTypeEnum.GET_APK_DOWN, sendApkListRequest);
        }
        catch (Exception e)
        {
            log.error("查询固件升级包处理异常：{}", e.getMessage(), e);
        }
    }

    @Override
    public void handleGetApkUrl(GetApkUrlMessage getApkUrlMessage)
    {
        try
        {
            log.info("获取固件升级包地址：{}", getApkUrlMessage);

            DeviceUpgradePackage query = new DeviceUpgradePackage();
            query.setType(DeviceUpgradePackageTypeEnum.PROD_PACKAGE.getType());
            query.setDeviceType(getApkUrlMessage.getTargetDeviceType());
            query.setFirmwareType(getApkUrlMessage.getFirmwareType());
            query.setAppVersion(getApkUrlMessage.getVersion());
            List<DeviceUpgradePackage> packageList = selectDeviceUpgradePackageList(query);
            if (packageList.isEmpty())
            {
                log.info("未找到匹配的固件升级包：{}", getApkUrlMessage);
                return;
            }

            SendApkUrlRequest sendApkUrlRequest = new SendApkUrlRequest();
            sendApkUrlRequest.setDeviceSn(getApkUrlMessage.getDeviceSn());
            sendApkUrlRequest.setDeviceType(getApkUrlMessage.getDeviceType());
            sendApkUrlRequest.setTargetDeviceType(getApkUrlMessage.getTargetDeviceType());
            sendApkUrlRequest.setFirmwareType(getApkUrlMessage.getFirmwareType());
            sendApkUrlRequest.setDownloadUrl(packageList.get(0).getDownloadUrl());

            encode(MsgTypeEnum.GET_APK_URL_DOWN, sendApkUrlRequest);
        }
        catch (Exception e)
        {
            log.error("获取固件升级包地址处理异常：{}", e.getMessage(), e);
        }
    }

    @SuppressWarnings("unchecked")
    private <T> void encode(MsgTypeEnum msgType, T request)
    {
        String key = MsgHandlerUtil.getEncoderKey(msgType);
        IEncoder<T> encoder = (IEncoder<T>) handlerManager.getEncoder(key);
        encoder.encode(request);
    }
}
