package com.lingniu.ingest.protocol.jt1078.downlink;

import org.junit.jupiter.api.Test;

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

class Jt1078CommandsTest {

    @Test
    void realtimePlayEncodesServerPortsChannelAndStreamOptions() {
        var command = Jt1078Commands.realtimePlay("10.1.2.3", 11078, 11079, 2, 1, 0);

        assertThat(command.messageId()).isEqualTo(0x9101);
        assertThat(command.body()).containsExactly(
                0x08, '1', '0', '.', '1', '.', '2', '.', '3',
                0x2B, 0x46,
                0x2B, 0x47,
                0x02,
                0x01,
                0x00);
    }

    @Test
    void resourceSearchEncodesBcdTimeAndQueryOptions() {
        var command = Jt1078Commands.resourceSearch(
                3,
                "2026-06-22 08:09:10",
                "260622180000",
                0x01020304L,
                0x05060708L,
                3,
                2,
                1);

        assertThat(command.messageId()).isEqualTo(0x9205);
        assertThat(command.body()).containsExactly(
                0x03,
                0x26, 0x06, 0x22, 0x08, 0x09, 0x10,
                0x26, 0x06, 0x22, 0x18, 0x00, 0x00,
                0x01, 0x02, 0x03, 0x04,
                0x05, 0x06, 0x07, 0x08,
                0x03,
                0x02,
                0x01);
    }

    @Test
    void queryMediaAttributesUses1003RequestMessageIdWithEmptyBody() {
        var command = Jt1078Commands.queryMediaAttributes();

        assertThat(command.messageId()).isEqualTo(0x1003);
        assertThat(command.body()).isEmpty();
    }

    @Test
    void realtimeControlEncodesChannelCommandCloseTypeAndStreamType() {
        var command = Jt1078Commands.realtimeControl(2, 1, 0, 1);

        assertThat(command.messageId()).isEqualTo(0x9102);
        assertThat(command.body()).containsExactly(0x02, 0x01, 0x00, 0x01);
    }

    @Test
    void historyPlayEncodesPlaybackOptionsAndBcdTimes() {
        var command = Jt1078Commands.historyPlay(
                "media.example.cn", 11078, 0, 4,
                3, 2, 1, 0, 0,
                "260622080910", "260622180000");

        assertThat(command.messageId()).isEqualTo(0x9201);
        assertThat(command.body()).containsExactly(
                0x10, 'm', 'e', 'd', 'i', 'a', '.', 'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'n',
                0x2B, 0x46,
                0x00, 0x00,
                0x04,
                0x03,
                0x02,
                0x01,
                0x00,
                0x00,
                0x26, 0x06, 0x22, 0x08, 0x09, 0x10,
                0x26, 0x06, 0x22, 0x18, 0x00, 0x00);
    }

    @Test
    void historyControlEncodesPlaybackTime() {
        var command = Jt1078Commands.historyControl(4, 5, 0, "260622090000");

        assertThat(command.messageId()).isEqualTo(0x9202);
        assertThat(command.body()).containsExactly(
                0x04, 0x05, 0x00,
                0x26, 0x06, 0x22, 0x09, 0x00, 0x00);
    }

    @Test
    void fileUploadEncodesServerAccountPathAndQueryOptions() {
        var command = Jt1078Commands.fileUpload(
                "10.1.2.3", 11078, "user", "pass", "/data/upload",
                2, "260622080910", "260622180000",
                1, 2, 3, 2, 1, 0x07);

        assertThat(command.messageId()).isEqualTo(0x9206);
        assertThat(command.body()).containsExactly(
                0x08, '1', '0', '.', '1', '.', '2', '.', '3',
                0x2B, 0x46,
                0x04, 'u', 's', 'e', 'r',
                0x04, 'p', 'a', 's', 's',
                0x0C, '/', 'd', 'a', 't', 'a', '/', 'u', 'p', 'l', 'o', 'a', 'd',
                0x02,
                0x26, 0x06, 0x22, 0x08, 0x09, 0x10,
                0x26, 0x06, 0x22, 0x18, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x01,
                0x00, 0x00, 0x00, 0x02,
                0x03,
                0x02,
                0x01,
                0x07);
    }

    @Test
    void fileUploadControlEncodesResponseSerialAndCommand() {
        var command = Jt1078Commands.fileUploadControl(0x1234, 2);

        assertThat(command.messageId()).isEqualTo(0x9207);
        assertThat(command.body()).containsExactly(0x12, 0x34, 0x02);
    }
}
