/*-
 *  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.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;

import org.citygml4j.model.citygml.building.RoofSurface;
import org.citygml4j.model.gml.basicTypes.Code;
import org.junit.Test;

import de.hft.stuttgart.citydoctor2.datastructure.LinearRing.LinearRingType;
import de.hft.stuttgart.citydoctor2.utils.Copy;

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

	@Test
	public void testCopy() {
		Building b = new Building();
		b.setGmlId(new GmlId("testId"));
		
		Vertex v1 = new Vertex(1, 0, 0);
		Vertex v2 = new Vertex(2, 0, 0);
		LinearRing lr = new LinearRing(LinearRingType.EXTERIOR);
		ConcretePolygon p = new ConcretePolygon();
		p.setExteriorRing(lr);
		lr.addVertex(v1);
		lr.addVertex(v2);
		lr.addVertex(v1);
		Geometry geom = new Geometry(GeometryType.SOLID, Lod.LOD0);
		geom.addPolygon(p);
		geom.updateEdgesAndVertices();
		b.addGeometry(geom);
		
		b.setCityGmlBuilding(createCityGmlBuilding());
		RoofSurface rs = new RoofSurface();
		rs.setId("testRoof");
		BoundarySurface surface = new BoundarySurface(SurfaceFeatureType.BUILDING, BoundarySurfaceType.ROOF, rs);
		Geometry geomSurface = new Geometry(GeometryType.MULTI_SURFACE, Lod.LOD2);
		geomSurface.addPolygon(new LinkedPolygon(p, geomSurface));
		geomSurface.updateEdgesAndVertices();
		surface.addGeometry(geomSurface);
		b.addBoundarySurface(surface);
		
		Building copy = Copy.copy(b);
		assertNotSame(b, copy);
		
		assertEquals(b.getGmlId(), copy.getGmlId());
		
		Geometry copyGeom = copy.getGeometries().get(0);
		assertNotSame(geom, copyGeom);
		
		Edge edge = geom.getEdges().get(0);
		Edge copyEdge = copyGeom.getEdges().get(0);
		
		assertEquals(edge, copyEdge);
		assertNotSame(edge, copyEdge);
		
		Polygon copyPolygon = copyGeom.getPolygons().get(0);
		assertNotSame(p, copyPolygon);
		
		LinearRing copyRing = copyPolygon.getExteriorRing();
		assertNotSame(lr, copyRing);
		
		BoundarySurface copyBs = copy.getBoundarySurfaces().get(0);
		assertNotSame(surface, copyBs);
		assertEquals(surface.getType(), copyBs.getType());
		assertEquals(surface.getFeatureType(), copyBs.getFeatureType());
		
		Geometry copyBsGeom = copyBs.getGeometries().get(0);
		assertNotSame(geomSurface, copyBsGeom);
		
		Polygon copyLinkedPoly = copyBsGeom.getPolygons().get(0);
		assertNotSame(geomSurface.getPolygons().get(0), copyLinkedPoly);
		LinearRing copyExtRing = copyLinkedPoly.getExteriorRing();
		assertSame(copyExtRing, copyPolygon.getExteriorRing());
		
		Vertex copyV1 = copyExtRing.getVertices().get(0);
		assertSame(copyV1, copyExtRing.getVertices().get(2));
		assertEquals(v1, copyV1);
		assertNotSame(v1, copyV1);
		assertEquals(v2, copyExtRing.getVertices().get(1));
		assertNotSame(v2, copyExtRing.getVertices().get(1));
		assertEquals(v1, copyExtRing.getVertices().get(2));
		Polygon adjacentPoly = v1.getAdjacentPolygons(geom).iterator().next();
		Polygon copyAdjacentPoly = copyV1.getAdjacentPolygons(copyGeom).iterator().next();
		assertNotNull(copyAdjacentPoly);
		assertNotSame(adjacentPoly, copyAdjacentPoly);
		
		assertEquals(b.getGmlObject().getFunction().get(0).getValue(), copy.getGmlObject().getFunction().get(0).getValue());
	}
	
	private org.citygml4j.model.citygml.building.Building createCityGmlBuilding() {
		org.citygml4j.model.citygml.building.Building b = new org.citygml4j.model.citygml.building.Building();
		b.addFunction(new Code("2349"));
		b.setId("test");
		return b;
	}

}
