"use strict";


function RendererBase()
{
}


RendererBase.prototype = {
	/**
	 * @type {Function}
	 */
	constructor: RendererBase,


	/**
	 * @param {String} strFieldName
	 * @param {*} mxValue
	 * @param {Object} objRow
	 * @param {{bTextOnly: boolean, bAllowEmptyText: boolean}} objOptions
	 *
	 * @returns {Element|null}
	 */
	render: function(strFieldName, mxValue, objRow, objOptions, controller)
	{
		if(!objOptions)
			objOptions=RendererBase.makeDefaultOptions();

		return null;
	},


	/**
	 * @returns {string|null}
	 */
	renderLabel: function()
	{
		return null;
	},


	/**
	 * @param arrFields
	 * @returns {*}
	 */
	appendColumnDependencies: function(arrFields)
	{
		for(var i in arrFields)
		{
			if(this.columnDependencies()[arrFields[i]])
			{
				arrFields.push.apply(arrFields, this.columnDependencies()[arrFields[i]]);
			}
		}

		// Remove duplicates.
		return arrFields.reduce(function(a, b){
			if (a.indexOf(b) < 0 ) a.push(b);
			return a;
		},[]);
	},


	/**
	 * @param {String} strFieldName
	 * @param {String} strTag
	 * @param {*} mxValue
	 *
	 * @returns {Array}
	 */
	fieldToCSSClassNames: function(strFieldName, strTag, mxValue)
	{
		return [];
	},


	/**
	 * Returns an Object with a field name as key and an array of field names as values.
	 *
	 * @returns {Object}
	 */
	columnDependencies: function()
	{
		return {};
	},


	shouldDisplayFieldLabelIfPossible: function(strFieldName)
	{
		return true;
	}
};


/**
 * @param {String} strFieldName
 * @param {*} mxValue
 * @param {Array} arrRow
 * @param {Boolean} bTextOnly
 * @param {String} strToolTipMode
 *
 * @returns {Text|Element}
 */
RendererBase.renderDate = function(strFieldName, mxValue, arrRow, bTextOnly, strToolTipMode)
{
	var elReturn;

	var nUnixtimeSeconds = parseInt(new Date(mxValue).getTime()/1000);

	if(bTextOnly)
	{
		elReturn = document.createTextNode(mxValue.substr(0, 10));
	}
	else if(nUnixtimeSeconds)
	{
		strToolTipMode = (typeof strToolTipMode === "undefined") ? "relative" : strToolTipMode;

		RendererBase.bTimeAgoServerSet = false;

		elReturn = document.createElement("span");
		elReturn.innerHTML = Utils.htmlSpecialChars(mxValue.substr(0, 10));

		new ClickTooltipTime(
			nUnixtimeSeconds,
			elReturn,
			strToolTipMode
		);
	}
	else
	{
		elReturn=document.createTextNode("");
	}

	return elReturn;
};


/**
 * @param {String} strFieldName
 * @param {*} mxValue
 * @param {Array} arrRow
 * @param {Boolean} bTextOnly
 * @param {String} strToolTipMode
 *
 * @returns {Text|Element}
 */
RendererBase.renderTimestamp = function(strFieldName, mxValue, arrRow, bTextOnly, strToolTipMode, bDisplayNoneIfNull)
{
	var elReturn;

	var nUnixtimeSeconds = parseInt(new Date(mxValue).getTime()/1000);

	if(typeof bDisplayNoneIfNull === "undefined")
	{
		bDisplayNoneIfNull = true;
	}

	if(bTextOnly)
	{
		elReturn = document.createTextNode(mxValue);
	}
	else if(isNaN(nUnixtimeSeconds) || nUnixtimeSeconds === 0)
	{

		if(bDisplayNoneIfNull === true)
		{
			elReturn = document.createElement("span");
			elReturn.innerHTML = "<i>" + Texts.NONE + "</i>";
		}
	}
	else if(nUnixtimeSeconds){
		strToolTipMode = (typeof strToolTipMode === "undefined") ? ClickTooltipTime.displayRelative : strToolTipMode;

		FieldRendererDefault.bTimeAgoServerSet = false;
		if(!FieldRendererDefault.bTimeAgoServerSet)
		{
			//Helps display correct relative timestamps for users with incorrect operating system timezone.
			FieldRendererDefault.bTimeAgoServerSet = true;
			TimeAgo.serverTime(parseInt(new Date().getTime()/1000));
		}

		elReturn = document.createElement("span");
		elReturn.className = (strToolTipMode == ClickTooltipTime.displayRelative) ? "TimeAgo" : "";
		elReturn.setAttribute("data-unixtime-seconds", nUnixtimeSeconds);
		elReturn.innerHTML = TimeAgo.singleton.relativeTime(nUnixtimeSeconds);

		new ClickTooltipTime(
			nUnixtimeSeconds,
			elReturn,
			strToolTipMode
		);
	}
	else
	{
		elReturn=document.createTextNode("");
	}

	return elReturn;
};

/**
 * @returns {{bTextOnly: boolean, bAllowEmptyText: boolean, bDoNotRenderYesNo: boolean, nMultiRowIndex: number}}
 */
RendererBase.makeDefaultOptions=function()
{
	return {
		bTextOnly: false,
		bAllowEmptyText: false,
		bDoNotRenderYesNo: false,
		nMultiRowIndex: 0
	};
};


/**
 * @param {String} strFieldName
 * @param {Text|Element|undefined} elNode
 * @param {String|undefined} strRenderedValue
 * @param {String|null|Number} mxValue
 *
 * @returns {null|Element}
 */
RendererBase.renderFinish=function(strFieldName, elNode, strRenderedValue, mxValue)
{
	if(
		elNode===undefined
		&& strRenderedValue===undefined
	)
	{
		return null;
	}

	if((mxValue==="" || mxValue===null || mxValue===undefined) && strRenderedValue==="" && elNode===undefined)
	{
		return null;
	}

	if(!elNode)
	{
		elNode=document.createTextNode(strRenderedValue);
	}

	var span = document.createElement("span");
	span.className = ValueToDOMRenderers.singleton.fieldToClassName(strFieldName, /*strTag*/ "", mxValue);
	span.appendChild(elNode);
	//span.appendChild(document.createElement("br"));
	span.style["word-wrap"] = "break-word";

	return span;
};


/**
 * @param {Number} nSeconds
 *
 * @return string
 */
RendererBase.renderSeconds=function(nSeconds)
{
	var strRenderedValue="";
	var bStripLeadingZeroes=true;
	if(Math.floor(nSeconds/3600) > 0)
	{
		strRenderedValue+=parseInt(Math.floor(nSeconds / 3600))+"h ";
		bStripLeadingZeroes=false;
	}

	if(
		Math.floor(nSeconds / 3600) > 0
		|| Math.floor((nSeconds / 60) % 60) > 0
	)
	{
		if(bStripLeadingZeroes)
		{
			strRenderedValue+=parseInt(Math.floor((nSeconds/60)%60))+"m ";
		}
		else
		{
			strRenderedValue+=String("0"+parseInt(Math.floor((nSeconds/60)%60))).substr(-2)+"m ";
		}

		bStripLeadingZeroes=false;
	}

	if(bStripLeadingZeroes)
	{
		strRenderedValue+=(nSeconds % 60)+"s";
	}
	else
	{
		strRenderedValue+=(String("0"+nSeconds % 60).substr(-2))+"s";
	}

	return strRenderedValue;
};
