/*-
 *  Copyright 2022 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 org.citygml4j.core.model.deprecated.bridge.DeprecatedPropertiesOfBridgeConstructiveElement;
import org.citygml4j.core.util.geometry.GeometryFactory;
import org.xmlobjects.gml.model.geometry.GeometryProperty;
import org.xmlobjects.gml.model.geometry.aggregates.MultiSurface;
import org.xmlobjects.gml.model.geometry.aggregates.MultiSurfaceProperty;
import org.xmlobjects.gml.model.geometry.complexes.CompositeSurface;
import org.xmlobjects.gml.model.geometry.primitives.Solid;
import org.xmlobjects.gml.model.geometry.primitives.SolidProperty;

import de.hft.stuttgart.citydoctor2.parser.ParserConfiguration;
import de.hft.stuttgart.citydoctor2.utils.CityGmlUtils;
import de.hft.stuttgart.citydoctor2.utils.Copyable;

public class BridgeConstructiveElement extends CityObject {

	private static final String CANNOT_ADD = "Cannot add ";

	private static final long serialVersionUID = 7353233899458901155L;

	private org.citygml4j.core.model.bridge.BridgeConstructiveElement gmlBridgeElement;

	public BridgeConstructiveElement(org.citygml4j.core.model.bridge.BridgeConstructiveElement gmlObject) {
		this.gmlBridgeElement = gmlObject;
	}

	@Override
	public Copyable createCopyInstance() {
		return new BridgeConstructiveElement(gmlBridgeElement);
	}

	@Override
	public void reCreateGeometries(GeometryFactory factory, ParserConfiguration config) {
		// only handles CityGML2 for now
		// unknown which CityGML is handled here
		// need context information to decide
		for (Geometry geom : getGeometries()) {
			switch (geom.getType()) {
			case SOLID:
				Solid solid = CityGmlUtils.createSolid(geom, factory, config);
				setSolidAccordingToLod(geom, solid);
				break;
			case MULTI_SURFACE:
				MultiSurface ms = CityGmlUtils.createMultiSurface(geom, factory, config);
				setMultiSurfaceAccordingToLod(geom, ms);
				break;
			case COMPOSITE_SURFACE:
				CompositeSurface cs = CityGmlUtils.createCompositeSurface(geom, factory, config);
				setCompositeSurfaceAccordingToLod(geom, cs);
				break;
			}
		}
	}

	private void setCompositeSurfaceAccordingToLod(Geometry geom, CompositeSurface cs) {
		switch (geom.getLod()) {
		case LOD1:
			gmlBridgeElement.getDeprecatedProperties().setLod1Geometry(new GeometryProperty<>(cs));
			break;
		case LOD2:
			gmlBridgeElement.getDeprecatedProperties().setLod2Geometry(new GeometryProperty<>(cs));
			break;
		case LOD3:
			gmlBridgeElement.getDeprecatedProperties().setLod3Geometry(new GeometryProperty<>(cs));
			break;
		case LOD4:
			gmlBridgeElement.getDeprecatedProperties().setLod4Geometry(new GeometryProperty<>(cs));
			break;
		default:
			throw new IllegalStateException(CANNOT_ADD + geom.getLod() + " composite surface to buildings");
		}		
	}

	private void setSolidAccordingToLod(Geometry geom, Solid solid) {
		switch (geom.getLod()) {
		case LOD1:
			gmlBridgeElement.getDeprecatedProperties().setLod1Geometry(new SolidProperty(solid));
			break;
		case LOD2:
			gmlBridgeElement.getDeprecatedProperties().setLod2Geometry(new SolidProperty(solid));
			break;
		case LOD3:
			gmlBridgeElement.getDeprecatedProperties().setLod3Geometry(new SolidProperty(solid));
			break;
		case LOD4:
			gmlBridgeElement.getDeprecatedProperties().setLod4Geometry(new SolidProperty(solid));
			break;
		default:
			throw new IllegalStateException(CANNOT_ADD + geom.getLod() + " solid to buildings");
		}
	}
	
	private void setMultiSurfaceAccordingToLod(Geometry geom, MultiSurface ms) {
		switch (geom.getLod()) {
		case LOD0:
			gmlBridgeElement.getDeprecatedProperties().setLod1Geometry(new MultiSurfaceProperty(ms));
			break;
		case LOD1:
			gmlBridgeElement.getDeprecatedProperties().setLod1Geometry(new MultiSurfaceProperty(ms));
			break;
		case LOD2:
			gmlBridgeElement.getDeprecatedProperties().setLod1Geometry(new MultiSurfaceProperty(ms));
			break;
		case LOD3:
			gmlBridgeElement.getDeprecatedProperties().setLod1Geometry(new MultiSurfaceProperty(ms));
			break;
		case LOD4:
			gmlBridgeElement.getDeprecatedProperties().setLod1Geometry(new MultiSurfaceProperty(ms));
			break;
		default:
			throw new IllegalStateException(CANNOT_ADD + geom.getLod() + " multi surface to buildings");
		}
	}

	@Override
	public org.citygml4j.core.model.bridge.BridgeConstructiveElement getGmlObject() {
		return gmlBridgeElement;
	}

	@Override
	public void unsetGmlGeometries() {
		gmlBridgeElement.setLod0MultiSurface(null);
		gmlBridgeElement.setLod2MultiSurface(null);
		gmlBridgeElement.setLod3MultiSurface(null);
		DeprecatedPropertiesOfBridgeConstructiveElement depProps = gmlBridgeElement.getDeprecatedProperties();
		depProps.setLod1Geometry(null);
		depProps.setLod2Geometry(null);
		depProps.setLod3Geometry(null);
		depProps.setLod4Geometry(null);
		gmlBridgeElement.setLod1Solid(null);
		gmlBridgeElement.setLod2Solid(null);
		gmlBridgeElement.setLod3Solid(null);
	}

	@Override
	public FeatureType getFeatureType() {
		return FeatureType.BRIDGE_CONSTRUCTION_ELEMENT;
	}

}
