package com.njzscloud.common.localizer.jt808.support;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JT808MessageResolver {
    /* public static final String Heartbeat = "Heartbeat";
    public static final String LocationReport = "LocationReport";
    public static final String LocationBatchReport = "LocationBatchReport";
    public static final String LocationSearch = "LocationSearch";
    public static final String TxtReport = "TxtReport";

    private static final Map<String, Consumer<TerminalMessageBody>> resolvers = new ConcurrentHashMap<>();

    private static final Map<Integer, Consumer<JT808Message>> stdResolvers = MapUtil.<Integer, Consumer<JT808Message>>builder()
            .put(0x0100, JT808MessageResolver::onTerminalRegister)
            .put(0x0102, JT808MessageResolver::onTerminalAuth)
            .put(0x0002, JT808MessageResolver::onHeartbeat)
            .put(0x0200, JT808MessageResolver::onLocationReport)
            .put(0x704, JT808MessageResolver::onLocationBatchUpload)
            .put(0x0201, JT808MessageResolver::onSearchLocationResponse)
            .put(0x0001, JT808MessageResolver::onTerminalGeneralResponse)
            .put(0x6006, JT808MessageResolver::onTerminalTxtReport)
            .build();

    public static void addResolver(String messageId, Consumer<TerminalMessageBody> resolver) {
        JT808MessageResolver.resolvers.put(messageId, resolver);
    }

    public static void dispatchMsg(String messageId, JT808Message message) {
        Consumer<TerminalMessageBody> messageBodyConsumer = resolvers.get(messageId);
        if (messageBodyConsumer != null) {
            String terminalPhone = message.getTerminalPhone();
            byte[] messageBody = message.getMessageBody();
            ByteBuf byteBuf = Unpooled.wrappedBuffer(messageBody);
            try {
                messageBodyConsumer.accept(new TerminalMessageBody()
                        .setTerminalId(terminalPhone)
                        .setBody(byteBuf));
            } catch (Exception e) {
                log.error("处理消息时发生异常，消息 Id：{}，设备号：{} ", messageId, terminalPhone, e);
            } finally {
                byteBuf.release();
            }
        }
    }

    public static void dispatchMsg(Integer messageId, JT808Message message) {
        Consumer<JT808Message> stdResolver = stdResolvers.get(messageId);
        if (stdResolver != null) {
            stdResolver.accept(message);
        }
    }

     *//**
     * 终端注册消息
     *//*
    public static void onTerminalRegister(JT808Message message) {
        String terminalPhone = message.getTerminalPhone();
        int flowId = message.getFlowId();
        ServerRegisterReplyMessage serverRegisterReplyMessage = new ServerRegisterReplyMessage(terminalPhone, flowId, 0, terminalPhone);
        JT808.sendMessage(JT808.createBaseMessage(terminalPhone, 0x8100, serverRegisterReplyMessage.toBytes()));

        TerminalRegisterMessage terminalRegisterMessage = message.parseMessageBody(TerminalRegisterMessage.class);

        log.info("{}，终端注册消息: {}", message.getTerminalPhone(), terminalRegisterMessage);
    }

     *//**
     * 终端鉴权消息
     *//*
    public static void onTerminalAuth(JT808Message message) {
        JT808.sendGeneralReply(message);
        TerminalAuthMessage authMsg = message.parseMessageBody(TerminalAuthMessage.class);
        log.info("{}，终端鉴权消息: {}", message.getTerminalPhone(), authMsg);
    }

     *//**
     * 终端通用应答消息
     *//*
    public static void onTerminalGeneralResponse(JT808Message message) {
        TerminalGeneralResponseMessage terminalGeneralResponseMessage = message.parseMessageBody(TerminalGeneralResponseMessage.class);
        log.info("设备通用响应，设备号：{}，消息：{}", message.getTerminalPhone(), terminalGeneralResponseMessage.getResult());
    }

     *//**
     * 终端心跳消息
     *//*
    public static void onHeartbeat(JT808Message message) {
        log.info("{}，终端心跳消息: {}", message.getTerminalPhone(), message);
        JT808.sendGeneralReply(message);
        dispatchMsg(Heartbeat, message);
    }

     *//**
     * 终端位置信息汇报消息
     *//*
    public static void onLocationReport(JT808Message message) {
        log.info("{}，终端位置信息汇报消息: {}", message.getTerminalPhone(), message);
        dispatchMsg(LocationReport, message);
    }

     *//**
     * 定位数据批量上传消息
     *//*
    public static void onLocationBatchUpload(JT808Message message) {
        log.info("{}，定位数据批量上传消息: {}", message.getTerminalPhone(), message);
        dispatchMsg(LocationBatchReport, message);
    }

     *//**
     * 查询位置响应消息
     *//*
    public static void onSearchLocationResponse(JT808Message message) {
        log.info("{}，查询位置响应消息: {}", message.getTerminalPhone(), message);
        JT808.sendGeneralReply(message);
        dispatchMsg(LocationSearch, message);
    }

     *//**
     * 终端文本信息汇报消息
     *//*
    public static void onTerminalTxtReport(JT808Message message) {
        log.info("{}，终端文本信息汇报消息: {}", message.getTerminalPhone(), message);
        JT808.sendGeneralReply(message);
        dispatchMsg(TxtReport, message);
    } */

}
