/**
 * @fileOverview
 * Objects that write the result of DAAPI discovery.
 * The Nyx main library MUST be loaded before this script.
 * 
 * @author Glen Ford (glenford [at] yahoo.com)
 * @namespace NYX
 */

NYX.DiscoWriter = function(idRoot, itemTemplate) {
	//do some inheritance
	NYX.util.obj.extend(this, NYX.TemplateTool);
	
	//required, but don't necessarily have to be set in the constructor call
	this.idRoot = idRoot;
	if (typeof template == "string") this.template = template;
	else template = "@Nyx.PageTitle@";
	
	//optional settings
	this.discoLimit = 99;
	this.untitledText = "Untitled";
	this.unnamedSection = "";
	this.categoryJoin = ", ";
	
	//internal use
	this.discoContent = [];
	
	this.prepareData = function(discoItem) {
		//cleans up a data item; override this as needed to add/subtract post-processing of the flattened data object
		
		//first, flatten 
		var dataObj = this.flattenDaapiItem(discoItem);
		
		//join the Categories into a string
		if (typeof discoItem.Categories == "object") {
			//should be safe to assume an array
			dataObj.Categories = "";
			for (var catIdx = 0; catIdx < discoItem.Categories.length; catIdx++) {
				dataObj.Categories += discoItem.Categories[catIdx].Name + this.categoryJoin;
			}
			if (catIdx > 0) {
				dataObj.Categories = dataObj.Categories.substring(0, dataObj.Categories.length - this.categoryJoin.length);
			}
		}
		
		//make sure the title isn't blank
		if (typeof dataObj.PageTitle != "undefined") {
			if ( NYX.util.string.isBlank(dataObj.PageTitle) ) dataObj.PageTitle = this.untitledText;
		}
		
		//fix null Section
		if (discoItem.Section == null) {
			dataObj.Section = this.unnamedSection;
		} else if (typeof discoItem.Section != "undefined") {
			if ( NYX.util.string.isBlank(discoItem.Section.Name) ) dataObj.Section = this.unnamedSection;
			else dataObj.Section = discoItem.Section.Name;
		}
		
		return dataObj;
	}
	
	this.writeGui = function(daapiResponse) {
		/* this method expects the daapiResponse to be a valid 
		 * DiscoverContentAction.DiscoveredContent array (
		 * or something with the same interface)
		 */
		//console.dir(daapiResponse); return false;
		this.discoContent = daapiResponse;
		
		var discoData, itemIdx, loopLimit, html, targetElem;
		targetElem = this.getElem(this.DOM_TARGET_SUFFIX);
		
		this.initGui();
		loopLimit = this.discoContent.length < this.discoLimit ? this.discoContent.length : this.discoLimit;
		for (itemIdx = 0; itemIdx < loopLimit; itemIdx++) {
			//get processable data
			discoData = this.prepareData(this.discoContent[itemIdx]);
			//console.group("DiscoWriter content item " + itemIdx); console.dir(discoData); console.groupEnd();
			
			//do the template replacements for this item and append the HTML to the content target
			html = this.processTemplate(discoData, this.template);
			
			//call the customizable method to do more replacements
			html = this.customizeItem(discoData, html, targetElem, itemIdx);
			
			targetElem.innerHTML += html;
		}
		
		this.finishGui();
		
		//show the content and hide the wait message/spinner
		this.showTarget(targetElem);
	}
	
	//these methods are empty; they are called during the GUI-writing process to let you add effects/features, and have access to all the instance members
	this.initGui = function() {
		//override this method as needed to add your own GUI customizations
		return true;
	}
	this.finishGui = function() {
		//override this method as needed to add your own GUI customizations
		return true;
	}
	this.customizeItem = function(dataObj, itemHtml, domElemThisWritesTo, itemIndex) {
		//override this method as needed to add your own GUI customizations
		return itemHtml;	//IMPORTANT: always return the itemHtml (processed or otherwise)
	}
}
