/*-
 *  Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart
 * 
 *  This file is part of CityDoctor2.
 *
 *  CityDoctor2 is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CityDoctor2 is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with CityDoctor2.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.mapper;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.citygml4j.model.citygml.waterbody.WaterBody;
import org.citygml4j.model.gml.geometry.aggregates.MultiSurface;
import org.citygml4j.model.gml.geometry.aggregates.MultiSurfaceProperty;
import org.citygml4j.model.gml.geometry.primitives.Coord;
import org.citygml4j.model.gml.geometry.primitives.Exterior;
import org.citygml4j.model.gml.geometry.primitives.LinearRing;
import org.citygml4j.model.gml.geometry.primitives.Polygon;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import de.hft.stuttgart.citydoctor2.datastructure.CityDoctorModel;
import de.hft.stuttgart.citydoctor2.datastructure.Edge;
import de.hft.stuttgart.citydoctor2.datastructure.Geometry;
import de.hft.stuttgart.citydoctor2.datastructure.Vertex;
import de.hft.stuttgart.citydoctor2.datastructure.WaterObject;
import de.hft.stuttgart.citydoctor2.parser.ParserConfiguration;

/**
 * 
 * @author Matthias Betz
 *
 */
public class FeatureMapperTest {

	@Rule
	public TemporaryFolder folder = new TemporaryFolder();

	@Test
	public void testNeighboringVertices() throws IOException {
		WaterBody body = new WaterBody();
		Polygon p = new Polygon();
		LinearRing ring = new LinearRing();
		Coord coord = createCoord(0, 0.0000000049, 0);
		ring.addCoord(coord);
		p.setExterior(new Exterior(ring));

		Polygon p2 = new Polygon();
		LinearRing ring2 = new LinearRing();
		coord = createCoord(0, 0.0000000050, 0);
		ring2.addCoord(coord);
		p2.setExterior(new Exterior(ring2));
		MultiSurface ms = new MultiSurface(p, p2);
		body.setLod1MultiSurface(new MultiSurfaceProperty(ms));

		ParserConfiguration config = new ParserConfiguration(8, false);
		FeatureMapper mapper = new FeatureMapper(config, folder.newFile());
		mapper.visit(body);

		CityDoctorModel model = mapper.getModel();
		assertEquals(1, model.getWater().size());
		WaterObject waterObject = model.getWater().get(0);
		Geometry geometry = waterObject.getGeometries().get(0);
		assertEquals(2, geometry.getVertices().size());
		Vertex vertex = geometry.getVertices().get(0);
		assertEquals(1, vertex.getNeighbors().size());
		Vertex neighbor = vertex.getNeighbors().get(0);
		if (vertex.equals(new Vertex(0, 0, 0))) {
			assertEquals(new Vertex(0, 0.00000001, 0), neighbor);
		} else {
			assertEquals(new Vertex(0, 0, 0), neighbor);
		}

		vertex = geometry.getVertices().get(1);
		assertEquals(1, vertex.getNeighbors().size());
		neighbor = vertex.getNeighbors().get(0);
		if (vertex.equals(new Vertex(0, 0, 0))) {
			assertEquals(new Vertex(0, 0.00000001, 0), neighbor);
		} else {
			assertEquals(new Vertex(0, 0, 0), neighbor);
		}
	}

	@Test
	public void testNeighboringVertices2() throws IOException {
		WaterBody body = new WaterBody();
		Polygon p = new Polygon();
		LinearRing ring = new LinearRing();
		Coord coord = createCoord(0, 0.0000000049, 0);
		ring.addCoord(coord);

		coord = createCoord(0, 0.0000000150, 0);
		ring.addCoord(coord);
		p.setExterior(new Exterior(ring));
		MultiSurface ms = new MultiSurface(p);
		body.setLod1MultiSurface(new MultiSurfaceProperty(ms));

		ParserConfiguration config = new ParserConfiguration(8, false);
		FeatureMapper mapper = new FeatureMapper(config, folder.newFile());
		mapper.visit(body);

		CityDoctorModel model = mapper.getModel();
		assertEquals(1, model.getWater().size());
		WaterObject waterObject = model.getWater().get(0);
		Geometry geometry = waterObject.getGeometries().get(0);
		assertEquals(2, geometry.getVertices().size());
		Vertex vertex = geometry.getVertices().get(0);
		assertEquals(0, vertex.getNeighbors().size());

	}

