/**
 * 
 */

document.addEventListener("DOMContentLoaded", function(event) 
{ 
                var body = document.getElementsByTagName("body")[0];
                
                var buttons = [];
                button1 = createButtonElement('Read Form Data',Test.onClickGetFormData);
                buttons.push(button1);
                button2 = createButtonElement('Read Full Form Data',Test.onClickGetFormDataFull);
                buttons.push(button2);
                button3 = createButtonElement('Clear Form ',Test.onClickClearForm);
                buttons.push(button3);
                button4 = createButtonElement('Write Form Data',Test.onClickWriteData);
                buttons.push(button4);
                button5 = createButtonElement('Validate',Test.onValidate);
                buttons.push(button5);
                button6 = createButtonElement('Validate as Draft',Test.onValidateAsDraft);
                buttons.push(button6);
                button7 = createButtonElement('Validate as Draft Warnings',Test.onValidateFormDataAsDraftWarnings);
                buttons.push(button7);
                button8 = createButtonElement('Inject JSON text',Test.onInjectJson);
                buttons.push(button8);
                /*
                button9 = createButtonElement('Print Item Tree',Test.onPrintItemTree);
                buttons.push(button9);
                */
                button10 = createButtonElement('Print labels CSV',Test.onPrintCSV);
                buttons.push(button10);
                button11 = createButtonElement('Print form',Test.onPrintForm);
                buttons.push(button11);
                
                insertButtonsToBody(buttons,body);
});


function createButtonElement(buttonText, onClickFunction)
{
                var m_button = document.createElement('input');
                m_button.type = 'button';
                m_button.value = buttonText;
                m_button.onclick = onClickFunction;
                
                return m_button;
}

function insertButtonsToBody(buttons, context)
{
	for(i = buttons.length - 1; i >= 0;i--)
	{
		context.insertBefore(buttons[i],context.childNodes[0]);
		if(i > 0)
			context.insertBefore(document.createTextNode("\r\n"),context.childNodes[0]);
	}
}


function Test()
{
	// we need this solely for eclipse outline to put everything under Test, 
	// otherwise we would have two items in the outline
	this._dummy = null;
}

var FORM_JSON = null;

Test.onClickGetFormData = function()
{
	FORM_JSON = getFormData();
	console.log(FORM_JSON);
}

Test.onClickGetFormDataFull = function()
{
	FORM_JSON = getFormDataFull();
	console.log(FORM_JSON);
}


Test.onClickWriteData = function()
{
	if (FORM_JSON != null)
		setFormData(FORM_JSON);
}

Test.onClickClearForm = function()
{
	clearFormData();
}

Test.onValidate = function()
{
	// false - means regular validation: true - means validate As Draft
	validateFormData(false);
}

Test.onValidateAsDraft = function()
{
	// false - means regular validation: true - means validate As Draft
	validateFormData(true);
}

Test.onValidateFormDataAsDraftWarnings = function()
{
	validateFormDataAsDraftWarnings();
}


Test.onPrintItemTree = function()
{
	var s = MetaModel.toString();
	console.log(s);
}

Test.readFields = function(parent, model, data)
{
	for (var sItem in model)
	{
		if (sItem === "sedPackage" || sItem === "sedGVer" || sItem === "sedVer" || sItem.indexOf("$$") == 0)
			continue;
		
		var item = model[sItem];
		
		if(item.$$name) {
			data += item.$$name+",\n";
			data = Test.readFields(model, item, data);
		}
	}
	
	return data;
}

Test.onPrintCSV = function()
{
	var metaModel = getMetaModel();
	//console.log(metaModel);
	var csvContent = "data:text/csv;charset=utf-8,";
	var name;
	
	for (var sItem in metaModel)
	{
		if (sItem === "sedPackage" || sItem === "sedGVer" || sItem === "sedVer" || sItem.indexOf("$$") == 0)
			continue;
		
		var item = metaModel[sItem];
	
		if(name == undefined && item.$$name) {
			name = item.$$name;
			csvContent += name+";\n";
			csvContent = Test.readFields(metaModel, item, csvContent);
		}
	}
	
	//csvContent = metaModel.toString();
	var encodedUri = encodeURI(csvContent);
	var link = document.createElement("a");
	link.setAttribute("href", encodedUri);
	link.setAttribute("download", name?name+".csv":"SEDdata.csv");
	document.body.appendChild(link); // Required for FF

	link.click(); // This will download the data file
}

Test.onInjectJson = function()
{
	var sJson = prompt("Please enter your JSON text");
	if(sJson != null){
		setFormData(sJson);
	}
	FORM_JSON = sJson;

}

Test.onPrintForm = function()
{
	printForm();
	return;
}

