"use strict";

function RenderFormat()
{
}

RenderFormat.prototype =
{
	htmlRowsCollapseObjects: function(objToBeCollapsed, bNormalizeObject)
	{
		if(bNormalizeObject)
		{
			objToBeCollapsed = this.normalizeObject(objToBeCollapsed);
		}
		var mxFirstElementKey = 0;
		for(var mxKey in objToBeCollapsed)
		{
			var mxFirstElementKey = mxKey;
			break;
		}

		var arrHtmlRowsCollapsed = [];

		if(!objToBeCollapsed || !objToBeCollapsed[mxFirstElementKey])
		{
			arrHtmlRowsCollapsed["cell_values"] = [];
			arrHtmlRowsCollapsed["cell_properties"] = [];
		}
		else
		{
			arrHtmlRowsCollapsed = this._collapseArraySubrowsToHTMLTableRowsArray(Object.keys(objToBeCollapsed[mxFirstElementKey]), objToBeCollapsed);
		}

		var arrColapsedFormat=[];
		arrColapsedFormat["rows"]=[];
		arrColapsedFormat["rows"]["cell_values"] = arrHtmlRowsCollapsed["cell_values"];
		arrColapsedFormat["rows"]["cell_properties"] = arrHtmlRowsCollapsed["cell_properties"];

		if(arrColapsedFormat["rows"]["cell_properties"].length == 0)
		{
			arrColapsedFormat["rows_total"] = 0;
		}

		return arrColapsedFormat;
	},


	_collapseArraySubrowsToHTMLrowRecursive: function(objSubrows, arrHTMLRows, bRowCreated)
	{
		arrHTMLRows = (typeof arrHTMLRows === "undefined") ? [] : arrHTMLRows;
		bRowCreated = (typeof bRowCreated === "undefined") ? false : bRowCreated;

		for(var arrRowKey in objSubrows)
		{
			var arrRow = objSubrows[arrRowKey];

			if(!bRowCreated)
			{
				arrHTMLRows.push([]);
			}

			var arrHTMLCells=arrHTMLRows[arrHTMLRows.length - 1];

			for(var strColumn in arrRow)
			{
				var mxValue = arrRow[strColumn];

				if(strColumn.substr(0,1)==="_")
				{
					continue;
				}

				if(!arrRow.hasOwnProperty("_rowspan"))
				{
					arrRow["_rowspan"]=1;
				}

				arrHTMLRows[arrHTMLRows.length - 1][strColumn]=[];
				arrHTMLRows[arrHTMLRows.length - 1][strColumn]["_rowspan"] = arrRow["_rowspan"];
				arrHTMLRows[arrHTMLRows.length - 1][strColumn]["value"] = mxValue;

			}

			if(arrRow.hasOwnProperty("_subrows"))
			{
				arrHTMLRows = this._collapseArraySubrowsToHTMLrowRecursive(arrRow["_subrows"], arrHTMLRows, true);
			}

			bRowCreated=false;

		}
		return arrHTMLRows;
	},

	_collapseArraySubrowsToHTMLTableRowsArray: function(arrColumns, objSubrows)
	{
		var arrHTMLRows = [];

		arrHTMLRows = this._collapseArraySubrowsToHTMLrowRecursive(objSubrows, arrHTMLRows);

		var arrHTMLRowsValues=[];
		var arrHTMLRowsCellProperties=[];

		for(var nHTMLRowIndex in arrHTMLRows)
		{
			var arrHTMLCells = arrHTMLRows[nHTMLRowIndex];
			var arrHTMLCellsValues=[];
			var arrHTMLCellProperties=[];

			for(var nColumnKey in arrColumns)
			{
				var strColumn = arrColumns[nColumnKey];

				if(arrHTMLCells.hasOwnProperty(strColumn))
				{
					arrHTMLCellsValues[strColumn]=arrHTMLCells[strColumn]["value"];
					if(!arrHTMLCellProperties.hasOwnProperty(strColumn))
					{
						arrHTMLCellProperties[strColumn]=[];
					}
					arrHTMLCellProperties[strColumn]["rowspan"]=arrHTMLCells[strColumn]["_rowspan"];
				}
			}

			arrHTMLRowsValues.push(arrHTMLCellsValues);
			arrHTMLRowsCellProperties.push(arrHTMLCellProperties);
		}

		var arrReturn = [];
		arrReturn["cell_values"]=arrHTMLRowsValues;
		arrReturn["cell_properties"]=arrHTMLRowsCellProperties;

		return arrReturn;
	},

	normalizeObject: function(objToBeCollapsed)
	{
		var arrColumns = [];

		for(var mxKey in objToBeCollapsed)
		{
			for(var strObjectKey in  objToBeCollapsed[mxKey])
			{
				arrColumns.push(strObjectKey);
			}
			break;
		}

		for(var nIndex in objToBeCollapsed)
		{
			for(var nDisplayIndex in arrColumns)
			{
				if(!objToBeCollapsed[nIndex].hasOwnProperty(arrColumns[nDisplayIndex]))
				{
					objToBeCollapsed[nIndex][arrColumns[nDisplayIndex]] = ""
				}
			}
		}

		return objToBeCollapsed;
	}
}