	@Test
	public void testNeighboringVerticesWithCompleteGeometry() throws IOException {
		WaterBody body = new WaterBody();
		Polygon p1 = new Polygon();
		LinearRing ring1 = new LinearRing();
		p1.setExterior(new Exterior(ring1));
		ring1.addCoord(createCoord(0, 0, 0));
		ring1.addCoord(createCoord(1, 0, 0));
		ring1.addCoord(createCoord(1, 1, 0));
		ring1.addCoord(createCoord(1, 2, 0));
		ring1.addCoord(createCoord(0, 2, 0));
		ring1.addCoord(createCoord(0, 0, 0));

		Polygon p2 = new Polygon();
		LinearRing ring2 = new LinearRing();
		p2.setExterior(new Exterior(ring2));
		ring2.addCoord(createCoord(1, 0, 0));
		ring2.addCoord(createCoord(2, 0, 0));
		ring2.addCoord(createCoord(2, 2, 0));
		ring2.addCoord(createCoord(1, 2, 0));
		ring2.addCoord(createCoord(1, 1.0000000050, 0));
		ring2.addCoord(createCoord(1, 0, 0));

		MultiSurface ms = new MultiSurface(p1, p2);
		body.setLod1MultiSurface(new MultiSurfaceProperty(ms));

		ParserConfiguration config = new ParserConfiguration(8, false);
		FeatureMapper mapper = new FeatureMapper(config, folder.newFile());
		mapper.visit(body);

		CityDoctorModel model = mapper.getModel();
		assertEquals(1, model.getWater().size());
		WaterObject waterObject = model.getWater().get(0);
		Geometry geometry = waterObject.getGeometries().get(0);
		assertEquals(8, geometry.getVertices().size());
		List<Vertex> neighborVertices = new ArrayList<>();
		for (Vertex v : geometry.getVertices()) {
			if (!v.getNeighbors().isEmpty()) {
				assertEquals(1, v.getNeighbors().size());
				assertEquals(2, v.getAdjacentPolygons(geometry).size());
				assertEquals(2, v.getAdjacentRings(geometry).size());
				neighborVertices.add(v);
			}
		}
		assertEquals(2, neighborVertices.size());
		Vertex v0 = neighborVertices.get(0);
		Vertex v1 = neighborVertices.get(1);
		assertEquals(v0.getNeighbors().get(0), v1);
		assertEquals(v1.getNeighbors().get(0), v0);
		assertEquals(8, geometry.getEdges().size());
		assertNull(geometry.getEdge(v0, v1));
		List<Vertex> ringVertices = geometry.getPolygons().get(0).getExteriorRing().getVertices();
		int pos = -1;
		for (int i = 0; i < ringVertices.size(); i++) {
			if (ringVertices.get(i).equals(v0) || ringVertices.get(i).equals(v1)) {
				pos = i;
				break;
			}
		}
		assertTrue(pos > 0);
		Vertex prev = ringVertices.get(pos - 1);
		Edge e1 = geometry.getEdge(v0, prev);
		Edge e2 = geometry.getEdge(v1, prev);
		Edge e3 = geometry.getEdge(prev, v0);
		Edge e4 = geometry.getEdge(prev, v1);
		assertEquals(e1, e2);
		assertEquals(e1, e3);
		assertEquals(e1, e4);
		assertEquals(e2, e3);
		assertEquals(e2, e4);
		assertEquals(e3, e4);
	}

	private Coord createCoord(double x, double y, double z) {
		Coord coord = new Coord();
		coord.setX(x);
		coord.setY(y);
		coord.setZ(z);
		return coord;
	}

}
