/*-
 *  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.core.model.construction.AbstractFillingSurface;
import org.citygml4j.core.model.core.AbstractCityObject;
import org.citygml4j.core.util.geometry.GeometryFactory;
import org.xmlobjects.gml.model.geometry.aggregates.MultiSurface;
import org.xmlobjects.gml.model.geometry.aggregates.MultiSurfaceProperty;

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

/**
 * 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 AbstractFillingSurface ao;

	private Opening(OpeningType type) {
		this.type = type;
	}

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

	public void reCreateGeometries(GeometryFactory factory, ParserConfiguration config) {
		if (ao.getId() != null) {
			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("Openings can only have MultiSurface geometries");
			}
		}
	}

	private void setGeometryAccordingToLod(Lod lod, MultiSurfaceProperty ms) {
		switch (lod) {
		case LOD0:
			ao.setLod0MultiSurface(ms);
			break;
		case LOD1:
			ao.setLod1MultiSurface(ms);
			break;
		case LOD2:
			ao.setLod2MultiSurface(ms);
			break;
		case LOD3:
			ao.setLod3MultiSurface(ms);
			break;
		case LOD4:
			ao.getDeprecatedProperties().setLod4MultiSurface(ms);
			break;
		default:
			throw new IllegalStateException("Cannot add geometry to opening because lod is not allowed: " + lod);
		}
	}


	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() {
		ao.setLod0MultiSurface(null);
		ao.setLod1MultiSurface(null);
		ao.setLod2MultiSurface(null);
		ao.setLod3MultiSurface(null);
		ao.getDeprecatedProperties().setLod4MultiSurface(null);
	}

	@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;
	}

	@Override
	public Copyable createCopyInstance() {
		return new Opening(type);
	}

	@Override
	public void collectInstances(CopyHandler handler) {
		super.collectInstances(handler);
		handler.addInstance(partOf);
	}

	@Override
	public void fillValues(Copyable original, CopyHandler handler) {
		super.fillValues(original, handler);
		Opening originalOpening = (Opening) original;
		partOf = handler.getCopyInstance(originalOpening.partOf);
		type = originalOpening.type;
		featureType = originalOpening.featureType;
		ao = originalOpening.ao;
	}

}
