package eu.simstadt.regionchooser;

import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.locationtech.jts.io.ParseException;
import org.osgeo.proj4j.CoordinateReferenceSystem;
import com.ximpleware.NavException;
import com.ximpleware.XPathEvalException;
import com.ximpleware.XPathParseException;


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

	@Test
	void testExtractRegionWithLocalCRS()
			throws IOException, XPathParseException, NavException, XPathEvalException, ParseException {
		String citygml = "DA13_DA14_3D_Buildings_Port_Morris.gml";
		String projectName = "NewYork";
		Path project = TEST_REPOSITORY.resolve(projectName + ".proj");
		String wktPolygon = "POLYGON ((307475.5578835043 70804.63316765877, 307236.984333501 70360.67156492763, 307650.1509142817 70061.64902227426, 307964.8070375744 70462.43265268637, 307627.75063951925 70710.99549733262, 307475.5578835043 70804.63316765877))";
		Path citygmlPath = project.resolve(citygml);

		CoordinateReferenceSystem localCRS = RegionChooserUtils.crsFromCityGMLHeader(citygmlPath);
		StringBuilder sb = RegionExtractor.selectRegionDirectlyFromCityGML(wktPolygon, localCRS.getName(), citygmlPath);
		assertTrue(sb.toString().contains("gml_ZVHMQQ6BZGRT0O3Q6RGXF12BDOV49QIZ58XB"),
				"One weird shaped roof should be inside the region");
	}

	@Test
	void testExtractRegionWithWGS84()
			throws ParseException, IOException, XPathParseException, NavException, XPathEvalException {
		String wgs84WktPolygon = "POLYGON((-73.91140940026597 40.804246732157196,-73.91424181298568 40.80025100302325,-73.90934946374252 40.79755456207104,-73.90561582879378 40.80116062104605,-73.90960695580794 40.80340212653638,-73.91140940026597 40.804246732157196))";
		String citygml = "DA13_DA14_3D_Buildings_Port_Morris.gml";
		String projectName = "NewYork";
		Path project = TEST_REPOSITORY.resolve(projectName + ".proj");
		Path citygmlPath = project.resolve(citygml);

		CoordinateReferenceSystem localCRS = RegionChooserUtils.crsFromCityGMLHeader(citygmlPath);
		String localWktPolygon = RegionChooserUtils.wktPolygonToLocalCRS(wgs84WktPolygon, localCRS);
		StringBuilder sb = RegionExtractor.selectRegionDirectlyFromCityGML(localWktPolygon, localCRS.getName(),
				citygmlPath);
		assertTrue(sb.toString().contains("gml_ZVHMQQ6BZGRT0O3Q6RGXF12BDOV49QIZ58XB"),
				"One weird shaped roof should be inside the region");
	}

	@Test
	void testExtractRegionWithOldCoordinates()
			throws ParseException, IOException, XPathParseException, NavException, XPathEvalException {
		String oldFormatPolygon = "(40.81173171854368,-73.93268437431763)\r\n" +
				"(40.81069231965162,-73.93068165999941)\r\n" +
				"(40.809176499753505,-73.92907948540162)\r\n" +
				"(40.806924362178165,-73.92433018905214)\r\n" +
				"(40.80328613125832,-73.91620488213013)\r\n" +
				"(40.803546011509866,-73.91425938739778)\r\n" +
				"(40.80649124858661,-73.90750737411074)\r\n" +
				"(40.81069231965162,-73.90184254867128)\r\n" +
				"(40.81285771431433,-73.89806599837831)\r\n" +
				"(40.811688409259915,-73.89537663331359)\r\n" +
				"(40.80874340284353,-73.89486164918273)\r\n" +
				"(40.80943635693916,-73.89011235283326)\r\n" +
				"(40.80553839032126,-73.88919682374328)\r\n" +
				"(40.80501864440376,-73.90207142701476)\r\n" +
				"(40.79674547521796,-73.9115128027472)\r\n" +
				"(40.798868011290665,-73.9190086863666)\r\n" +
				"(40.801553568920895,-73.92135472343872)\r\n" +
				"(40.802506484398094,-73.92324299858521)\r\n" +
				"(40.80259311095742,-73.92759175521425)\r\n" +
				"(40.803762576287,-73.92787785576294)\r\n" +
				"(40.806967670606156,-73.93113942192505)\r\n" +
				"(40.80878671206696,-73.93262715473178)\r\n" +
				"(40.81173171854368,-73.93268437431763)\r\n";

		String wgs84WktPolygon = "POLYGON((" +
				Arrays.stream(oldFormatPolygon.replaceAll("\\(", "").replaceAll("\\)", "").split("\\r?\\n")).map(latLon -> {
					String lat = latLon.split(",")[0];
					String lon = latLon.split(",")[1];
					return lon + " " + lat;
				}).collect(Collectors.joining(",")) + "))";
		String citygml = "DA13_DA14_3D_Buildings_Port_Morris.gml";
		String projectName = "NewYork";
		Path project = TEST_REPOSITORY.resolve(projectName + ".proj");
		Path citygmlPath = project.resolve(citygml);

		CoordinateReferenceSystem localCRS = RegionChooserUtils.crsFromCityGMLHeader(citygmlPath);
		String localWktPolygon = RegionChooserUtils.wktPolygonToLocalCRS(wgs84WktPolygon, localCRS);
		StringBuilder sb = RegionExtractor.selectRegionDirectlyFromCityGML(localWktPolygon, localCRS.getName(),
				citygmlPath);

		assertTrue(sb.toString().contains("gml_ZVHMQQ6BZGRT0O3Q6RGXF12BDOV49QIZ58XB"),
				"One weird shaped roof should be inside the region");
	}
}
