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

import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import de.hft.stuttgart.citydoctor2.check.Check;
import de.hft.stuttgart.citydoctor2.check.CheckError;
import de.hft.stuttgart.citydoctor2.check.CheckId;
import de.hft.stuttgart.citydoctor2.check.CheckResult;
import de.hft.stuttgart.citydoctor2.check.CheckType;
import de.hft.stuttgart.citydoctor2.check.Checkable;
import de.hft.stuttgart.citydoctor2.check.DefaultParameter;
import de.hft.stuttgart.citydoctor2.check.ResultStatus;
import de.hft.stuttgart.citydoctor2.check.error.UnknownCheckError;
import de.hft.stuttgart.citydoctor2.datastructure.AbstractBuilding;
import de.hft.stuttgart.citydoctor2.datastructure.BoundarySurface;
import de.hft.stuttgart.citydoctor2.datastructure.BridgeObject;
import de.hft.stuttgart.citydoctor2.datastructure.Building;
import de.hft.stuttgart.citydoctor2.datastructure.BuildingInstallation;
import de.hft.stuttgart.citydoctor2.datastructure.BuildingPart;
import de.hft.stuttgart.citydoctor2.datastructure.CityObject;
import de.hft.stuttgart.citydoctor2.datastructure.Geometry;
import de.hft.stuttgart.citydoctor2.datastructure.LandObject;
import de.hft.stuttgart.citydoctor2.datastructure.LinearRing;
import de.hft.stuttgart.citydoctor2.datastructure.Opening;
import de.hft.stuttgart.citydoctor2.datastructure.Polygon;
import de.hft.stuttgart.citydoctor2.datastructure.TransportationObject;
import de.hft.stuttgart.citydoctor2.datastructure.Vegetation;
import de.hft.stuttgart.citydoctor2.datastructure.WaterObject;
import de.hft.stuttgart.citydoctor2.parser.ParserConfiguration;

/**
 * A check according to the decorator pattern for handling unexpected exceptions
 * while performing the containing check. This will generate an error object for
 * writing the the reports, and will not cause the checking process to halt.
 * 
 * @author Matthias Betz
 *
 */
public class CheckContainer extends Check {

	private static Logger logger = LogManager.getLogger(CheckContainer.class);

	private final Check check;

	public CheckContainer(Check check) {
		super(null);
		this.check = check;
	}

	@Override
	public void init(Map<String, String> parameters, ParserConfiguration config) {
		check.init(parameters, config);
	}

	@Override
	public CheckId getCheckId() {
		return check.getCheckId();
	}

	@Override
	public List<Class<? extends Checkable>> getApplicableToClasses() {
		return check.getApplicableToClasses();
	}

	@Override
	public boolean canExecute(Checkable c) {
		return check.canExecute(c);
	}

	private void handleException(Exception e, Checkable c) {
		logger.error("Unexpected exception while executing check: " + check.getCheckId(), e);
		CheckError err = new UnknownCheckError(c, e, this);
		CheckResult cr = new CheckResult(check.getCheckId(), ResultStatus.ERROR, err);
		c.addCheckResult(cr);
	}

	@Override
	public List<DefaultParameter> getDefaultParameter() {
		return check.getDefaultParameter();
	}

	@Override
	public List<CheckId> getDependencies() {
		return check.getDependencies();
	}

	@Override
	public void check(AbstractBuilding ab) {
		try {
			check.check(ab);
		} catch (Exception e) {
			handleException(e, ab);
		}
	}

	@Override
	public void check(BoundarySurface bs) {
		try {
			check.check(bs);
		} catch (Exception e) {
			handleException(e, bs);
		}
	}

	@Override
	public void check(BridgeObject b) {
		try {
			check.check(b);
		} catch (Exception e) {
			handleException(e, b);
		}
	}

	@Override
	public void check(Building b) {
		try {
			check.check(b);
		} catch (Exception e) {
			handleException(e, b);
		}
	}

	@Override
	public void check(BuildingInstallation bi) {
		try {
			check.check(bi);
		} catch (Exception e) {
			handleException(e, bi);
		}
	}

	@Override
	public void check(BuildingPart bp) {
		try {
			check.check(bp);
		} catch (Exception e) {
			handleException(e, bp);
		}
	}

	@Override
	public void check(CityObject co) {
		try {
			check.check(co);
		} catch (Exception e) {
			handleException(e, co);
		}
	}

	@Override
	public void check(Geometry g) {
		try {
			check.check(g);
		} catch (Exception e) {
			handleException(e, g);
		}
	}

	@Override
	public void check(LandObject lo) {
		try {
			check.check(lo);
		} catch (Exception e) {
			handleException(e, lo);
		}
	}

	@Override
	public void check(LinearRing lr) {
		try {
			check.check(lr);
		} catch (Exception e) {
			handleException(e, lr);
		}
	}

	@Override
	public void check(Opening o) {
		try {
			check.check(o);
		} catch (Exception e) {
			handleException(e, o);
		}
	}

	@Override
	public void check(Polygon p) {
		try {
			check.check(p);
		} catch (Exception e) {
			handleException(e, p);
		}
	}

	@Override
	public void check(TransportationObject to) {
		try {
			check.check(to);
		} catch (Exception e) {
			handleException(e, to);
		}
	}

	@Override
	public void check(Vegetation veg) {
		try {
			check.check(veg);
		} catch (Exception e) {
			handleException(e, veg);
		}
	}

	@Override
	public void check(WaterObject wo) {
		try {
			check.check(wo);
		} catch (Exception e) {
			handleException(e, wo);
		}
	}

	@Override
	public CheckType getType() {
		return check.getType();
	}

	@Override
	public Check createNewInstance() {
		return check.createNewInstance();
	}

}
