package com.github.longkerdandy.mithril.mqtt.storage.redis.util;

import com.github.longkerdandy.mithril.mqtt.api.internal.InternalMessage;
import com.github.longkerdandy.mithril.mqtt.api.internal.PacketId;
import com.github.longkerdandy.mithril.mqtt.api.internal.Publish;
import io.netty.handler.codec.mqtt.MqttMessageType;
import io.netty.handler.codec.mqtt.MqttQoS;
import io.netty.handler.codec.mqtt.MqttVersion;
import org.apache.commons.lang3.BooleanUtils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Converter Utils
 */
public class Converter {

    /**
     * Convert Map to InternalMessage
     *
     * @param map Map
     * @return InternalMessage
     */
    public static InternalMessage mapToInternal(Map<String, String> map) {
        if (map == null || map.isEmpty()) return null;

        int type = Integer.parseInt(map.get("type"));
        if (type == MqttMessageType.PUBLISH.value()) {
            byte[] payload = null;
            if (map.get("payload") != null) try {
                payload = map.get("payload").getBytes("ISO-8859-1");
            } catch (UnsupportedEncodingException ignore) {
            }
            return new InternalMessage<>(
                    MqttMessageType.PUBLISH,
                    BooleanUtils.toBoolean(map.getOrDefault("dup", "0"), "1", "0"),
                    MqttQoS.valueOf(Integer.parseInt(map.getOrDefault("qos", "0"))),
                    BooleanUtils.toBoolean(map.getOrDefault("retain", "0"), "1", "0"),
                    MqttVersion.valueOf(map.getOrDefault("version", MqttVersion.MQTT_3_1_1.toString())),
                    map.get("clientId"),
                    map.get("userName"),
                    null,
                    new Publish(
                            map.get("topicName"),
                            Integer.parseInt(map.getOrDefault("packetId", "0")),
                            payload
                    ));
        } else if (type == MqttMessageType.PUBREL.value()) {
            return new InternalMessage<>(
                    MqttMessageType.PUBREL,
                    false,
                    MqttQoS.AT_LEAST_ONCE,
                    false,
                    MqttVersion.valueOf(map.getOrDefault("version", MqttVersion.MQTT_3_1_1.toString())),
                    map.get("clientId"),
                    map.get("userName"),
                    null,
                    new PacketId(Integer.parseInt(map.getOrDefault("packetId", "0"))));
        } else {
            throw new IllegalArgumentException("Invalid in-flight MQTT message type: " + MqttMessageType.valueOf(type));
        }
    }

    /**
     * Convert InternalMessage to Map
     *
     * @param msg InternalMessage
     * @return Map
     */
    public static Map<String, String> internalToMap(InternalMessage msg) {
        Map<String, String> map = new HashMap<>();
        if (msg == null) return map;

        if (msg.getMessageType() == MqttMessageType.PUBLISH) {
            Publish publish = (Publish) msg.getPayload();
            map.put("type", String.valueOf(MqttMessageType.PUBLISH.value()));
            map.put("retain", BooleanUtils.toString(msg.isRetain(), "1", "0"));
            map.put("qos", String.valueOf(msg.getQos().value()));
            map.put("dup", BooleanUtils.toString(msg.isDup(), "1", "0"));
            map.put("version", msg.getVersion().toString());
            if (!msg.isRetain()) map.put("clientId", msg.getClientId());
            map.put("userName", msg.getUserName());
            map.put("topicName", publish.getTopicName());
            if (!msg.isRetain()) map.put("packetId", String.valueOf(publish.getPacketId()));
            if (publish.getPayload() != null && publish.getPayload().length > 0) try {
                map.put("payload", new String(publish.getPayload(), "ISO-8859-1"));
            } catch (UnsupportedEncodingException ignore) {
            }
            return map;
        } else if (msg.getMessageType() == MqttMessageType.PUBREL) {
            PacketId packetId = (PacketId) msg.getPayload();
            map.put("type", String.valueOf(MqttMessageType.PUBREL.value()));
            map.put("version", msg.getVersion().toString());
            map.put("clientId", msg.getClientId());
            map.put("userName", msg.getUserName());
            map.put("packetId", String.valueOf(packetId.getPacketId()));
            return map;
        } else {
            throw new IllegalArgumentException("Invalid in-flight MQTT message type: " + msg.getMessageType());
        }
    }

    /**
     * Convert Map to Array
     *
     * @param map Map
     * @return Array of Key Value pair
     */
    public static String[] mapToArray(Map<String, String> map) {
        List<String> l = new ArrayList<>();
        map.forEach((k, v) -> {
            l.add(k);
            l.add(v);
        });
        return l.toArray(new String[l.size()]);
    }
}

@@ -4637,371 +4637,7 @@
    %7D
-%0A%0A    /**%0A     * Convert Map to Array%0A     *%0A     * @param map Map%0A     * @return Array of Key Value pair%0A     */%0A    public static String%5B%5D mapToArray(Map%3CString, String%3E map) %7B%0A        List%3CString%3E l = new ArrayList%3C%3E();%0A        map.forEach((k, v) -%3E %7B%0A            l.add(k);%0A            l.add(v);%0A        %7D);%0A        return l.toArray(new String%5Bl.size()%5D);%0A    %7D
 %0A%7D%0A

Remove unused method;