/*
* Copyright Lars Op den Kamp 2009
* License LGPL
* 
* Beetje uitleg over deze tool:
* Dit is een projectje om een beetje te klooien met OO in javascript.
* Het origineel dat ik gemaakt had was wat minder fancy en geschreven in PHP.
*
* Dingen met TODO moeten nog gefixt worden
*/

var netConnections = [];

/* A connection between two nodes */
var netConnection = Class.create();
netConnection.prototype =
{
	/**
	  * Constructor
	  * @param _parent The parent node
	  * @param _child The child node
	  * @param _status The status of the connection
	  */
	initialize: function(_parent, _child, _status, _vpn_conn) {
		this._parent = _parent;
		this._child = _child;
		this._status = _status;
		this._vpn_conn = (_vpn_conn == undefined || _vpn_conn != true) ? false : true;
	},
	
	/**
	  * Get the parent node
	  */
	getParentNode : function() {
		return this._parent;
	},
	
	/**
	  * Get the child node
	  */
  getChildNode : function() {
  	return this._child;
  },
  
  /**
    * Get the status of the connection
    */
  getStatus : function() {
  	return this._status;
  },
  
  /**
    * Get the status of the connection
    */
  setStatus : function(_status) {
  	this._status = _status;
  },
  
  /**
    * Get the id of this connection
    */
  getId : function() {
  	return "p"+this.getParentNode().getId()+"c"+this.getChildNode().getId();
  },
  
  /**
    * Get the status of the uplink
    */
  getUplinkStatus : function (lastStatus) {
		if (lastStatus == undefined)
		{
			lastStatus = this._status;
		}
		
		if (this._parent.getType() == 'internet')
		{
			return lastStatus;
		}else
		{
			st = this._parent.getParentConnection().getStatus();
			
			if (st == 'offline' || st == 'unknown')
			{
				return st;
			}else
			{
				return lastStatus;
			}
		}
	},
	
	/**
	  * TODO Get the color of a status
	  */
	statusColor : function (st) {
		if (st == undefined)
		{
			st = this._status;
		}
		
		if (st == 'offline')
		{
			return '#ff3333';
		}else if (st == 'online')
		{
			return '#33ff33';
		}else
		{
			return '#ffff33';
		}
	},
	
	draw : function(){
		var _gr = getGraph();
		_gr.setColor(this.statusColor());
		
		if (this._vpn_conn) _gr.setStroke(Stroke.DOTTED);
		else _gr.setStroke(2);
		
		_gr.drawLine(this.getParentNode().getCenterX(), this.getParentNode().getCenterY(), this.getChildNode().getCenterX(), this.getChildNode().getCenterY());
	}
}

function _netChangeConnStatus(_parent, _child, _status)
{
	var con = _netGetConnection(_parent, _child, _status);
	con.setStatus(_status);
}

function _netGetConnection(_parent, _child, _status, _vpn_conn)
{
	var retval = null;
	
	for (var i = 0; i < netConnections.length; i++)
	{
		var item = netConnections[i];
		
		if (item.getParentNode().getId() == _parent && item.getChildNode().getId() == _child)
		{
			retval = item;
			break;
		}
	}
	
	if (retval == null)
	{
		var pn = netGetNode(_parent);
		var cn = netGetNode(_child);
		if (_status == undefined)
		{
			_status = 'unknown';
		}
		
		retval = new netConnection(pn, cn, _status, _vpn_conn);
	
		netConnections[netConnections.length] = retval;
	}
	
	return retval;
}

function netSetConnection(_parent, _child, _status)
{
	var con = _netGetConnection(_parent, _child, _status);
	netGetNode(_parent).setConnection(con);
	netGetNode(_child).setConnection(con);
}

function _netSetVpnConnection(_parent, _child, _status)
{
	var con = _netGetConnection(_parent, _child, _status, true);
	netGetNode(_parent)._setVpnConnection(con);
}