package com.imdroid.ntripproxy.executor;

import com.imdroid.ntripproxy.message.*;
import io.netty.buffer.ByteBuf;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Layton
 * @date 2023/2/2 20:41
 */
public class MessageParser {

    private static final Map<Short, Class<? extends NtripMessage>> types = new HashMap<>();

    public static final MessageParser instance = new MessageParser();

    private MessageParser() {

    }

    static {
        types.put((short)0xd331, D331RtcmMessage.class);
        types.put((short)0xd333, D333RtcmMessage.class);
        types.put((short)0xd341, D341GgaMessage.class);
    }


    public NtripMessage parse(ByteBuf src) throws Exception {
        short flag = src.getShort(0); // msg flag
        Class<? extends NtripMessage> clz = types.get(flag);
        if (clz == null) {
            throw new UnSupportedMessageException();
        }
        NtripMessage message = clz.newInstance();
        message.decode(src);
        return message;
    }
}
