package DIKBD;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;

import org.jgrapht.EdgeFactory;
import org.jgrapht.Graph;

public class DIKBD_Graph  implements Graph<DIKBD_Vertex, DIKBD_Edge> {

	
	static DIKBD_Vertex[] vertexes;

	private int numNodes;
	private int Cd;
	private int root;

	
	public DIKBD_Graph( int numNodes) {
		super();
		this.numNodes = numNodes;
		Cd=0;
		vertexes = new DIKBD_Vertex[numNodes];
	}
	
	public  int getNumNodes()
	{
		return numNodes;
	}
	public DIKBD_Vertex getVertexByID(int id){
		return vertexes[id];
	}
	
	public DIKBD_Edge addWeightedEdge(DIKBD_Vertex sourceVertex, DIKBD_Vertex targetVertex, int d, int id) {
		if(d>Cd){
			Cd=d;
		}
		vertexes[sourceVertex.getID()].addOutGoingEdge(new DIKBD_Edge(d,sourceVertex,targetVertex, id));
		return null;
	}
	
	
	
	@Override
	public DIKBD_Edge addEdge(DIKBD_Vertex sourceVertex, DIKBD_Vertex targetVertex) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public boolean addEdge(DIKBD_Vertex sourceVertex, DIKBD_Vertex targetVertex, DIKBD_Edge e) {
		return false;
	}

	@Override
	public boolean addVertex(DIKBD_Vertex v) {
		vertexes[v.getID()] = v;
		return true;
	}
	
	@Override
	public boolean containsEdge(DIKBD_Vertex sourceVertex,
			DIKBD_Vertex targetVertex) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean containsEdge(DIKBD_Edge e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean containsVertex(DIKBD_Vertex v) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Set<DIKBD_Edge> edgeSet() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Set<DIKBD_Edge> edgesOf(DIKBD_Vertex vertex) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Set<DIKBD_Edge> getAllEdges(DIKBD_Vertex sourceVertex,
			DIKBD_Vertex targetVertex) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DIKBD_Edge getEdge(DIKBD_Vertex sourceVertex, DIKBD_Vertex targetVertex) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public EdgeFactory<DIKBD_Vertex, DIKBD_Edge> getEdgeFactory() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DIKBD_Vertex getEdgeSource(DIKBD_Edge e) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DIKBD_Vertex getEdgeTarget(DIKBD_Edge e) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public double getEdgeWeight(DIKBD_Edge e) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean removeAllEdges(Collection<? extends DIKBD_Edge> edges) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Set<DIKBD_Edge> removeAllEdges(DIKBD_Vertex sourceVertex,
			DIKBD_Vertex targetVertex) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean removeAllVertices(Collection<? extends DIKBD_Vertex> vertices) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public DIKBD_Edge removeEdge(DIKBD_Vertex sourceVertex,
			DIKBD_Vertex targetVertex) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean removeEdge(DIKBD_Edge e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean removeVertex(DIKBD_Vertex v) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Set<DIKBD_Vertex> vertexSet() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public int getCd()
	{
		return Cd;
	}
	/**
	 * Obtain the path from the node given in the DIKBD SP
	 * to an specified node.
	 * @param endNode the node at where we ask the sp
	 * @return a string of the path
	 */
	public ArrayList<Integer> getPath(int endNode, ArrayList<Integer> path){
		path.add(0, endNode);
		if(endNode == root)
			return path;
		else
			return getPath(vertexes[endNode].pre , path);
	}
	
	public String toString() {

		String nodesS = "";
		String edgesS = "";
		for (int i = 0; i < vertexes.length; i++) {
			nodesS += (", " + i);
			DIKBD_Edge e = vertexes[i].getEdges();
			while (e != null) {
				edgesS += (", ("+e.getSource().getID() +" , "+e.getTarget().getID() +")=" +e.getWeightDist());
				e = e.getNext();
			}
		}
		nodesS = nodesS.replaceFirst(", ", "");
		edgesS = edgesS.replaceFirst(", ", "");
		return "["+nodesS+"]\n"+ "["+edgesS+"]\n";
	}

/*	public String getAllPaths() {
		String retorno="";
		for(int v_i = 0 ; v_i<numNodes ; v_i++){
			retorno += getPath(v_i)+"\n";
		}
		
		return retorno;
	}*/

	public void setRoot(int nR) {
		root = nR;
		
	}

	public ArrayList<Integer> getPathArcs(int endNode, ArrayList<Integer> pathArcs) {
		
		if(endNode == root)
			return pathArcs;
		else{
			pathArcs.add(0, vertexes[endNode].arc);
			return getPathArcs(vertexes[endNode].pre , pathArcs);
		}
			
	}

	public void setEdgeWeight(int i, int m) {
		// TODO Auto-generated method stub
		
	}
	
}
