/*-
 *  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 org.citygml4j.factory.GMLGeometryFactory;
import org.citygml4j.model.citygml.core.AbstractCityObject;
import org.citygml4j.model.gml.geometry.aggregates.MultiSurface;
import org.citygml4j.model.gml.geometry.aggregates.MultiSurfaceProperty;

import de.hft.stuttgart.citydoctor2.check.Check;
import de.hft.stuttgart.citydoctor2.parser.ParserConfiguration;
import de.hft.stuttgart.citydoctor2.utils.CityGmlUtils;

/**
 * Represents an Opening suchs a window or Door in a surface of a feature.
 * Contains a reference to the boundary surface where this opening is embedded
 * in.
 * 
 * @author Matthias Betz
 *
 */
public class Opening extends CityObject {

	private static final long serialVersionUID = 6409303152284607944L;

	private BoundarySurface partOf;
	private OpeningType type;
	private SurfaceFeatureType featureType;

	private AbstractCityObject ao;

	public Opening(OpeningType type, SurfaceFeatureType featureType, BoundarySurface partOf, AbstractCityObject ao) {
		this.featureType = featureType;
		this.partOf = partOf;
		this.type = type;
		this.ao = ao;
	}

	@Override
	public CityObject copy() {
		throw new UnsupportedOperationException("Cannot copy Opening");
	}

	public void reCreateGeometries(GMLGeometryFactory factory, ParserConfiguration config) {
		if (!ao.isSetId()) {
			ao.setId(getGmlId().getGmlString());
		}
		for (Geometry geom : getGeometries()) {
			if (geom.getType() == GeometryType.MULTI_SURFACE) {
				MultiSurface ms = CityGmlUtils.createMultiSurface(geom, factory, config);
				setGeometryAccordingToLod(geom.getLod(), new MultiSurfaceProperty(ms));
			} else {
				throw new IllegalStateException("BoundarySurfaces can only have MultiSurface geometries");
			}
		}
	}

	private void setGeometryAccordingToLod(Lod lod, MultiSurfaceProperty ms) {
		switch (lod) {
		case LOD3:
			setLod3Ms(ms);
			break;
		case LOD4:
			setLod4Ms(ms);
			break;
		default:
			throw new IllegalStateException("Cannot add geometry to opening because lod is not allowed: " + lod);
		}
	}

	private void setLod3Ms(MultiSurfaceProperty ms) {
		switch (featureType) {
		case BRIDGE:
			org.citygml4j.model.citygml.bridge.AbstractOpening bridgeAo = (org.citygml4j.model.citygml.bridge.AbstractOpening) ao;
			bridgeAo.setLod3MultiSurface(ms);
			break;
		case BUILDING:
			org.citygml4j.model.citygml.building.AbstractOpening buildAo = (org.citygml4j.model.citygml.building.AbstractOpening) ao;
			buildAo.setLod3MultiSurface(ms);
			break;
		case TUNNEL:
			org.citygml4j.model.citygml.tunnel.AbstractOpening tunAo = (org.citygml4j.model.citygml.tunnel.AbstractOpening) ao;
			tunAo.setLod3MultiSurface(ms);
		}
	}

	private void setLod4Ms(MultiSurfaceProperty ms) {
		switch (featureType) {
		case BRIDGE:
			org.citygml4j.model.citygml.bridge.AbstractOpening bridgeAo = (org.citygml4j.model.citygml.bridge.AbstractOpening) ao;
			bridgeAo.setLod4MultiSurface(ms);
			break;
		case BUILDING:
			org.citygml4j.model.citygml.building.AbstractOpening buildAo = (org.citygml4j.model.citygml.building.AbstractOpening) ao;
			buildAo.setLod4MultiSurface(ms);
			break;
		case TUNNEL:
			org.citygml4j.model.citygml.tunnel.AbstractOpening tunAo = (org.citygml4j.model.citygml.tunnel.AbstractOpening) ao;
			tunAo.setLod4MultiSurface(ms);
		}
	}

	public SurfaceFeatureType getSurfaceFeatureType() {
		return featureType;
	}

	public BoundarySurface getPartOf() {
		return partOf;
	}

	public void setType(OpeningType type) {
		this.type = type;
	}

	public OpeningType getType() {
		return type;
	}

	@Override
	public void accept(Check c) {
		super.accept(c);
		if (c.canExecute(this)) {
			c.check(this);
		}
	}

	@Override
	public void clearAllContainedCheckResults() {
		super.clearCheckResults();
	}

	public void unsetGmlGeometries() {
		switch (featureType) {
		case BRIDGE:
			org.citygml4j.model.citygml.bridge.AbstractOpening bridgeAo = (org.citygml4j.model.citygml.bridge.AbstractOpening) ao;
			bridgeAo.unsetLod3MultiSurface();
			bridgeAo.unsetLod4MultiSurface();
			break;
		case BUILDING:
			org.citygml4j.model.citygml.building.AbstractOpening buildAo = (org.citygml4j.model.citygml.building.AbstractOpening) ao;
			buildAo.unsetLod3MultiSurface();
			buildAo.unsetLod4MultiSurface();
			break;
		case TUNNEL:
			org.citygml4j.model.citygml.tunnel.AbstractOpening tunAo = (org.citygml4j.model.citygml.tunnel.AbstractOpening) ao;
			tunAo.unsetLod3MultiSurface();
			tunAo.unsetLod4MultiSurface();
		}
	}

	@Override
	public String toString() {
		return "Opening [type=" + type + ", id=" + getGmlId() + "]";
	}

	public void setPartOfSurface(BoundarySurface boundarySurface) {
		partOf = boundarySurface;
	}

	@Override
	public AbstractCityObject getGmlObject() {
		return ao;
	}

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

}
