/*-
 *  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.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import de.hft.stuttgart.citydoctor2.check.CheckId;
import de.hft.stuttgart.citydoctor2.check.CheckResult;
import de.hft.stuttgart.citydoctor2.check.Checker;
import de.hft.stuttgart.citydoctor2.check.ResultStatus;
import de.hft.stuttgart.citydoctor2.datastructure.CityDoctorModel;
import de.hft.stuttgart.citydoctor2.datastructure.Edge;
import de.hft.stuttgart.citydoctor2.datastructure.Geometry;
import de.hft.stuttgart.citydoctor2.datastructure.Vertex;
import de.hft.stuttgart.citydoctor2.parser.CityGmlParseException;
import de.hft.stuttgart.citydoctor2.parser.ParserConfiguration;
import de.hft.stuttgart.citydoctor2.parser.CityGmlParser;
import de.hft.stuttgart.citydoctor2.parser.InvalidGmlFileException;

/**
 * 
 * @author Matthias Betz
 *
 */
public class OuterEdgeCheckTest {
	
	@Before
	public void setup() {
		// clear interrupt flag
		Thread.interrupted();
	}
	
	@Test()
	public void testOuterEdge1() throws CityGmlParseException, IOException, InvalidGmlFileException {
		ParserConfiguration config = new ParserConfiguration(8, false);
		CityDoctorModel m = CityGmlParser.parseCityGmlFile("src/test/resources/QA-CS-OUTEREDGE.gml", config);
		Checker c = new Checker(m);
		c.runChecks();
		
		ArrayList<Vertex> expectedErrorEdges = new ArrayList<Vertex>();
		expectedErrorEdges.add(new Vertex(2.0, 0.0, 3.0));
		expectedErrorEdges.add(new Vertex(2.0, 0.0, 0.0));
		expectedErrorEdges.add(new Vertex(0.0, 0.0, 0.0));
		expectedErrorEdges.add(new Vertex(0.0, 0.0, 3.0));
		expectedErrorEdges.add(new Vertex(2.0, 0.0, 0.0));
		expectedErrorEdges.add(new Vertex(0.0, 0.0, 0.0));
		expectedErrorEdges.add(new Vertex(0.0, 0.0, 3.0));
		expectedErrorEdges.add(new Vertex(1.0, 0.0, 3.5));
		expectedErrorEdges.add(new Vertex(1.0, 0.0, 3.5));
		expectedErrorEdges.add(new Vertex(2.0, 0.0, 3.0));
		
		Geometry geom = m.getBuildings().get(0).getGeometries().get(0);
		CheckResult cr = geom.getCheckResult(CheckId.C_GE_S_NOT_CLOSED);
		Assert.assertEquals(ResultStatus.ERROR, cr.getResultStatus());
	}
	
	public boolean isEdgeInExpectedErrorEdges(Edge e, Geometry geom, List<Vertex> expectedErrorEdges) {
		Vertex v0 = e.getFrom();
		Vertex v1 = e.getTo();
		for (int i = 0; i < expectedErrorEdges.size() - 1; i = i + 2) {
			if (expectedErrorEdges.get(i).equals(v0)) {
				if (expectedErrorEdges.get(i + 1).equals(v1)) {
					return true;
				}
			}
		}
		return false;
	}

}
