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

public class PolyLine extends BaseEntity {

	private PolyLineSegment mpFirst;
	private PolyLineSegment mpLast;

	public PolyLine(Coordinate3d pVertex1, Coordinate3d pVertex2) {
		PolyLineSegment pSegment = new PolyLineSegment(pVertex1, pVertex2);
		mpFirst = mpLast = pSegment;
		addChild(pSegment);
	}

	public PolyLine(List<Coordinate3d> rCoordinateList, boolean closePolyLine) {
		if (rCoordinateList.size() < 2) {
			throw new IllegalArgumentException("rCoordinateList list needs at least 2 points");
		}

		mpFirst = mpLast = null;

		for (int i = 0; i < rCoordinateList.size() - 1; i++) {
			Coordinate3d itCS = rCoordinateList.get(i);
			Coordinate3d itCE = rCoordinateList.get(i + 1);
			PolyLineSegment pSegment = new PolyLineSegment(itCS, itCE);

			if (null == mpFirst) {
				mpFirst = mpLast = pSegment;
			} else {
				mpLast.setNext(pSegment);
				mpLast = pSegment;
			}

			addChild(pSegment);
		}

		if (mpLast == null) {
			throw new NullPointerException();
		}

		if ((rCoordinateList.get(0) == rCoordinateList.get(rCoordinateList.size() - 1)) && closePolyLine) {
			mpLast.setNext(mpFirst);
		}
	}

	public Coordinate3d getStart() {
		return mpFirst.getStart();
	}

	public Coordinate3d getEnd() {
		return mpLast.getEnd();
	}

	public PolyLineSegment getFirstSegment() {
		return mpFirst;
	}

	public PolyLineSegment getLastSegment() {
		return mpLast;
	}

	@Override
	public String toString() {
		return "PolyLine [mpFirst=" + mpFirst + ", mpLast=" + mpLast + "]";
	}

}
