package com.usky.cdi.service.mqtt;

import com.usky.cdi.service.vo.MqttBaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 中间处理消息转发
 */
@Service
public class SimpleContext {
    private final Map<String, MqttStrategy> strategyMap;

    @Autowired
    public SimpleContext(Map<String, MqttStrategy> strategyMap) {
        this.strategyMap = new ConcurrentHashMap<>();
        if (strategyMap != null) {
            strategyMap.forEach(this.strategyMap::put);
        }
    }

    public String getResource(MqttBaseVO mqttBaseVO) {
        if (mqttBaseVO == null || mqttBaseVO.getDescribe() == null) {
            return null;
        }
        MqttStrategy strategy = strategyMap.get(mqttBaseVO.getDescribe());
        if (strategy == null) {
            return null;
        }
        return strategy.disposeMessage(mqttBaseVO);
    }
}
