/*-
 *  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.fail;
import static org.mockito.Mockito.mock;

import java.io.File;
import java.io.IOException;

import org.citygml4j.core.model.vegetation.PlantCover;
import org.citygml4j.core.model.waterbody.WaterBody;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.xmlobjects.gml.model.geometry.DirectPosition;
import org.xmlobjects.gml.model.geometry.GeometricPosition;
import org.xmlobjects.gml.model.geometry.aggregates.MultiSurface;
import org.xmlobjects.gml.model.geometry.aggregates.MultiSurfaceProperty;
import org.xmlobjects.gml.model.geometry.primitives.AbstractRingProperty;
import org.xmlobjects.gml.model.geometry.primitives.LinearRing;
import org.xmlobjects.gml.model.geometry.primitives.Polygon;
import org.xmlobjects.gml.model.geometry.primitives.SurfaceProperty;

import de.hft.stuttgart.citydoctor2.datastructure.CityDoctorModel;
import de.hft.stuttgart.citydoctor2.datastructure.Geometry;
import de.hft.stuttgart.citydoctor2.datastructure.GeometryType;
import de.hft.stuttgart.citydoctor2.datastructure.Lod;
import de.hft.stuttgart.citydoctor2.datastructure.Vertex;
import de.hft.stuttgart.citydoctor2.datastructure.WaterObject;
import de.hft.stuttgart.citydoctor2.mapper.citygml3.Citygml3FeatureMapper;
import de.hft.stuttgart.citydoctor2.parser.ParserConfiguration;

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

	@Rule
	public TemporaryFolder folder = new TemporaryFolder();

	@Test
	public void testVisitWaterBody() {
		WaterBody body = new WaterBody();
		Citygml3FeatureMapper mapper = new Citygml3FeatureMapper(mock(ParserConfiguration.class),
				new File("").toPath());
		mapper.visit(body);
		assertEquals(1, mapper.getModel().getWater().size());
		body.setId("test1");
		mapper.visit(body);
		assertEquals(2, mapper.getModel().getWater().size());
		WaterObject waterObject = mapper.getModel().getWater().get(1);
		assertEquals("test1", waterObject.getGmlId().getGmlString());

		body.getDeprecatedProperties().setLod1MultiSurface(createDummyMsp());
		mapper.visit(body);
		assertEquals(3, mapper.getModel().getWater().size());
		assertNull(body.getDeprecatedProperties().getLod1MultiSurface());
		Geometry geometry = mapper.getModel().getWater().get(2).getGeometries().get(0);
		assertEquals(1, geometry.getPolygons().size());
		assertEquals(Lod.LOD1, geometry.getLod());
		assertEquals(GeometryType.MULTI_SURFACE, geometry.getType());
	}

	@Test
	public void testVisitPlantCover() {
		PlantCover cover = new PlantCover();
		Citygml3FeatureMapper mapper = new Citygml3FeatureMapper(mock(ParserConfiguration.class),
				new File("").toPath());
		mapper.visit(cover);
	}

	private MultiSurfaceProperty createDummyMsp() {
		LinearRing ext = new LinearRing();
		ext.getControlPoints().getGeometricPositions().add(createCoord(0, 0, 0));
		ext.getControlPoints().getGeometricPositions().add(createCoord(10, 0, 0));
		ext.getControlPoints().getGeometricPositions().add(createCoord(10, 10, 0));
		ext.getControlPoints().getGeometricPositions().add(createCoord(0, 10, 0));
		ext.getControlPoints().getGeometricPositions().add(createCoord(0, 0, 0));
		Polygon p = new Polygon();
		p.setExterior(new AbstractRingProperty(ext));
		MultiSurface ms = new MultiSurface();
		ms.getSurfaceMember().add(new SurfaceProperty(p));
		return new MultiSurfaceProperty(ms);
	}

	@Test
	public void testNeighboringVertices() throws IOException {
		WaterBody body = new WaterBody();
		Polygon p = new Polygon();
		LinearRing ring = new LinearRing();
		GeometricPosition coord = createCoord(0, 0.0000000049, 0);
		ring.getControlPoints().getGeometricPositions().add(coord);
		p.setExterior(new AbstractRingProperty(ring));

		Polygon p2 = new Polygon();
		LinearRing ring2 = new LinearRing();
		coord = createCoord(0, 0.0000000050, 0);
		ring2.getControlPoints().getGeometricPositions().add(coord);
		p2.setExterior(new AbstractRingProperty(ring2));
		MultiSurface ms = new MultiSurface();
		ms.getSurfaceMember().add(new SurfaceProperty(p));
		ms.getSurfaceMember().add(new SurfaceProperty(p2));
		body.getDeprecatedProperties().setLod1MultiSurface(new MultiSurfaceProperty(ms));

		ParserConfiguration config = new ParserConfiguration(8, false);
		Citygml3FeatureMapper mapper = new Citygml3FeatureMapper(config, folder.newFile().toPath());
		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(1, geometry.getVertices().size());
		Vertex vertex = geometry.getVertices().get(0);
		assertEquals(new Vertex(0, 0, 0), vertex);
	}

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

		coord = createCoord(0, 0.0000000150, 0);
		ring.getControlPoints().getGeometricPositions().add(coord);
		p.setExterior(new AbstractRingProperty(ring));
		MultiSurface ms = new MultiSurface();
		ms.getSurfaceMember().add(new SurfaceProperty(p));
		body.getDeprecatedProperties().setLod1MultiSurface(new MultiSurfaceProperty(ms));

		ParserConfiguration config = new ParserConfiguration(8, false);
		Citygml3FeatureMapper mapper = new Citygml3FeatureMapper(config, folder.newFile().toPath());
		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);
		if (vertex.equals(new Vertex(0, 0, 0))) {
			assertEquals(new Vertex(0, 0.00000002, 0), geometry.getVertices().get(1));
		} else if (vertex.equals(new Vertex(0, 0.00000002, 0))) {
			assertEquals(new Vertex(0, 0, 0), geometry.getVertices().get(1));
		} else {
			fail("Did not find both vertices again");
		}
	}

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

		Polygon p2 = new Polygon();
		LinearRing ring2 = new LinearRing();
		p2.setExterior(new AbstractRingProperty(ring2));
		ring2.getControlPoints().getGeometricPositions().add(createCoord(1, 0, 0));
		ring2.getControlPoints().getGeometricPositions().add(createCoord(2, 0, 0));
		ring2.getControlPoints().getGeometricPositions().add(createCoord(2, 2, 0));
		ring2.getControlPoints().getGeometricPositions().add(createCoord(1, 2, 0));
		ring2.getControlPoints().getGeometricPositions().add(createCoord(1, 1.0000000050, 0));
		ring2.getControlPoints().getGeometricPositions().add(createCoord(1, 0, 0));

		MultiSurface ms = new MultiSurface();
		ms.getSurfaceMember().add(new SurfaceProperty(p1));
		ms.getSurfaceMember().add(new SurfaceProperty(p2));
		body.getDeprecatedProperties().setLod1MultiSurface(new MultiSurfaceProperty(ms));

		ParserConfiguration config = new ParserConfiguration(8, false);
		Citygml3FeatureMapper mapper = new Citygml3FeatureMapper(config, folder.newFile().toPath());
		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(7, geometry.getVertices().size());
		assertEquals(8, geometry.getEdges().size());
	}

	private GeometricPosition createCoord(double x, double y, double z) {
		return new GeometricPosition(new DirectPosition(x, y, z));
	}

}
