package eu.simstadt.regionchooser.fast_xml_parser;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
import com.ximpleware.XPathParseException;
import eu.simstadt.regionchooser.fast_xml_parser.BuildingXmlNode;
import eu.simstadt.regionchooser.fast_xml_parser.CityGmlIterator;


public class CitygmlParserTests
{
	private static final String REGION_CHOOSER_TESTDATA = "src/test/resources/testdata";
	private static final String COORDINATES_SHOULD_BE_PLAUSIBLE = "Min/Max Coordinates should be plausible";
	private static final String COORDINATE_SHOULD_BE_A_DOUBLE = "Coordinate should be a double";

	private void testNoNanInCoordinates(Path citygmlPath) throws XPathParseException {
		CityGmlIterator buildingXmlNodes = new CityGmlIterator(citygmlPath);
		for (BuildingXmlNode buildingXmlNode : buildingXmlNodes) {
			assertTrue(buildingXmlNode.hasCoordinates(), "Buildings should have coordinates");
			assertFalse(Double.isNaN(buildingXmlNode.x), COORDINATE_SHOULD_BE_A_DOUBLE);
			assertFalse(Double.isNaN(buildingXmlNode.y), COORDINATE_SHOULD_BE_A_DOUBLE);
			assertFalse(Double.isNaN(buildingXmlNode.xMax), COORDINATE_SHOULD_BE_A_DOUBLE);
			assertFalse(Double.isNaN(buildingXmlNode.yMax), COORDINATE_SHOULD_BE_A_DOUBLE);
			assertFalse(Double.isNaN(buildingXmlNode.xMin), COORDINATE_SHOULD_BE_A_DOUBLE);
			assertFalse(Double.isNaN(buildingXmlNode.yMin), COORDINATE_SHOULD_BE_A_DOUBLE);
			assertTrue(buildingXmlNode.xMax > buildingXmlNode.x, COORDINATES_SHOULD_BE_PLAUSIBLE);
			assertTrue(buildingXmlNode.yMax > buildingXmlNode.y, COORDINATES_SHOULD_BE_PLAUSIBLE);
			assertTrue(buildingXmlNode.xMin < buildingXmlNode.x, COORDINATES_SHOULD_BE_PLAUSIBLE);
			assertTrue(buildingXmlNode.yMin < buildingXmlNode.y, COORDINATES_SHOULD_BE_PLAUSIBLE);
		}
	}

	@Test
	public void testExtractCoordsFromStuttgart() throws XPathParseException {
		Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Stuttgart.proj");
		Path citygmlPath = repo.resolve("Stuttgart_LOD0_LOD1_buildings_and_trees.gml");
		testNoNanInCoordinates(citygmlPath);
	}

	@Test
	public void testExtractCoordsFromGruenbuehl() throws XPathParseException {
		Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Gruenbuehl.proj");
		Path citygmlPath = repo.resolve("20140218_Gruenbuehl_LOD2_1building.gml");
		testNoNanInCoordinates(citygmlPath);
	}

	@Test
	public void testExtractCoordsFromMunich() throws XPathParseException {
		Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Muenchen.proj");
		Path citygmlPath = repo.resolve("Munich_v_1_0_0.gml");
		testNoNanInCoordinates(citygmlPath);
	}

	@Test
	public void testExtractCoordsFromNYC() throws XPathParseException {
		Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "NewYork.proj");
		Path citygmlPath = repo.resolve("ManhattanSmall.gml");
		testNoNanInCoordinates(citygmlPath);
	}

	@Test
	public void testExtractNoCoordsFromEmptyBuilding() throws XPathParseException {
		Path repo = Paths.get(REGION_CHOOSER_TESTDATA, "Stuttgart.proj");
		Path citygmlPath = repo.resolve("Stöckach_empty_buildings.gml");
		CityGmlIterator buildingXmlNodes = new CityGmlIterator(citygmlPath);
		int counter = 0;
		for (BuildingXmlNode buildingXmlNode : buildingXmlNodes) {
			assertFalse(buildingXmlNode.hasCoordinates(), "Empty Buildings shouldn't have coordinates");
			assertTrue(Double.isNaN(buildingXmlNode.x), "Coordinate should be a Nan");
			assertTrue(Double.isNaN(buildingXmlNode.y), "Coordinate should be a Nan");
			counter++;
		}
		assertEquals(3, counter, "3 buildings should have been analyzed");
	}
}
