/*
 BINViz, Bidirectional Interactive Network Visualization
 Copyright (C) 2007  Benjamin Weyers, University of Michigan, Ann Arbor, MI, U.S.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program 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 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @fileoverview Provides the PLUGIN class necessary for X
 * @author Benjamin Weyers, University of Duisburg-Essen, University of Michigan
 * @version 0.1
 */

/**
 * Constructor description
 * 
 * @constructor
 *  
 * @class Class description
 * 
 * @author Benjamin Weyers, University of Duisburg-Essen, University of Michigan
 * @version 0.1
 */
function DotParserPlugin()
{
	this.htmlElement;
	this.graphView;
}

DotParserPlugin.prototype = Object();

DotParserPlugin.prototype.init=function(graphView, initData)
{
	this.graphView = graphView;
	this.htmlElement = this.createHtmlElement();
}

DotParserPlugin.prototype.createHtmlElement=function()
{
	div = document.createElement("div");
	this.textElement = document.createElement("textarea");
	$(this.textElement).css({'width':'360px','height':'150px'});
	
	// option Box
	this.optionBox = document.createElement("div");
	$(this.optionBox).css({'height':'80px','width':'400px','position':'relative'});
	
	this.serializeButton = document.createElement("button");
	this.serializeButton.appendChild(document.createTextNode("Serialize Graph"));
	$(this.serializeButton).css({'width':'120px','height':'25px','position':'absolute','top':'25px','left':'240px'});
	
	this.parseButton = document.createElement("button");
	this.parseButton.appendChild(document.createTextNode("Parse Graph"));
	$(this.parseButton).css({'tooltip':'Serializes the graph to DOT format','width':'120px','height':'25px','position':'absolute','top':'52px','left':'240px'});
	
	this.nodeSpan = document.createElement("span");
	this.nodeSpan.appendChild(document.createTextNode("Node options:"));
	$(this.nodeSpan).css({'font-weight':'bold','position':'absolute','top':'0px','left':'0px'});
	
	this.checkPositionSpan = document.createElement("span");
	this.checkPositionSpan.appendChild(document.createTextNode("Position:"));
	$(this.checkPositionSpan).css({'position':'absolute','top':'20px','left':'0px'});
	this.checkPosition = document.createElement("input");
	this.checkPosition.type = "checkbox";
	this.checkPosition.checked = "true";
	$(this.checkPosition).css({'position':'absolute','top':'20px','left':'46px'});
	
	this.checkNodeColorSpan = document.createElement("span");
	this.checkNodeColorSpan.appendChild(document.createTextNode("Color:"));
	$(this.checkNodeColorSpan).css({'position':'absolute','top':'20px','left':'68px'});
	this.checkNodeColor = document.createElement("input");
	this.checkNodeColor.type = "checkbox";
	this.checkNodeColor.checked = "true";
	$(this.checkNodeColor).css({'position':'absolute','top':'20px','left':'100px'});
	
	this.checkBorderSpan = document.createElement("span");
	this.checkBorderSpan.appendChild(document.createTextNode("Border:"));
	$(this.checkBorderSpan).css({'color':'gray','position':'absolute','top':'20px','left':'124px'});
	this.checkBorder = document.createElement("input");
	this.checkBorder.type = "checkbox";
	this.checkBorder.disabled = "true";
	//this.checkBorder.checked = "true";
	$(this.checkBorder).css({'position':'absolute','top':'20px','left':'164px'});
	
	this.edgeSpan = document.createElement("span");
	this.edgeSpan.appendChild(document.createTextNode("Edge options:"));
	$(this.edgeSpan).css({'font-weight':'bold','position':'absolute','top':'40px','left':'0px'});
	
	this.checkEdgeColorSpan = document.createElement("span");
	this.checkEdgeColorSpan.appendChild(document.createTextNode("Color:"));
	$(this.checkEdgeColorSpan).css({'position':'absolute','top':'60px','left':'0'});
	this.checkEdgeColor = document.createElement("input");
	this.checkEdgeColor.type = "checkbox";
	this.checkEdgeColor.checked = "true";
	$(this.checkEdgeColor).css({'position':'absolute','top':'60px','left':'32px'});
	
	this.checkEdgeWeightSpan = document.createElement("span");
	this.checkEdgeWeightSpan.appendChild(document.createTextNode("Weight:"));
	$(this.checkEdgeWeightSpan).css({'position':'absolute','top':'60px','left':'59px'});
	this.checkEdgeWeight = document.createElement("input");
	this.checkEdgeWeight.type = "checkbox";
	this.checkEdgeWeight.checked = "true";
	$(this.checkEdgeWeight).css({'position':'absolute','top':'60px','left':'100px'});
	
	this.graphSpan = document.createElement("span");
	this.graphSpan.appendChild(document.createTextNode("Graph options:"));
	$(this.graphSpan).css({'font-weight':'bold','position':'absolute','top':'40px','left':'130px'});
	
	this.checkGraphTypeSpan = document.createElement("span");
	this.checkGraphTypeSpan.appendChild(document.createTextNode("Directed:"));
	$(this.checkGraphTypeSpan).css({'color':'black','position':'absolute','top':'60px','left':'130px'});
	this.checkGraphType = document.createElement("input");
	this.checkGraphType.type = "checkbox";
	//this.checkGraphType.disabled = "true";
	this.checkGraphType.checked = "true";
	$(this.checkGraphType).css({'position':'absolute','top':'60px','left':'180px'});
	
	this.optionBox.appendChild(this.checkNodeColorSpan);
	this.optionBox.appendChild(this.checkPositionSpan);
	//this.optionBox.appendChild(this.checkBorderSpan);
	this.optionBox.appendChild(this.checkEdgeColorSpan);
	this.optionBox.appendChild(this.checkEdgeWeightSpan);
	this.optionBox.appendChild(this.checkGraphTypeSpan);
	this.optionBox.appendChild(this.nodeSpan);
	this.optionBox.appendChild(this.edgeSpan);
	this.optionBox.appendChild(this.graphSpan);
	this.optionBox.appendChild(this.parseButton);	
	this.optionBox.appendChild(this.serializeButton);	
	this.optionBox.appendChild(this.checkPosition);	
	this.optionBox.appendChild(this.checkNodeColor);	
	//this.optionBox.appendChild(this.checkBorder);	
	this.optionBox.appendChild(this.checkEdgeColor);	
	this.optionBox.appendChild(this.checkEdgeWeight);	
	this.optionBox.appendChild(this.checkGraphType);	
	div.appendChild(this.optionBox);
	div.appendChild(this.textElement);
	
	this.initV = true;
	var t = this;
	this.parsingCounter = 1;
	
	this.serializeButton.onclick = function()
	{
		if ( !t.initV ) {
			result  = "//////////////////////////\n";
			/*result += "//   generated by the   //\n";
			result += "//  DOT Parsing plugin  //\n";
			result += "//       of BINViz      //\n";
			result += "//        Graph version " + t.parsingCounter + "\n";
			result += "//////////////////////////\n";*/
			if ( t.checkGraphType.checked ) result += "digraph{ \n";
			else result += "graph{ \n";
			var edges = t.graphView.currentGraph.getEdges();
			var nodes = t.graphView.currentGraph.getNodes();
			for ( var i = 0; i < nodes.length; i++) {
				var iCount = 0;
				//result += nodes[i].getLabel();
				if (t.checkPosition.checked) { result += nodes[i].getLabel() + "[" + "pos=\"" + nodes[i].getPosition().x + "," + nodes[i].getPosition().y + "\""; iCount++;} 
				if (t.checkNodeColor.checked) { 
					if ( iCount > 0 ) result += ",";
					else if ( iCount == 0 ) result += nodes[i].getLabel() + "[";
					result += "color=\"" + t.graphView.style.getNodeStyle(nodes[i].getType()).getInactiveColor()[1] + "\"";
					iCount++;
				}
				if ( iCount > 0 ) result += "];\n";
			}
			for ( var i = 0; i < edges.length; i++ ) {
				if ( t.checkGraphType.checked ) result += edges[i].node1.getLabel() + " -> " + edges[i].node2.getLabel();
				else result += edges[i].node1.getLabel() + " -- " + edges[i].node2.getLabel();
				var iCount = 0;
				//result += nodes[i].getLabel();
				if ( t.checkEdgeColor.checked ) { 
					result += "[" + "color=\"" + t.graphView.style.getEdgeStyle(edges[i].getType()).getInactiveColor(0)[1] + "\""; 
					iCount++;
				}
				if ( t.checkEdgeWeight.checked ) {
					if ( iCount > 0 ) result += ",";
					else if ( iCount == 0 ) result += nodes[i].getLabel() + "[";
					result += "weight=\"" + (edges[i].getWeight() / 100) + "\"";
					iCount++;
				}
				if ( iCount > 0 ) result += "];\n";
				else result += ";\n";
			}
			result += "}\n \n";
			t.textElement.appendChild(document.createTextNode(result));
			t.parsingCounter++;
		}
	}
	
	this.initV = false;
	
	return div;
}