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

var jsonDecodeData = localStorage.getItem('json_Decode_Data_For_Mcq_Crt_Ans');
// console.log(jsonDecodeData);

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 getQuestionsNodes(questionNode,type)
{
		// console.log(questionNode);
		var questionObj = {};
		if(type == 1)
		{
				questionObj['question'] = 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['question_sub_type'] = 'false';
				questionObj['marks'] = (questionNode.childNodes[2].firstChild !== null ? questionNode.childNodes[2].firstChild.nodeValue : "");
				questionObj['display_marks'] = questionNode.childNodes[3].firstChild.nodeValue;
				questionObj['is_math_enabled'] = questionNode.childNodes[4].firstChild.nodeValue;

		}
		else
		{
				questionObj['question'] = 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['question_sub_type'] = questionNode.childNodes[5].firstChild.nodeValue;
				questionObj['marks'] = (questionNode.childNodes[2].firstChild !== null ? questionNode.childNodes[2].firstChild.nodeValue : "");
				questionObj['display_marks'] = questionNode.childNodes[3].firstChild.nodeValue;
				questionObj['is_math_enabled'] = questionNode.childNodes[4].firstChild.nodeValue;
				questionObj['answers'] = [];
				questionObj['correct_answer'] = [];

				var choiceList = questionNode.childNodes[6].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'] = (choiceList[k].childNodes[3].firstChild !== null ? choiceList[k].childNodes[3].firstChild.nodeValue : "");
						 // console.log(tempChoiceobj);
						 questionObj['answers'].push(tempChoiceobj);
					}
				}
				if(jsonDecodeData !== null)
				{   
					var jsonParsedData = JSON.parse(jsonDecodeData);

					// console.log('jsonParsedData : ', jsonParsedData)

					// Iterate over each question
					for (const question of jsonParsedData.questions) {

						const questionId = question.questionId;
						const answers = question.answers;
                        
						console.log(typeof answers)

						// console.log(`Question ID: ${questionId}`);
                                                
                        if(answers !== undefined)  {
						// console.log(`Not Undifined Answer`);
						for (const answer of answers) {

							// console.log(`  Answer ID: ${answer}`);

							if(questionId == questionObj['attributes']['Id']) {
								questionObj['correct_answer'].push(answer); 
							}
						}
					  }
				}
					// console.log('jsonDecodeData.length', jsonDecodeData);
				}
		}
		return questionObj;

}
		
function getQuestionGroupNodes(questionGroup)
{
	var questionGroupObj = {};
	
				questionGroupObj['group_title'] = 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['group_display_marks'] = questionGroup.childNodes[3].firstChild.nodeValue;
				questionGroupObj['group_attempt_any'] = (questionGroup.childNodes[4].firstChild !== null ? questionGroup.childNodes[4].firstChild.nodeValue : "");
				questionGroupObj['attributes'] = getAttributesOfXMLNode(questionGroup);
				questionGroupObj['questions'] = [];
				//handle questions
				var questionList = questionGroup.childNodes[5].childNodes;
				// console.log('No of quetions in GQ:' + questionList.length);
				for(let q = 0; q < questionList.length ;q++)
				{
						
						var questionType = questionList[q].getAttribute("xsi:type");
						if(questionType == 'DescriptiveQuestion')
						{   
							questionGroupObj['questions'].push(getQuestionsNodes(questionList[q],1));
						}
						else if(questionType == 'MultiChoiceQuestion')
						{
							questionGroupObj['questions'].push(getQuestionsNodes(questionList[q],2));
						}
						else
						{
							questionGroupObj['questions'].push(getQuestionGroupNodes(questionList[q]));
							
						}
						
				}
				return questionGroupObj;
}



//busi logic start
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].childNodes[0].nodeValue;
jsonObj['question_paper_description'] = xmlDoc.getElementsByTagName("Subtitle")[0].childNodes[0].nodeValue;
//get attributes of root/question paper element
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]);
		tempObj['section_title'] = sectionsList[i].childNodes[0].firstChild.nodeValue;
		tempObj['section_description'] = sectionsList[i].childNodes[1].firstChild.nodeValue;
		tempObj['section_marks'] = (sectionsList[i].childNodes[2].firstChild !== null ? sectionsList[i].childNodes[2].firstChild.nodeValue : "");
		tempObj['section_display_marks'] = sectionsList[i].childNodes[3].firstChild.nodeValue;
		tempObj['section_attempt_any'] = (sectionsList[i].childNodes[4].firstChild !== null && sectionsList[i].childNodes[4].firstChild !== undefined ? sectionsList[i].childNodes[4].firstChild.nodeValue : "");
		tempObj['attributes'] = getAttributesOfXMLNode(sectionsList[i]);
		tempObj['questions'] = [];

		//handle questions
		var questionList = sectionsList[i].childNodes[5].childNodes;
		// console.log('No of quetions:' + questionList.length);
		// 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("xsi:type");
				// console.log(questionType);
				if(questionType == 'DescriptiveQuestion')
				{
					tempObj['questions'].push(getQuestionsNodes(questionList[j],1));
				}
				else if(questionType == 'MultiChoiceQuestion')
				{
					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);
	// console.log('Xml to Json Conversion ');
	console.log('jsonData : ', jsonData);
	// console.log(jsonObj);
	jsonToHtml(jsonObj);
	// console.log(jsonToHtml(jsonObj))
	localStorage.removeItem('json_Decode_Data_For_Mcq_Crt_Ans');

}


