package com.dji.sdk.mqtt.state;

import com.dji.sdk.cloudapi.device.*;
import com.dji.sdk.cloudapi.livestream.RcLivestreamAbilityUpdate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Collections;
import java.util.Set;

/**
 *
 * @author sean.zhou
 * @date 2021/11/18
 * @version 0.1
 */
public enum RcStateDataKeyEnum {

    FIRMWARE_VERSION(Set.of("firmware_version"), FirmwareVersion.class),

    LIVE_CAPACITY(Set.of("live_capacity"), RcLivestreamAbilityUpdate.class),

    CONTROL_SOURCE(Set.of("control_source"), RcDroneControlSource.class),

    LIVE_STATUS(Set.of("live_status"), RcLiveStatus.class),

    PAYLOAD_FIRMWARE(PayloadModelConst.getAllModelWithPosition(), PayloadFirmwareVersion.class),

    /** RC / 飞行器也会上报 4G Dongle；与机场共用 DongleInfos 模型 */
    DONGLE_INFOS(Set.of("dongle_infos"), DongleInfos.class),

    /** Pilot/机场共用：当前指点飞模式 */
    CURRENT_COMMANDER_FLIGHT_MODE(Set.of("current_commander_flight_mode"), Object.class),

    /**
     * Pilot 云端控制授权状态（文档亦见 is_cloud_control_auth）。
     * 暂无强类型消费，走 DEFAULT，避免刷 ERROR。
     */
    CLOUD_CONTROL_AUTH(Set.of("cloud_control_auth", "is_cloud_control_auth"), Object.class),

    /** 图传拓扑等固件新增字段；暂无业务消费，走 DEFAULT */
    WIRELESS_LINK_TOPO(Set.of("wireless_link_topo", "wireless_link_topo_all"), Object.class),

    /** 固件新增未收录字段：不抛异常，路由到 DEFAULT */
    UNKNOWN(Collections.emptySet(), Object.class),
    ;

    private static final Logger log = LoggerFactory.getLogger(RcStateDataKeyEnum.class);

    private final Set<String> keys;

    private final Class classType;


    RcStateDataKeyEnum(Set<String> keys, Class classType) {
        this.keys = keys;
        this.classType = classType;
    }

    public Class getClassType() {
        return classType;
    }

    public Set<String> getKeys() {
        return keys;
    }

    public static RcStateDataKeyEnum find(Set<String> keys) {
        return Arrays.stream(values())
                .filter(keyEnum -> keyEnum != UNKNOWN && !Collections.disjoint(keys, keyEnum.keys))
                .findAny()
                .orElseGet(() -> {
                    log.debug("RC state unknown keys {}, route to DEFAULT", keys);
                    return UNKNOWN;
                });
    }

}
