package ca.appbox.jira.plugins.issuedependencyviewer.graph;

import java.util.ArrayList;
import java.util.List;

import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.link.IssueLinkManager;

public final class GraphBuilder {

	private IssueManager issueManager;
	private IssueLinkManager issueLinkManager;

	private List<Long> existingNodes = new ArrayList<Long>();
	
	public GraphBuilder(IssueManager issueManager,
			IssueLinkManager issueLinkManager) {
		
		this.issueManager = issueManager;
		this.issueLinkManager = issueLinkManager;
	}

	public Graph buildGraph(Long currentIssueId, boolean considerInwardDep, boolean considerOutwardDep) {
		
		final Graph graph = new Graph();

		MutableIssue issueObject = issueManager.getIssueObject(currentIssueId);
		
		addAdjacentNodesToGraph(graph, issueObject.getId(), considerInwardDep, considerOutwardDep);
		
//		issueManager.
		
//		issueLinkManager.getOutwardLinks(0);
		
//		graph.getNodes().add(new Node("GT-123", "1"));
//		graph.getNodes().add(new Node("GT-456", "2"));
//		graph.getNodes().add(new Node("GT-789", "3"));
		
//		graph.getLinks().add(new Link(0, 1, "resolved"));
//		graph.getLinks().add(new Link("GT-123", "GT-789", "resolved"));
//		graph.getLinks().add(new Link("GT-456", "GT-789", "resolved"));
//		graph.getLinks().add(new Link("GT-789", "GT-123", "resolved"));
		
		return graph;
	}

	private void addAdjacentNodesToGraph(Graph graph, Long issueId, boolean considerInwardDep, boolean considerOutwardDep) {

		if (!existingNodes.contains(issueId)) {
			MutableIssue issueObject = issueManager.getIssueObject(issueId);
			Node newNode = new Node(issueId, issueObject.getKey(),"1");
			existingNodes.add(issueId);
			graph.addNode(newNode);
		}
		
		if (considerOutwardDep) {
			for (IssueLink currentOutwardLink : issueLinkManager.getOutwardLinks(issueId)) {
				
				if (!currentOutwardLink.isSystemLink()) {
					if (!existingNodes.contains(currentOutwardLink.getDestinationId())) {
						addAdjacentNodesToGraph(graph, currentOutwardLink.getDestinationId(), considerInwardDep, considerOutwardDep);
					}
					
					Link potentialNewLink = new Link(currentOutwardLink.getSourceId(),currentOutwardLink.getDestinationId(),"resolved");
					
					if (!graph.containsLink(potentialNewLink)) {
						graph.addLink(potentialNewLink);
					}
				}
			}
		}
		
		if (considerInwardDep) {
			for (IssueLink currentInwardLink : issueLinkManager.getInwardLinks(issueId)) {

				if (!currentInwardLink .isSystemLink()) {
					if (!existingNodes.contains(currentInwardLink.getSourceId())) {
						addAdjacentNodesToGraph(graph, currentInwardLink.getSourceId(), considerInwardDep, considerOutwardDep);
					}
					
					Link potentialNewLink = new Link(currentInwardLink.getDestinationId(),currentInwardLink.getSourceId(),"resolved");
					
					if (!graph.containsLink(potentialNewLink)) {
						graph.addLink(potentialNewLink);
					}
				}
			}
		}
	}
}

@@ -974,490 +974,8 @@
 %0A%09%09%0A
-//%09%09issueManager.%0A%09%09%0A//%09%09issueLinkManager.getOutwardLinks(0);%0A%09%09%0A//%09%09graph.getNodes().add(new Node(%22GT-123%22, %221%22));%0A//%09%09graph.getNodes().add(new Node(%22GT-456%22, %222%22));%0A//%09%09graph.getNodes().add(new Node(%22GT-789%22, %223%22));%0A%09%09%0A//%09%09graph.getLinks().add(new Link(0, 1, %22resolved%22));%0A//%09%09graph.getLinks().add(new Link(%22GT-123%22, %22GT-789%22, %22resolved%22));%0A//%09%09graph.getLinks().add(new Link(%22GT-456%22, %22GT-789%22, %22resolved%22));%0A//%09%09graph.getLinks().add(new Link(%22GT-789%22, %22GT-123%22, %22resolved%22));%0A%09%09%0A
 %09%09re
@@ -1344,26 +1344,53 @@
 wNode);%0A%09%09%7D%0A
+%0A
 %09%09
+// FIXME : accept as param
 %0A%09%09if (consi
@@ -1984,19 +1984,48 @@
 %09%09%7D%0A%09%09%7D%0A
-
 %09%09%0A
+%09%09// FIXME : accept as param%0A
 %09%09if (co

clean up.