package com.njzscloud.common.localizer.tuqiang.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.njzscloud.common.localizer.jt808.message.TerminalMessageBody;
import com.njzscloud.common.localizer.jt808.message.TerminalTxtMessage;
import com.njzscloud.common.localizer.mqtt.result.RealtimeLocationResult;
import com.njzscloud.common.localizer.tuqiang.message.LocationReportMessage;
import com.njzscloud.common.localizer.tuqiang.message.SearchDeviceInfoMessage;
import com.njzscloud.common.localizer.tuqiang.message.SearchLocationResponseMessage;
import com.njzscloud.common.localizer.tuqiang.support.TuqiangListener;
import com.njzscloud.common.mqtt.util.Mqtt;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;

import static com.njzscloud.common.localizer.jt808.support.JT808MessageResolver.*;

@Slf4j
public class TuqiangListeners {
    /**
     * 终端心跳消息
     */
    @TuqiangListener(messageId = Heartbeat)
    public void onHeartbeat(TerminalMessageBody message) {
        String terminalId = message.getTerminalId();
        Mqtt.publish(terminalId + "/online", MapUtil.builder()
                .put("online", true)
                .put("terminalId", terminalId)
                .put("time", DateUtil.now())
                .build());
    }

    /**
     * 终端位置信息汇报消息
     */
    @TuqiangListener(messageId = LocationReport)
    public void onLocationReport(TerminalMessageBody message) {
        ByteBuf byteBuf = message.getBody();
        LocationReportMessage locationReportMsg = new LocationReportMessage(byteBuf);
        // log.info("终端位置信息汇报消息: {}", locationReportMsg);

        String terminalId = message.getTerminalId();
        RealtimeLocationResult realtimeLocationResult = BeanUtil.copyProperties(locationReportMsg, RealtimeLocationResult.class)
                .setTerminalId(terminalId);
        Mqtt.publish(terminalId + "/track_location", realtimeLocationResult);
        Mqtt.publish(terminalId + "/track_location_real", realtimeLocationResult);
    }

    /**
     * 定位数据批量上传消息
     */
    @TuqiangListener(messageId = LocationBatchReport)
    public void onLocationBatchUpload(TerminalMessageBody message) {
        String terminalId = message.getTerminalId();
        ByteBuf body = message.getBody();
        int count = body.readUnsignedShort();
        int type = body.readByte();

        for (int i = 0; i < count; i++) {
            int length = body.readUnsignedShort();
            LocationReportMessage locationReportMsg = new LocationReportMessage(body.slice(body.readerIndex(), length));
            body.skipBytes(length);
            RealtimeLocationResult realtimeLocationResult = BeanUtil.copyProperties(locationReportMsg, RealtimeLocationResult.class)
                    .setTerminalId(terminalId)
                    .setType(type);
            Mqtt.publish(terminalId + "/track_location", realtimeLocationResult);
            Mqtt.publish(terminalId + "/track_location_real", realtimeLocationResult);
        }
    }

    /**
     * 查询位置响应消息
     */
    @TuqiangListener(messageId = LocationSearch)
    public void onSearchLocationResponse(TerminalMessageBody message) {
        String terminalId = message.getTerminalId();
        ByteBuf body = message.getBody();
        SearchLocationResponseMessage searchLocationResponseMsg = new SearchLocationResponseMessage(body);
        // log.info("查询位置响应消息: {}", searchLocationResponseMsg);

        LocationReportMessage locationReportMsg = searchLocationResponseMsg.getLocationReportMessage();
        RealtimeLocationResult realtimeLocationResult = BeanUtil.copyProperties(locationReportMsg, RealtimeLocationResult.class)
                .setTerminalId(terminalId);
        Mqtt.publish(terminalId + "/current_location", realtimeLocationResult);
    }

    /**
     * 终端文本信息汇报消息
     */
    @TuqiangListener(messageId = TxtReport)
    public void onTerminalTxtReport(TerminalMessageBody message) {
        TerminalTxtMessage terminalTxtReportMsg = new TerminalTxtMessage(message.getBody());
        // log.info("终端文本信息汇报消息: {}", terminalTxtReportMsg);
        String txt = terminalTxtReportMsg.getTxt();
        SearchDeviceInfoMessage deviceInfo = SearchDeviceInfoMessage.parse(txt);
        if (deviceInfo != null) {
            String terminalId = message.getTerminalId();
            deviceInfo.setTerminalId(terminalId);

            Mqtt.publish(terminalId + "/device_info", deviceInfo);
        }
    }
}
