/*-
 *  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.core.AbstractCityObject;
import org.citygml4j.core.model.landuse.LandUse;
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.Copyable;

/**
 * Represents a land use CityGML object.
 * 
 * @author Matthias Betz
 *
 */
public class LandObject extends CityObject {

	private static final long serialVersionUID = 1887455662411087326L;

	private LandUse lu;

	public LandObject(LandUse lu) {
		this.lu = lu;
	}

	@Override
	public void reCreateGeometries(GeometryFactory factory, ParserConfiguration config) {
		for (Geometry geom : getGeometries()) {
			if (geom.getType() == GeometryType.MULTI_SURFACE) {
				MultiSurface ms = CityGmlUtils.createMultiSurface(geom, factory, config);
				setMultiSurfaceAccordingToLod(geom, ms);
			} else {
				throw new IllegalStateException("Landuse cannot have solids: " + geom);
			}
		}
	}

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

	private void setMultiSurfaceAccordingToLod(Geometry geom, MultiSurface ms) {
		switch (geom.getLod()) {
		case LOD0:
			lu.setLod0MultiSurface(new MultiSurfaceProperty(ms));
			break;
		case LOD1:
			lu.setLod1MultiSurface(new MultiSurfaceProperty(ms));
			break;
		case LOD2:
			lu.setLod2MultiSurface(new MultiSurfaceProperty(ms));
			break;
		case LOD3:
			lu.setLod3MultiSurface(new MultiSurfaceProperty(ms));
			break;
		case LOD4:
			lu.getDeprecatedProperties().setLod4MultiSurface(new MultiSurfaceProperty(ms));
			break;
		}
	}

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

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

	@Override
	public void unsetGmlGeometries() {
		lu.setLod0MultiSurface(null);
		lu.setLod1MultiSurface(null);
		lu.setLod2MultiSurface(null);
		lu.setLod3MultiSurface(null);
		lu.getDeprecatedProperties().setLod4MultiSurface(null);
	}

	public void setGmlObject(LandUse landUse) {
		lu = landUse;
	}

	@Override
	public String toString() {
		return "LandObject [id=" + getGmlId() + "]";
	}
	
	@Override
	public Copyable createCopyInstance() {
		return new LandObject(lu);
	}

}
