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;


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 (CityObjectMember cityObjectNode : buildingXmlNodes) {
			assertTrue(cityObjectNode.hasCoordinates(), "Building and vegetations should have coordinates");
			assertFalse(Double.isNaN(cityObjectNode.x), COORDINATE_SHOULD_BE_A_DOUBLE);
			assertFalse(Double.isNaN(cityObjectNode.y), COORDINATE_SHOULD_BE_A_DOUBLE);
			assertFalse(Double.isNaN(cityObjectNode.xMax), COORDINATE_SHOULD_BE_A_DOUBLE);
			assertFalse(Double.isNaN(cityObjectNode.yMax), COORDINATE_SHOULD_BE_A_DOUBLE);
			assertFalse(Double.isNaN(cityObjectNode.xMin), COORDINATE_SHOULD_BE_A_DOUBLE);
			assertFalse(Double.isNaN(cityObjectNode.yMin), COORDINATE_SHOULD_BE_A_DOUBLE);
			// Some SolitaryVegetationObjects are defined with a single point.
			assertTrue(cityObjectNode.xMax >= cityObjectNode.x, COORDINATES_SHOULD_BE_PLAUSIBLE);
			assertTrue(cityObjectNode.yMax >= cityObjectNode.y, COORDINATES_SHOULD_BE_PLAUSIBLE);
			assertTrue(cityObjectNode.xMin <= cityObjectNode.x, COORDINATES_SHOULD_BE_PLAUSIBLE);
			assertTrue(cityObjectNode.yMin <= cityObjectNode.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);
		CityGmlIterator buildingXmlNodes = new CityGmlIterator(citygmlPath);
		boolean foundBuildings = false;
		boolean foundTrees = false;
		for (CityObjectMember cityObjectNode : buildingXmlNodes) {
			boolean isBuilding = cityObjectNode.isBuilding();
			foundBuildings |= isBuilding;
			foundTrees |= !isBuilding;
		}
		assertTrue(foundBuildings, "At least one building should have been found.");
		assertTrue(foundTrees, "At least one tree should have been found.");
	}

	@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 (CityObjectMember 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");
	}
}
