package eu.simstadt.regionchooser;

import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.AfterEach; // JUnit 5
import org.junit.jupiter.api.BeforeEach; // JUnit 5
import org.junit.jupiter.api.Test;
import picocli.CommandLine;


class RegionChooserCommandLineInterfaceTest
{
	final PrintStream originalOut = System.out;
	final PrintStream originalErr = System.err;
	final ByteArrayOutputStream out = new ByteArrayOutputStream();
	final ByteArrayOutputStream err = new ByteArrayOutputStream();

	private static final Path TEST_REPOSITORY = Paths.get("src/test/resources/testdata/");

	@BeforeEach
	public void setUpStreams() {
		out.reset();
		err.reset();
		System.setOut(new PrintStream(out));
		System.setErr(new PrintStream(err));
	}

	@AfterEach
	public void restoreStreams() {
		System.setOut(originalOut);
		System.setErr(originalErr);
	}

	@Test
	void testNoInput() {
		new CommandLine(new RegionChooserCommandLineInterface()).execute("");
		originalOut.println(err.toString());
		String expectedErr = "Missing required options: '--input=input.gml', '--output=output.gml', '--wkt=polygon.wkt'";
		assertTrue(err.toString().contains(expectedErr), err.toString() + " should contain " + expectedErr);
	}

	@Test
	void testExtractRegionFromTwoCitygmls() throws IOException {
		String wktPolygon = "POLYGON((3512984.7003764412 5405148.310572891,3513038.6360455155 5405010.072163861,3513142.7277745553 5405004.02571992,3514204.1661769524 5405563.192081669,3514399.2818417274 5405720.905457244,3514291.6158155007 5405896.706492759,3512984.7003764412 5405148.310572891))";
		Path citygml1 = TEST_REPOSITORY.resolve("Stuttgart.proj/Stuttgart_LOD0_LOD1_small.gml");
		Path citygml2 = TEST_REPOSITORY.resolve("Stuttgart.proj/Stöckach_überarbeitete GML-NoBuildingPart.gml");
		Path outGML = Files.createTempFile("output", ".gml");
		Path inWKT = Files.createTempFile("polygon", ".wkt");
		try (BufferedWriter wkt = Files.newBufferedWriter(inWKT)) {
			wkt.write(wktPolygon);
		}
		new CommandLine(new RegionChooserCommandLineInterface()).execute("--input=" + citygml1 + "," + citygml2,
				"--output=" + outGML, "--wkt=" + inWKT, "--epsg=31463", "--local");
		String expectedErr = "Buildings found in selected region 20";
		assertTrue(err.toString().contains(expectedErr), err.toString() + " should contain " + expectedErr);
		originalOut.println(Files.size(outGML));
		assertTrue(Files.size(outGML) > 600_000);
	}
}

