/*-
 *  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.datastructure;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

import org.citygml4j.factory.GMLGeometryFactory;
import org.citygml4j.model.citygml.building.AbstractBoundarySurface;
import org.citygml4j.model.citygml.building.WallSurface;
import org.junit.Test;

import de.hft.stuttgart.citydoctor2.datastructure.LinearRing.LinearRingType;
import de.hft.stuttgart.citydoctor2.parser.ParserConfiguration;

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

	@Test
	public void testCreate() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD0);
		
		assertNotNull(geom);
		assertSame(GeometryType.SOLID, geom.getType());
		assertSame(Lod.LOD0, geom.getLod());
	}
	
	@Test
	public void testAddPolygon() {
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD0);
		
		Polygon p = new ConcretePolygon();
		geom.addPolygon(p);
		
		assertEquals(1, geom.getPolygons().size());
		assertSame(geom, p.getParent());
		assertSame(geom.getPolygons().get(0), p);
	}
	
	@Test
	public void testRemovePolygon() {
		ParserConfiguration config = new ParserConfiguration(4, false);
		AbstractBoundarySurface abs = new WallSurface();
		BoundarySurface bs = new BoundarySurface(SurfaceFeatureType.BUILDING, BoundarySurfaceType.WALL, abs);
		org.citygml4j.model.citygml.building.BuildingInstallation gmlBi = new org.citygml4j.model.citygml.building.BuildingInstallation();
		BuildingInstallation bi = new BuildingInstallation();
		bi.setGmlObject(gmlBi);
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD2);

		Polygon p = new ConcretePolygon();
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		p.setExteriorRing(lr);
		lr.addVertex(new Vertex(0, 0, 0));
		
		geom.addPolygon(p);
		p.setPartOfSurface(bs);
		p.setPartOfInstallation(bi);
		
		geom.removePolygon(p);
		
		assertEquals(0, geom.getPolygons().size());
		
		bs.reCreateGeometries(new GMLGeometryFactory(), config);
		assertNull(abs.getLod2MultiSurface());
		
		bi.reCreateGeometries(new GMLGeometryFactory(), config);
		assertNull(gmlBi.getLod2Geometry());
	}
	
	@Test
	public void testReplacePolygon() {
		ParserConfiguration config = new ParserConfiguration(4, false);
		AbstractBoundarySurface abs = new WallSurface();
		BoundarySurface bs = new BoundarySurface(SurfaceFeatureType.BUILDING, BoundarySurfaceType.WALL, abs);
		org.citygml4j.model.citygml.building.BuildingInstallation gmlBi = new org.citygml4j.model.citygml.building.BuildingInstallation();
		BuildingInstallation bi = new BuildingInstallation();
		bi.setGmlObject(gmlBi);
		bi.addBoundarySurface(bs);
		Geometry geom2 = new Geometry(GeometryType.MULTI_SURFACE, Lod.LOD2);
		ConcretePolygon p = new ConcretePolygon();
		geom2.addPolygon(p);
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		p.setExteriorRing(lr);
		lr.addVertex(new Vertex(0, 0, 0));
		p.setPartOfSurface(bs);
		p.setPartOfInstallation(bi);
		bs.addGeometry(geom2);
		
		
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD2);
		Polygon linkedPoly = new LinkedPolygon(p, geom);
		geom.addPolygon(linkedPoly);
		
		ConcretePolygon p2 = new ConcretePolygon();
		p2.setExteriorRing(lr);

		ConcretePolygon p3 = new ConcretePolygon();
		p3.setExteriorRing(lr);
		
		geom.replacePolygon(linkedPoly, p2, p3);
		
		assertEquals(2, geom.getPolygons().size());
		assertEquals(2, geom2.getPolygons().size());
		
		bi.reCreateGeometries(new GMLGeometryFactory(), config);
		assertNull(gmlBi.getLod2Geometry());
		assertNull(gmlBi.getLod3Geometry());
		assertNull(gmlBi.getLod4Geometry());
		
		bs.reCreateGeometries(new GMLGeometryFactory(), config);
		assertNotNull(abs.getLod2MultiSurface());
		assertNotNull(abs.getLod2MultiSurface().getMultiSurface());
		assertNotNull(abs.getLod2MultiSurface().getMultiSurface().getSurfaceMember());
		assertFalse(abs.getLod2MultiSurface().getMultiSurface().getSurfaceMember().isEmpty());
		assertEquals(2, abs.getLod2MultiSurface().getMultiSurface().getSurfaceMember().size());
		assertNull(gmlBi.getLod2Geometry());
	}

}
