package com.yeejoin.equip.mqtt;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yeejoin.equip.entity.EquipmentIndexVO;
import com.yeejoin.equip.entity.IndicatorData;
import com.yeejoin.equip.mqtt.message.MqttTopicEnum;
import com.yeejoin.equip.utils.RedisUtils;
import com.yeejoin.equip.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Map;
import static com.yeejoin.equip.mqtt.message.MqttConstant.*;

/**
 * @author LiuLin
 * @date 2023年07月13日 09:58
 */
@Slf4j
@Component
public class MessageTransfer {

    /**
     * 转为原生数据，payload为字节数组
     **/
    public static IndicatorData mqttMessage2RawMessage(String payload, Map<String, Object> headers) {
        log.info("received raw message, header >>> {}, payload >>> {}", headers, JSONObject.toJSONString(payload));
        RedisUtils redisUtils = (RedisUtils) SpringUtils.getBean("redisUtils");
        IndicatorData indicatorData = JSON.parseObject(payload, IndicatorData.class);

        try {
            String topic = headers.get(MqttHeaders.RECEIVED_TOPIC).toString();
            String[] topicItems = topic.split(TOPIC_SPLITTER);
            indicatorData.setMqttTopicEnum(MqttTopicEnum.of(topicItems[topicItems.length - 1]));
            String key = indicatorData.getAddress() + "_" + indicatorData.getGatewayId();
            if (redisUtils.hasKey(key)) {
                EquipmentIndexVO equipmentSpeIndex = JSONObject.parseObject(redisUtils.get(key),EquipmentIndexVO.class) ;
                String valueLabel = valueTranslate(indicatorData.getValue(), equipmentSpeIndex.getValueEnum());
                indicatorData.setIsAlarm(String.valueOf(equipmentSpeIndex.getIsAlarm()));
                indicatorData.setEquipmentIndexName(equipmentSpeIndex.getEquipmentIndexName());
                indicatorData.setEquipmentSpecificName(equipmentSpeIndex.getEquipmentSpecificName());
                indicatorData.setUnit(equipmentSpeIndex.getUnitName());
                indicatorData.setEquipmentsIdx(key);
                indicatorData.setValueLabel(valueLabel.isEmpty() ? indicatorData.getValue() : valueLabel);
                if (!Arrays.asList(TRUE, FALSE).contains(indicatorData.getValue())) {
                    indicatorData.setValueF(Float.parseFloat(indicatorData.getValue()));
                }
            }else {
                return null;
            }
        } catch (Exception e) {
            log.error("mqttMessage2RawMessage解析消息数据异常", e);
        }
        return indicatorData;
    }

    private static String valueTranslate(String value, String enumStr) {
        if (ObjectUtils.isEmpty(enumStr)) {
            return "";
        }
        try {
            JSONArray jsonArray = JSONArray.parseArray(enumStr);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                if (jsonObject.get("key").equals(value)) {
                    return jsonObject.getString("label");
                }
            }
        } catch (Exception e) {
            log.error("告警枚举转换异常" + e.getMessage(), e);
        }
        return "";
    }
}
