package com.imdroid.sideslope.executor;

import com.imdroid.sideslope.exception.UnSupportedMessageException;
import com.imdroid.sideslope.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 BaseMessage>> types = new HashMap<>();

    public static final MessageParser instance = new MessageParser();

    private MessageParser() {

    }

    static {
        types.put((short)0xd3f0, D3F0SelfCheckMessage.class);
        types.put((short)0xd3f2, D3F2StopIndicationMessage.class);
        //types.put((short)0xd330, D330RtcmMessage.class);
        //types.put((short)0xd340, D340LocationMessage.class);
        types.put((short)0xd331, D331RtcmMessage.class);
        types.put((short)0xd341, D341LocationMessage.class);
        types.put((short)0xd342, D342LocationMessage.class);
        types.put((short)0xd310, D31xConfigAckMessage.class);//GNSS配置应答
        types.put((short)0xd311, D31xConfigAckMessage.class);//单片机时间配置应答
        types.put((short)0xd312, D31xConfigAckMessage.class);//单片机通用参数配置应答
        types.put((short)0xd313, D31xConfigAckMessage.class);//单片机DEBUG应答
        types.put((short)0xd31A, D31xConfigAckMessage.class);//DTU配置应答
        types.put((short)0xd31B, D31xConfigAckMessage.class);//LORA配置应答
        types.put((short)0xd350, D350TestMessage.class);//ACC上报
    }


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