/*-
 *  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.systemtest;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import de.hft.stuttgart.citydoctor2.check.CheckId;
import de.hft.stuttgart.citydoctor2.check.Checker;
import de.hft.stuttgart.citydoctor2.check.ValidationConfiguration;
import de.hft.stuttgart.citydoctor2.datastructure.CityDoctorModel;
import de.hft.stuttgart.citydoctor2.datastructure.CityObject;
import de.hft.stuttgart.citydoctor2.datastructure.Geometry;
import de.hft.stuttgart.citydoctor2.datastructure.Polygon;
import de.hft.stuttgart.citydoctor2.parser.CityGmlParseException;
import de.hft.stuttgart.citydoctor2.parser.CityGmlParser;
import de.hft.stuttgart.citydoctor2.parser.InvalidGmlFileException;

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

	public static CityDoctorModel loadAndCheckCityModel(String path) throws CityGmlParseException, IOException, InvalidGmlFileException {
		ValidationConfiguration valConfig = ValidationConfiguration.loadStandardValidationConfig();
		CityDoctorModel m = CityGmlParser.parseCityGmlFile(path, valConfig.getParserConfiguration());
		Checker c = new Checker(valConfig, m);
		c.runChecks();
		return m;
	}

	public static CityDoctorModel loadAndCheckCityModel(String path, int  numberOfRoundingPlaces)
			throws CityGmlParseException, IOException, InvalidGmlFileException {
		ValidationConfiguration config = ValidationConfiguration.loadStandardValidationConfig();
		config.setNumberOfRoundingPlaces(numberOfRoundingPlaces);
		CityDoctorModel m = CityGmlParser.parseCityGmlFile(path, config.getParserConfiguration());
		Checker c = new Checker(config, m);
		c.runChecks();
		return m;

	}
	
	public static CityDoctorModel loadAndCheckCityModel(String path, int  numberOfRoundingPlaces, double minVertexDistance)
			throws CityGmlParseException, IOException, InvalidGmlFileException {
		ValidationConfiguration config = ValidationConfiguration.loadStandardValidationConfig();
		config.setNumberOfRoundingPlaces(numberOfRoundingPlaces);
		config.setMinVertexDistance(minVertexDistance);
		CityDoctorModel m = CityGmlParser.parseCityGmlFile(path, config.getParserConfiguration());
		Checker c = new Checker(config, m);
		c.runChecks();
		return m;

	}
	
	public static CityDoctorModel loadAndCheckCityModel(String path, Map<CheckId, Map<String, String>> paramMap)
			throws CityGmlParseException, IOException, InvalidGmlFileException {
		ValidationConfiguration valConfig = ValidationConfiguration.loadStandardValidationConfig();
		for (Entry<CheckId, Map<String, String>> e : paramMap.entrySet()) {
			valConfig.getChecks().get(e.getKey()).setParameters(e.getValue());
		}
		CityDoctorModel m = CityGmlParser.parseCityGmlFile(path, valConfig.getParserConfiguration());
		Checker c = new Checker(valConfig, m);
		c.runChecks();
		return m;

	}

	public static Polygon getPolygonById(String gmlId, CityDoctorModel model) {
		if (gmlId == null) {
			return null;
		}
		Polygon result = getPolygonById(gmlId, model.getBuildings());
		if (result != null) {
			return result;
		}
		result = getPolygonById(gmlId, model.getVegetation());
		if (result != null) {
			return result;
		}
		return getPolygonById(gmlId, model.getTransportation());
	}

	private static Polygon getPolygonById(String gmlId, List<? extends CityObject> features) {
		if (gmlId == null) {
			return null;
		}
		for (CityObject co : features) {
			Polygon result = getPolygonById(gmlId, co);
			if (result != null) {
				return result;
			}
		}
		return null;
	}

	public static Polygon getPolygonById(String gmlId, String featureId, CityDoctorModel model) {
		if (gmlId == null || featureId == null) {
			return null;
		}
		CityObject co = getCityObjectById(featureId, model);
		return getPolygonById(gmlId, co);
	}

	private static Polygon getPolygonById(String gmlId, CityObject co) {
		for (Geometry geom : co.getGeometries()) {
			for (Polygon p : geom.getPolygons()) {
				if (gmlId.equals(p.getGmlId().getGmlString())) {
					return p;
				}
			}
		}
		return null;
	}

	private static CityObject getCityObjectById(String featureId, CityDoctorModel model) {
		if (featureId == null) {
			return null;
		}
		for (CityObject co : model.getBuildings()) {
			if (featureId.equals(co.getGmlId().getGmlString())) {
				return co;
			}
		}
		for (CityObject co : model.getTransportation()) {
			if (featureId.equals(co.getGmlId().getGmlString())) {
				return co;
			}
		}
		for (CityObject co : model.getVegetation()) {
			if (featureId.equals(co.getGmlId().getGmlString())) {
				return co;
			}
		}
		return null;
	}

}
