package eu.simstadt.geo.fast_xml_parser;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.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 (BuildingXmlNode buildingXmlNode : buildingXmlNodes) {
			assertTrue("Buildings should have coordinates", buildingXmlNode.hasCoordinates());
			assertFalse(COORDINATE_SHOULD_BE_A_DOUBLE, Double.isNaN(buildingXmlNode.x));
			assertFalse(COORDINATE_SHOULD_BE_A_DOUBLE, Double.isNaN(buildingXmlNode.y));
			assertFalse(COORDINATE_SHOULD_BE_A_DOUBLE, Double.isNaN(buildingXmlNode.xMax));
			assertFalse(COORDINATE_SHOULD_BE_A_DOUBLE, Double.isNaN(buildingXmlNode.yMax));
			assertFalse(COORDINATE_SHOULD_BE_A_DOUBLE, Double.isNaN(buildingXmlNode.xMin));
			assertFalse(COORDINATE_SHOULD_BE_A_DOUBLE, Double.isNaN(buildingXmlNode.yMin));
			assertTrue(COORDINATES_SHOULD_BE_PLAUSIBLE, buildingXmlNode.xMax > buildingXmlNode.x);
			assertTrue(COORDINATES_SHOULD_BE_PLAUSIBLE, buildingXmlNode.yMax > buildingXmlNode.y);
			assertTrue(COORDINATES_SHOULD_BE_PLAUSIBLE, buildingXmlNode.xMin < buildingXmlNode.x);
			assertTrue(COORDINATES_SHOULD_BE_PLAUSIBLE, buildingXmlNode.yMin < buildingXmlNode.y);
		}
	}

	@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("Empty Buildings shouldn't have coordinates", buildingXmlNode.hasCoordinates());
			assertTrue("Coordinate should be a Nan", Double.isNaN(buildingXmlNode.x));
			assertTrue("Coordinate should be a Nan", Double.isNaN(buildingXmlNode.y));
			counter++;
		}
		assertEquals("3 buildings should have been analyzed", counter, 3);
	}
}
