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

public class PolyLineSegment2d extends BaseEntity {

	private Coordinate2d start;
	private Coordinate2d end;

	private PolyLineSegment2d next;

	public PolyLineSegment2d(Coordinate2d start, Coordinate2d end, PolyLineSegment2d next) {
		this.start = start;
		this.end = end;
		this.next = next;

		addChild(start);
		addChild(end);
	}

	public PolyLineSegment2d(Coordinate2d start, Coordinate2d end) {
		this(start, end, null);
	}

	public Coordinate2d getEnd() {
		return end;
	}

	public Coordinate2d getStart() {
		return start;
	}

	public void reverse() {
		Coordinate2d pHelp = start;
		start = end;
		end = pHelp;
	}

	public void setNext(PolyLineSegment2d next) {
		this.next = next;
	}

	public void setStart(Coordinate2d start) {
		this.start = start;
	}

	public void setEnd(Coordinate2d end) {
		this.end = end;
	}

	public PolyLineSegment2d getNext() {
		return next;
	}

	public double getLength() {
		Point2d startPoint = start.getPoint();
		Point2d endPoint = end.getPoint();
		return ((startPoint.equals(endPoint)) ? 0.0 : (startPoint.minus(endPoint)).getLength());
	}

	public boolean isGeometricallyEqual(PolyLineSegment2d cpSegment2, double eps) {
		PolyLineSegment2d cpSegment1 = this;
		Point2d start1 = cpSegment1.start.getPoint();
		Point2d end1 = cpSegment1.end.getPoint();
		Point2d start2 = cpSegment2.start.getPoint();
		Point2d end2 = cpSegment2.end.getPoint();

		return (start1.isAlmostEqual(start2, eps) && end1.isAlmostEqual(end2, eps))
				|| (start1.isAlmostEqual(end2, eps) && end1.isAlmostEqual(start2, eps));

	}

	@Override
	public String toString() {
		return "PolyLineSegment2d [start=" + start + ", end=" + end + "]";
	}

}
