/*
 *
 * Copyright (C) 2009 EmbedOne.
 * Contact: EmbedOne Software Information (info@embedone.com)
 *
 * Commercial Usage
 * Licensees holding valid EmbedOne Commercial licenses may use this file
 * in accordance with the EmbedOne Commercial License Agreement provided
 * with the Software or, alternatively, in accordance with the terms
 * contained in a written agreement between you and EmbedOne.
 *
 * GNU General Public License Usage
 * Alternatively, this file may be used under the terms of the GNU
 * General Public License version 3.0 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.  Please review the following information to
 * ensure the GNU General Public License version 3.0 requirements will be
 * met: http://www.gnu.org/copyleft/gpl.html.
 *
 * If you are unsure which license is appropriate for your use, please
 * contact the EmbedOne sales department at sales@embedone.com.
 *
 */

var objTxtStat = new oTxtStat();

// Creating an object with multiple constants
function oTxtStat(){
	this.iTitle = "<h1>Statistics</h1>";
	this.iSubNav ="<ul> "
					+"</ul>"
					;
//	this.iContent = '<textarea name="data" id="data" rows="10" cols="80" readonly></textarea>';
	this.iContent = '<div id="data" ></div>';
	this.iActions = '<input name="Refresh" type="submit" id="button" value="Refresh" onclick="mRefreshStat();" /> ' 
					;
	this.iFooter = '<div id="copyright"> '
			+"<p>Copyright &copy; 2009 EmbedOne. All rights reserved.</p> "
			+"</div>"
			;
}

var vOldStatData;
function mRefreshStat() {
	mSetWrapper( numConstWrapperStateWaiting );
	mSendStat("*", "mRefreshComplete");
}

function mRefreshComplete(dData){
	//alert(dData[0].key);
	mSetWrapper( numConstWrapperStateMain );
	//alert("done");
	if( dData != null ){
		document.getElementById("data").innerHTML = mStatDisplayFromData(dData);
	}
	//vOldStatData = document.getElementById("data").innerHTML;
}


// ======================================
function mStatDisplayFromData( dData ){
	var dDisplay = "";
	vOldStatData = "";
	
	for(var vCkl = 0;  vCkl < dData.length; vCkl++ ){
		dDisplay = dDisplay + dData[vCkl].key + "="+ dData[vCkl].value + "<br>";
		vOldStatData = vOldStatData + dData[vCkl].key + "="+ dData[vCkl].value + "<br>";
	}
	return dDisplay;
}


// statistics request could be a long one, hence we need to 
// send the request (we always refresh atm)
var gStatCallback;
var gStatTask = 0;
function mSendStat(dKey, dCallback){
	// mSetWrapper( numConstWrapperStateWaiting );
	gStatCallback = dCallback;
	mEmiRpcSend( "emi.stat.get", {"key":dKey,"refresh":true,"rec":true} , "mStat_GetComplete");
}

function mStat_GetComplete(dResult){
	//alert("got one1" + mSimpleJSONStringify(dResult));
	if( dResult == null ){
		// do nothing, its a non-critical error
	}else{
		if( dResult.error != null ){
			if( dResult.error.code != 0 ){ // real error
				// unlike in many other cases, here we can have a provisional error
				if( dResult.error.code == 600 ){
						//alert("Provisional");
						setTimeout("mStat_DelayedSend()", 500);
						return;
					}else{
						eval(gStatCallback + "( null )");	
					}
			} else {
				// TODO: could be a warning or something. Not handling now.
			}
		}
	
		if( dResult.result != null ){
			// we have received some result. However, if data = null and task is present, then
			// its a long request else if data is present, we return the data
			if( dResult.result.data == null && dResult.result.task != null ){
				gStatTask = dResult.result.task;
				//alert(gStatTask);
				setTimeout("mStat_DelayedSend()", 500);
			}else{
				eval(gStatCallback + "( dResult.result.data)");
				// enven if null, we leave it for caller to handle
			}
		}else{
						eval(gStatCallback + "( null )");	
		}
	}
}


function mStat_DelayedSend( ){
	//alert("about to send" + gStatTask);
	mEmiRpcSend( "emi.stat.get", {"task":gStatTask} , "mStat_GetComplete");
}
