
function  xml2json(xmlData) {
// console.clear();
console.log('xml to json converstion');
// console.log(xmlData);z

function getAttributesOfXMLNode(node)
{
	// Get attributes of the node (if any)
	 attributes = {};
  if (node.attributes.length > 0) {
    for (let i = 0; i < node.attributes.length; i++) {
      const attribute = node.attributes[i];
      attributes[attribute.nodeName] = attribute.nodeValue;
    }
  }

   return attributes;
}

function getAttributesOfXMLNodeWithoutArray(node)
{   
	// console.log('xmal attributes');
	// console.log(node);
	// Get attributes of the node (if any)
	 attributes = {};
  if (node.attributes.length > 0) {
    for (let i = 0; i < node.attributes.length; i++) {
      const attribute = node.attributes[i];
      attributes = attribute.nodeValue;
    }
  }

   return attributes;
}

function getQuestionsNodes(questionNode,type)
{
		// console.log(questionNode);
		var questionObj = {};
		if(type == 1)
		{
			questionObj['question'] = (questionNode.childNodes[0].firstChild !== null? questionNode.childNodes[0].firstChild.nodeValue : "");
			questionObj['question_description'] = (questionNode.childNodes[1].firstChild !== null? questionNode.childNodes[1].firstChild.nodeValue : "");
			questionObj['question_type'] = 'text';
			questionObj['attributes'] = getAttributesOfXMLNode(questionNode);
			questionObj['titleMath'] = getAttributesOfXMLNodeWithoutArray(questionNode.childNodes[0]);
			questionObj['descriptionMath'] = getAttributesOfXMLNodeWithoutArray(questionNode.childNodes[1]);
			questionObj['question_sub_type'] = 'false';
			questionObj['marks'] = (questionNode.childNodes[2].firstChild !== null ? questionNode.childNodes[2].firstChild.nodeValue : "");
	 
	}
	else
	{
			questionObj['question'] = (questionNode.childNodes[0].firstChild !== null? questionNode.childNodes[0].firstChild.nodeValue : "");
			questionObj['question_description'] = (questionNode.childNodes[1].firstChild !== null? questionNode.childNodes[1].firstChild.nodeValue : "");
			questionObj['question_type'] = 'group';
			questionObj['attributes'] = getAttributesOfXMLNode(questionNode);
			questionObj['titleMath'] = getAttributesOfXMLNodeWithoutArray(questionNode.childNodes[0]);
			questionObj['descriptionMath'] = getAttributesOfXMLNodeWithoutArray(questionNode.childNodes[1]);
			questionObj['question_sub_type'] = questionNode.childNodes[3].firstChild.nodeValue;
			questionObj['marks'] = (questionNode.childNodes[2].firstChild !== null ? questionNode.childNodes[2].firstChild.nodeValue : "");
			questionObj['answers'] = [];
			questionObj['correct_answer'] = [];
		
		var choiceList = questionNode.childNodes[4].childNodes;
				
				if(choiceList.length > 0)
				{
					for (let k = 0; k < choiceList.length ;k++) 
					{
						var tempChoiceobj = {};
						 tempChoiceobj['id'] = choiceList[k].childNodes[0].firstChild.nodeValue;
						 tempChoiceobj['no'] =  (choiceList[k].childNodes[1].firstChild !== null ? choiceList[k].childNodes[1].firstChild.nodeValue : "");
						 tempChoiceobj['choice_name'] =  (choiceList[k].childNodes[2].firstChild !== null ? choiceList[k].childNodes[2].firstChild.nodeValue : "");
						 tempChoiceobj['is_math_enabled'] = getAttributesOfXMLNodeWithoutArray(choiceList[k].childNodes[2]);
						 // console.log(tempChoiceobj);
						 questionObj['answers'].push(tempChoiceobj);

					}
				}
		}
		return questionObj;

}
		
function getQuestionGroupNodes(questionGroup)
{
				var questionGroupObj = {};
				questionGroupObj['group_title'] = (questionGroup.childNodes[0].firstChild !== null ? questionGroup.childNodes[0].firstChild.nodeValue : "");
				questionGroupObj['group_description'] = (questionGroup.childNodes[1].firstChild !== null ? questionGroup.childNodes[1].firstChild.nodeValue : "");
				questionGroupObj['group_marks'] = (questionGroup.childNodes[2].firstChild !== null ? questionGroup.childNodes[2].firstChild.nodeValue : "");
				questionGroupObj['attributes'] = getAttributesOfXMLNode(questionGroup);
				questionGroupObj['questions'] = [];
				//handle questions
				var questionList = questionGroup.childNodes[3].childNodes;
				// console.log('No of quetions in GQ:' + questionList.length);
				for(let q = 0; q < questionList.length ;q++)
				{
						
						var questionType = questionList[q].getAttribute("Type");
						console.log(questionType);
						if(questionType == 'Descriptive')
						{
								questionGroupObj['questions'].push(getQuestionsNodes(questionList[q],1));
						}
						else if(questionType == 'MCQ')
						{
								questionGroupObj['questions'].push(getQuestionsNodes(questionList[q],2));
						}
						else 
						{
								questionGroupObj['questions'].push(getQuestionGroupNodes(questionList[q]));
						}
						
				}
				return questionGroupObj;
}



//busi logic start
console.log(xmlData);
var xmlString = xmlData;
const parser = new DOMParser(); 

// Parse the XML string into an XML document
const xmlDoc = parser.parseFromString(xmlString, 'text/xml');
const jsonObj = {};

jsonObj['question_paper_title'] = xmlDoc.getElementsByTagName("Title")[0] && xmlDoc.getElementsByTagName("Title")[0].childNodes[0] ? xmlDoc.getElementsByTagName("Title")[0].childNodes[0].nodeValue : null;
jsonObj['question_paper_description'] = xmlDoc.getElementsByTagName("Subtitle")[0] && xmlDoc.getElementsByTagName("Subtitle")[0].childNodes[0] ? xmlDoc.getElementsByTagName("Subtitle")[0].childNodes[0].nodeValue : null;
jsonObj['attributes'] = getAttributesOfXMLNode(xmlDoc.documentElement);
jsonObj['sections'] = [];



	sectionsList = xmlDoc.getElementsByTagName("Items")[0].childNodes;
	// console.log(sectionsList);
	// console.log('no of sections:'+sectionsList.length);
	for (let i = 0; i < sectionsList.length ;i++) {
		var tempObj = {};

    // console.log('Section :' + i + '-' + sectionsList[i].childNodes[0].firstChild.nodeValue);
	tempObj['section_title'] = (sectionsList[i].childNodes[0].firstChild !== null ? sectionsList[i].childNodes[0].firstChild.nodeValue : "");
	tempObj['section_description'] = (sectionsList[i].childNodes[1].firstChild !== null ? sectionsList[i].childNodes[1].firstChild.nodeValue : "");
	tempObj['section_marks'] = (sectionsList[i].childNodes[2].firstChild !== null ? sectionsList[i].childNodes[2].firstChild.nodeValue : "");
	tempObj['attributes'] = getAttributesOfXMLNode(sectionsList[i]);
	tempObj['questions'] = [];


		//handle questions
		var questionList = sectionsList[i].childNodes[3].childNodes;
		// console.log('No of quetions:' + questionList.length);
		// console.log("Question List")
		// console.log(questionList);
		for(let j = 0; j < questionList.length ;j++)
		{
				//check weather quesiotn or question grup
				// console.log('QUESTION :' + j );
				// console.log(questionList[j]);
				var questionType = questionList[j].getAttribute("Type");
				// console.log(questionType);
				if(questionType == 'Descriptive')
				{
						tempObj['questions'].push(getQuestionsNodes(questionList[j],1));
				}
				else if(questionType == 'MCQ')
				{
						tempObj['questions'].push(getQuestionsNodes(questionList[j],2));
				}
				else if(questionType == 'QuestionGroup')
				{
					tempObj['questions'].push(getQuestionGroupNodes(questionList[j]));
				}	
		}
    jsonObj['sections'].push(tempObj);
}

    var jsonData = JSON.stringify(jsonObj);
	localStorage.setItem('pjsonData', jsonData );
	console.log('xml2json_for_questionpaper : ',jsonData);
	QuestionPaperPreview(jsonObj);
	
}


