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

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Representing an edge between two consecutive points of a linear ring
 * 
 * @author Matthias Betz
 *
 */
public class Edge implements Serializable {

	private static final long serialVersionUID = -504694863498128296L;

	private List<Polygon> adjacentPolygons = new ArrayList<>(2);
	private List<LinearRing> adjacentRings = new ArrayList<>(2);

	private Vertex from;
	private Vertex to;

	// start with 1 for a new edge
	private int numberOfHalfEdges = 1;
	private int numberOfOppositeHalfEdges;

	public Edge(Vertex from, Vertex to) {
		this.from = from;
		this.to = to;
	}

	public Vertex getFrom() {
		return from;
	}

	public Vertex getTo() {
		return to;
	}

	public void addHalfEdge(Vertex v0, Vertex v1) {
		if (v0 == null || v1 == null) {
			return;
		}
		if (isPointOfEdge(v0, from) && isPointOfEdge(v1, to)) {
			numberOfHalfEdges++;
			return;
		}
		if (isPointOfEdge(v1, from) && isPointOfEdge(v0, to)) {
			numberOfOppositeHalfEdges++;
			return;
		}
		throw new IllegalStateException("The given vertices do not from this edge");
	}

	private boolean isPointOfEdge(Vertex v0, Vertex edgePoint) {
		if (v0.equals(edgePoint)) {
			return true;
		}
		for (Vertex v : v0.getNeighbors()) {
			if (v.equals(edgePoint)) {
				return true;
			}
		}
		return false;
	}

	public boolean formedByIgnoreDirection(Vertex v1, Vertex v2) {
		return (v1 == from && v2 == to) || (v1 == to && v2 == from);
	}

	public boolean equalsIgnoreDirection(Edge other) {
		return (other.from.equals(from) && other.to.equals(to)) || (other.from.equals(to) && other.to.equals(from));
	}

	public int getNumberOfHalfEdges() {
		return numberOfHalfEdges;
	}

	public int getNumberOppositeHalfEdges() {
		return numberOfOppositeHalfEdges;
	}

	public int getNumberOfAllHalfEdges() {
		return numberOfHalfEdges + numberOfOppositeHalfEdges;
	}

	public List<Polygon> getAdjacentPolygons() {
		return adjacentPolygons;
	}

	public List<LinearRing> getAdjacentRings() {
		return adjacentRings;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((from == null) ? 0 : from.hashCode());
		result = prime * result + ((to == null) ? 0 : to.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Edge other = (Edge) obj;
		if (from == null) {
			if (other.from != null) {
				return false;
			}
		} else if (!from.equals(other.from)) {
			return false;
		}
		if (to == null) {
			if (other.to != null) {
				return false;
			}
		} else if (!to.equals(other.to)) {
			return false;
		}
		return true;
	}

	public void addAdjacentPolygon(Polygon p) {
		adjacentPolygons.add(p);
	}

	public void addAdjacentRing(LinearRing lr) {
		adjacentRings.add(lr);
	}

	@Override
	public String toString() {
		return "Edge [from=" + from + ", to=" + to + "]";
	}

	public boolean contains(Vertex v) {
		return v == from || v == to;
	}

	public Vertex getConnectionPoint(Edge e) {
		if (from == e.getTo() || from == e.getFrom()) {
			return from;
		}
		if (to == e.getTo() || to == e.getFrom()) {
			return to;
		}
		return null;
	}

	/**
	 * finds the point on the other end of the edge if the given vertex is one of
	 * the end points.
	 * 
	 * @param c the given vertex
	 * @return the opposite vertex or null if the given vertex is not an end point.
	 */
	public Vertex getOppositeVertex(Vertex c) {
		if (c == from) {
			return to;
		}
		if (c == to) {
			return from;
		}
		return null;
	}

}
