package com.dji.sample.component;

import com.dji.sample.component.redis.RedisConst;
import com.dji.sample.component.redis.RedisOpsUtils;
import com.dji.sample.manage.model.dto.DeviceDTO;
import com.dji.sample.manage.model.entity.DeviceEntity;
import com.dji.sample.manage.service.IDeviceRedisService;
import com.dji.sample.manage.service.IDeviceService;
import com.dji.sdk.cloudapi.device.DeviceDomainEnum;
import com.dji.sdk.cloudapi.device.DeviceSubTypeEnum;
import com.dji.sdk.cloudapi.device.DeviceTypeEnum;
import com.dji.sdk.common.SDKManager;
import com.dji.sdk.config.version.GatewayManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
 * @author sean.zhou
 * @date 2021/11/24
 * @version 0.1
 */
@Component
public class ApplicationBootInitial implements CommandLineRunner {

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private IDeviceRedisService deviceRedisService;

    /**
     * Subscribe to the devices that exist in the redis when the program starts,
     * to prevent the data from being different from the pilot side due to program interruptions.
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        int start = RedisConst.DEVICE_ONLINE_PREFIX.length();

        // 系统启动的时候，把数据库中设备加入订阅中
        // 查询现有的设备
        List<DeviceEntity> deviceList = deviceService.list();

        // 订阅设备
        deviceList.stream()
                .filter(device -> device.getDomain() == 3)
                .forEach(device -> {

                    DeviceDomainEnum domainEnum = DeviceDomainEnum.find(device.getDomain());
                    DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.find(device.getDeviceType());
                    DeviceSubTypeEnum deviceSubTypeEnum = DeviceSubTypeEnum.find(device.getSubType());

                    GatewayManager gatewayManager = SDKManager.registerDevice(device.getDeviceSn(), device.getChildSn(), domainEnum,
                            deviceTypeEnum, deviceSubTypeEnum, device.getVersion(),
                            deviceRedisService.getDeviceOnline(device.getChildSn()).map(DeviceDTO::getThingVersion).orElse(null)
                    );

                    deviceService.subDeviceOnlineSubscribeTopic(gatewayManager);
                });

        RedisOpsUtils.getAllKeys(RedisConst.DEVICE_ONLINE_PREFIX + "*")
                .stream()
                .map(key -> key.substring(start))
                .map(deviceRedisService::getDeviceOnline)
                .map(Optional::get)
                .filter(device -> DeviceDomainEnum.DRONE != device.getDomain())
                .forEach(device -> deviceService.subDeviceOnlineSubscribeTopic(
                        SDKManager.registerDevice(device.getDeviceSn(), device.getChildDeviceSn(), device.getDomain(),
                                device.getType(), device.getSubType(), device.getThingVersion(),
                                deviceRedisService.getDeviceOnline(device.getChildDeviceSn()).map(DeviceDTO::getThingVersion).orElse(null))));

    }
}