package com.ai.trackwalker.uavaccess.wayline;

import com.ai.trackwalker.uavaccess.config.UavAccessProperties;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

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

class WpmlKmzCompilerTest {
    @Test
    void buildsDeterministicKmzWithTemplateAndExecutionWayline() throws Exception {
        UavAccessProperties properties = properties();
        WpmlKmzCompiler compiler = new WpmlKmzCompiler(properties);
        WaylineUploadRequest request = new WaylineUploadRequest(
                "connection-1",
                "route-version-1",
                "桥梁巡检",
                List.of(
                        Map.of("longitude", 116.1, "latitude", 39.1, "altitude_m", 60, "speed_mps", 4),
                        Map.of("longitude", 116.2, "latitude", 39.2, "altitude_m", 65, "speed_mps", 5)
                ),
                Map.of("speed_mps", 5, "rth_altitude_m", 100),
                1_700_000_000_000L,
                null
        );

        byte[] first = compiler.compile(request);
        byte[] second = compiler.compile(request);
        Map<String, String> files = unzip(first);

        assertThat(first).isEqualTo(second);
        assertThat(files).containsKeys("wpmz/template.kml", "wpmz/waylines.wpml");
        assertThat(files.get("wpmz/template.kml"))
                .contains("http://www.dji.com/wpmz/1.0.6")
                .contains("<wpml:droneEnumValue>91</wpml:droneEnumValue>")
                .contains("<coordinates>116.1,39.1</coordinates>");
        assertThat(files.get("wpmz/waylines.wpml"))
                .contains("<wpml:executeHeight>60.0</wpml:executeHeight>")
                .contains("<wpml:waypointSpeed>4.0</wpml:waypointSpeed>");
    }

    private UavAccessProperties properties() {
        UavAccessProperties properties = new UavAccessProperties();
        UavAccessProperties.FlightHub flightHub = properties.getFlighthub();
        flightHub.setBaseUrl("https://flighthub.local");
        flightHub.setProjectUuid("00000000-0000-0000-0000-000000000001");
        flightHub.setOrganizationUuid("00000000-0000-0000-0000-000000000002");
        flightHub.setUserToken("secret");
        flightHub.getWpml().setDroneEnumValue("91");
        flightHub.getWpml().setDroneSubEnumValue("0");
        flightHub.getWpml().setPayloadEnumValue("67");
        flightHub.getWpml().setPayloadSubEnumValue("0");
        return properties;
    }

    private Map<String, String> unzip(byte[] kmz) throws Exception {
        Map<String, String> result = new LinkedHashMap<>();
        try (ZipInputStream input = new ZipInputStream(new ByteArrayInputStream(kmz), StandardCharsets.UTF_8)) {
            ZipEntry entry;
            while ((entry = input.getNextEntry()) != null) {
                result.put(entry.getName(), new String(input.readAllBytes(), StandardCharsets.UTF_8));
            }
        }
        return result;
    }
}
