/*-
 *  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 HalfEdge extends BaseEntity {

	private Coordinate3d start;
	private Coordinate3d end;

	private HalfEdge partner;
	private HalfEdge next;

	public HalfEdge(Coordinate3d start, Coordinate3d end, EdgePolygon parent) {
		this.start = start;
		this.end = end;

		List<HalfEdge> halfEdgesToStart = start.getParents(HalfEdge.class);
		List<HalfEdge> halfEdgesToEnd = end.getParents(HalfEdge.class);

		HalfEdge tempPartner;
		if (parent == null) {
			tempPartner = findPartner(halfEdgesToStart, halfEdgesToEnd);
		} else {
			tempPartner = findPartner(parent, halfEdgesToStart, halfEdgesToEnd);
		}
		if (tempPartner != null) {
			setPartner(tempPartner);
		}
		addChild(start);
		addChild(end);
	}

	public HalfEdge(Coordinate3d start, Coordinate3d end) {
		this(start, end, null);
	}

	private HalfEdge findPartner(EdgePolygon parent, List<HalfEdge> halfEdgesToStart, List<HalfEdge> halfEdgesToEnd) {
		HalfEdge tempPartner = null;
		for (HalfEdge heStart : halfEdgesToStart) {
			for (HalfEdge heEnd : halfEdgesToEnd) {
				if (heStart == heEnd) {
					tempPartner = heStart;
					if (tempPartner.getPartner() == null) {
						EdgePolygon partnerParent = tempPartner.getPolygon();
						if (partnerParent != null || partnerParent != parent) {
							return tempPartner;
						}
					}
				}
			}
		}
		return tempPartner;
	}

	private HalfEdge findPartner(List<HalfEdge> halfEdgesToStart, List<HalfEdge> halfEdgesToEnd) {
		HalfEdge tempPartner = null;
		for (HalfEdge heStart : halfEdgesToStart) {
			for (HalfEdge heEnd : halfEdgesToEnd) {
				if (heStart == heEnd) {
					tempPartner = heStart;
					if (tempPartner.getPartner() == null) {
						EdgePolygon partnerParent = tempPartner.getPolygon();
						if (partnerParent != null) {
							return tempPartner;
						}
					}
				}
			}
		}
		return tempPartner;
	}

	public void setPartner(HalfEdge partner) {
		this.partner = partner;
		partner.partner = this;
	}

	public HalfEdge getPartner() {
		return partner;
	}

	public EdgePolygon getPolygon() {
		List<EdgePolygon> polygons = getParents(EdgePolygon.class);
		if (!polygons.isEmpty()) {
			return polygons.get(0);
		}
		return null;
	}

	public GmBoundedStraight getStraight() {
		return GmBoundedStraight.of(start.getPoint(), end.getPoint());
	}

	public Coordinate3d getStart() {
		return start;
	}

	public Coordinate3d getEnd() {
		return end;
	}

	public HalfEdge getNext() {
		return next;
	}

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

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

}
