import $    from 'jquery';
import Qti1 from './qti1/parser';
import Qti2 from './qti2/parser';
//import EdX  from './edX/parser'; // For now we don't need edX

export const AssessmentFormats = {
  Qti1: "QTI1",
  Qti2: "QTI2",
  EdX: "EDX"
};

// Determines the type of provided assessment
export const getAssessmentFormat = (xml) => {

  const assessmentXml   = xml.find('assessment').addBack('assessment');
  const questestinterop = xml.find('questestinterop').addBack('questestinterop'); // <questestinterop xmlns="http://www.imsglobal.org/xsd/ims_qtiasiv1p2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.imsglobal.org/xsd/ims_qtiasiv1p2 http://www.imsglobal.org/xsd/ims_qtiasiv1p2p1.xsd">
  const sequential      = xml.find('sequential').addBack('sequential');
  if(assessmentXml.length > 0 || questestinterop.length > 0){
    return AssessmentFormats.Qti1;
  } else if(false){ // figure out QTI2 condition
    return AssessmentFormats.Qti2;
  } else if(sequential.length > 0){
    return AssessmentFormats.EdX;
  } else {
    throw "Open Assessments could not find valid QTI or EdX XML. Nothing will be rendered. Please verify that your XML meets one of these standards.";
  }

};

// Determines what type of assessment has been passed and parses it correctly,
// whatever type it is.
export const parse = (settings, data) => {

  const xml             = $($.parseXML(data));
  const assessmentXml   = xml.find('assessment').addBack('assessment');
  const sequential      = xml.find('sequential').addBack('sequential');
  const format          = getAssessmentFormat(xml);

  switch(format){
    case AssessmentFormats.Qti1:
      return {
        assessment: Qti1.parse(settings.get("assessmentId"), assessmentXml, xml),
        format: format
      };
    case AssessmentFormats.Qti2:
      return {
        assessment: null,
        format: format
      };
    case AssessmentFormats.EdX:
      return {
        assessment: EdX.parse(settings, sequential),
        format: format
      };
  }

};
@@ -1674,38 +1674,16 @@
   return
- %7B%0A        assessment:
  Qti1.pa
@@ -1739,40 +1739,8 @@
 xml)
-,%0A        format: format%0A      %7D
 ;%0A  
@@ -1787,66 +1787,12 @@
 urn 
-%7B%0A        assessment: null,%0A        format: format%0A      %7D
+null
 ;%0A  
@@ -1837,30 +1837,8 @@
 turn
- %7B%0A        assessment:
  EdX
@@ -1869,45 +1869,211 @@
 ial)
-,%0A        format: format%0A      %7D;%0A  %7D%0A%0A%7D;
+;%0A  %7D%0A%0A%7D;%0A%0A%0A%0A%0A// TODO have to figure out how to handle assessment parsing. We could just%0A// Store the raw data in the state and let the selectors figure out the real details%0A// by parsing the data on demand.

Revert to returning assessment. Adds TODO comments