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


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

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

public class Polygon2dNs extends Polygon2d {

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

	public Polygon2dNs(List<Coordinate2d> coordinates, boolean setPartner) {
		Set<Coordinate2d> singleCoords = new HashSet<>(coordinates);

		// check if the coordinates are used twice
		// NOTE: A CoordinateSet is used, which allows only unique elements
		if (singleCoords.size() != coordinates.size()) {
			logger.warn("Some coordinates are identical");
		}

		List<HalfEdge2d> halfEdges = new ArrayList<>();
		for (int i = 0; i < coordinates.size() - 1; i++) {
			HalfEdge2d pHE = new HalfEdge2d(coordinates.get(i), coordinates.get(i + 1), setPartner);
			addChild(pHE);
			halfEdges.add(pHE);
		}

		// it2 is always one Coordinate ahead of it1, hence if it2 is pointing
		// behind the last element, it1 is pointing to the last
		HalfEdge2d pLastHE = new HalfEdge2d(coordinates.get(coordinates.size() - 1), coordinates.get(0), setPartner);
		addChild(pLastHE);
		halfEdges.add(pLastHE);

		mHalfEdges.add(halfEdges);

		// TODO MW: inner edges
		Polygon2d.connectHalfEdges(halfEdges);
	}

}
