package com.lingniu.ingest.protocol.jt1078;

import com.lingniu.ingest.protocol.jt1078.codec.parser.FileUploadCompleteBodyParser;
import com.lingniu.ingest.protocol.jt1078.codec.parser.FileListBodyParser;
import com.lingniu.ingest.protocol.jt1078.codec.parser.MediaAttributesBodyParser;
import com.lingniu.ingest.protocol.jt1078.codec.parser.PassengerVolumeBodyParser;
import com.lingniu.ingest.protocol.jt808.model.Jt808Body;
import com.lingniu.ingest.protocol.jt808.model.Jt808Header;
import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;

import static org.assertj.core.api.Assertions.assertThat;

class Jt1078ParserTest {

    private final Jt808Header header = new Jt808Header(
            0, 0, 0, false, Jt808Header.ProtocolVersion.V2013, "13800138000", 1, 0, 0);

    @Test
    void parsesMediaAttributesAsSemanticBody() {
        byte[] raw = new byte[]{1, 2, 3, 4, 0x01, 0x40, 1, 98, 4, 8};

        Jt808Body body = new MediaAttributesBodyParser().parse(header, ByteBuffer.wrap(raw));

        assertThat(body).isInstanceOf(Jt808Body.MediaAttributes.class);
        Jt808Body.MediaAttributes attrs = (Jt808Body.MediaAttributes) body;
        assertThat(attrs.inputAudioEncoding()).isEqualTo(1);
        assertThat(attrs.inputAudioChannels()).isEqualTo(2);
        assertThat(attrs.inputAudioSampleRate()).isEqualTo(3);
        assertThat(attrs.inputAudioBitDepth()).isEqualTo(4);
        assertThat(attrs.audioFrameLength()).isEqualTo(320);
        assertThat(attrs.audioOutputSupported()).isTrue();
        assertThat(attrs.videoEncoding()).isEqualTo(98);
        assertThat(attrs.maxAudioChannels()).isEqualTo(4);
        assertThat(attrs.maxVideoChannels()).isEqualTo(8);
        assertThat(attrs.raw()).containsExactly(raw);
    }

    @Test
    void parsesFileUploadCompleteAsSemanticBody() {
        byte[] raw = new byte[]{0x12, 0x34, 1, 2, 3, 4};

        Jt808Body body = new FileUploadCompleteBodyParser().parse(header, ByteBuffer.wrap(raw));

        assertThat(body).isInstanceOf(Jt808Body.FileUploadComplete.class);
        Jt808Body.FileUploadComplete done = (Jt808Body.FileUploadComplete) body;
        assertThat(done.responseSerialNo()).isEqualTo(0x1234);
        assertThat(done.result()).isEqualTo(1);
        assertThat(done.raw()).containsExactly(raw);
    }

    @Test
    void parsesFileListSummaryAsSemanticBody() {
        byte[] raw = new byte[]{0x12, 0x34, 0x00, 0x00, 0x00, 0x02, 9, 8, 7};

        Jt808Body body = new FileListBodyParser().parse(header, ByteBuffer.wrap(raw));

        assertThat(body).isInstanceOf(Jt808Body.FileList.class);
        Jt808Body.FileList list = (Jt808Body.FileList) body;
        assertThat(list.responseSerialNo()).isEqualTo(0x1234);
        assertThat(list.fileCount()).isEqualTo(2);
        assertThat(list.raw()).containsExactly(raw);
    }

    @Test
    void parsesPassengerVolumeAsSemanticBody() {
        byte[] raw = new byte[]{
                0x02,
                0x26, 0x06, 0x22, 0x15, 0x30, 0x00,
                0x26, 0x06, 0x22, 0x16, 0x00, 0x00,
                0x00, 0x15,
                0x00, 0x03
        };

        Jt808Body body = new PassengerVolumeBodyParser().parse(header, ByteBuffer.wrap(raw));

        assertThat(body).isInstanceOf(Jt808Body.PassengerVolume.class);
        Jt808Body.PassengerVolume passengerVolume = (Jt808Body.PassengerVolume) body;
        assertThat(passengerVolume.channelId()).isEqualTo(2);
        assertThat(passengerVolume.startTime()).isEqualTo("2026-06-22T15:30:00");
        assertThat(passengerVolume.endTime()).isEqualTo("2026-06-22T16:00:00");
        assertThat(passengerVolume.passengerGetOn()).isEqualTo(21);
        assertThat(passengerVolume.passengerGetOff()).isEqualTo(3);
        assertThat(passengerVolume.raw()).containsExactly(raw);
    }
}
