function tofixed(num, precision) {
  if (!num) return num;
  if (precision) {
    return parseFloat(num.toFixed(precision));
  } else {
    return Math.round(num);
  }
}
// paser functions
function parseBaseQuestion(datas) {
  return datas.map(function(item) {
    item.name = item.name;
    item.avgScore = tofixed(item.avgScore, 2);
    item.stdevScore = tofixed(item.stdevScore, 2);
    item.coefficient = tofixed(item.coefficient, 2);
    item.difficulty = tofixed(item.difficulty, 2);
    item.discrimination = tofixed(item.discrimination, 2);
    item.fullRate = tofixed(item.fullRate, 2);
    item.scoreRate = tofixed(item.scoreRate, 2);
    return item;
  });
}
function parseBaseClass(datas) {
  return datas.map(function(item) {
    item.avgScore = tofixed(item.avgScore, 2);
    item.stdevScore = tofixed(item.stdevScore, 2);
    item.difficulty = tofixed(item.difficulty, 2);
    item.coefficient = tofixed(item.coefficient, 2);
    item.excellentRate = tofixed(item.excellentRate, 2);
    item.excellentRate100 = tofixed(item.excellentRate * 100, 2);
    item.passRate = tofixed(item.passRate, 2);
    item.passRate100 = tofixed(item.passRate * 100, 2);
    return item;
  });
}
function parseBaseTeacher(datas) {
  return datas.map(function(item) {
    item.avgScore = tofixed(item.avgScore, 2);
    item.excellentRate = tofixed(item.excellentRate, 2);
    item.excellentRate100 = tofixed(item.excellentRate * 100, 2);
    item.passRate = tofixed(item.passRate, 2);
    item.passRate100 = tofixed(item.passRate * 100, 2);
    item.relativeAvgScore = tofixed(item.relativeAvgScore, 2);
    if (item.classes && item.classes.length) {
      item.classes = parseBaseTeacher(item.classes);
    }
    return item;
  });
}
function parseBaseMainQuestion(datas) {
  return datas.map(function(item) {
    item.avgScore = tofixed(item.avgScore, 2);
    item.stdevScore = tofixed(item.stdevScore, 2);
    item.coefficient = tofixed(item.coefficient, 2);
    item.difficulty = tofixed(item.difficulty, 2);
    item.discrimination = tofixed(item.discrimination, 2);
    return item;
  });
}
function parseBaseQuestionOption(datas) {
  datas.questions = datas.questions.map(function(item) {
    item.optionsRate = {};
    for (key in item.options) {
      item.optionsRate[key] = (
        (100 * item.options[key]) /
        item.totalCount
      ).toFixed(2);
    }
    item.difficulty = tofixed(item.difficulty, 2);
    item.discrimination = tofixed(item.discrimination, 2);
    return item;
  });
  return datas;
}

function parseDiscriminationLevel(datas) {
  return datas.map(function(item) {
    item.groups = item.groups.map(function(elem) {
      elem.levels.excellent.questionRate = (
        (100 * elem.levels.excellent.questionCount) /
        elem.questionCount
      ).toFixed(2);
      elem.levels.good.questionRate = (
        (100 * elem.levels.good.questionCount) /
        elem.questionCount
      ).toFixed(2);
      elem.levels.general.questionRate = (
        (100 * elem.levels.general.questionCount) /
        elem.questionCount
      ).toFixed(2);
      elem.levels.bad.questionRate = (
        (100 * elem.levels.bad.questionCount) /
        elem.questionCount
      ).toFixed(2);
      elem.coefficient = tofixed(elem.coefficient, 2);

      return elem;
    });

    return item;
  });
}
function parseDifficultyLevel(datas) {
  return datas.map(function(item) {
    item.groups = item.groups.map(function(elem) {
      elem.levels.high.questionRate = (
        (100 * elem.levels.high.questionCount) /
        elem.questionCount
      ).toFixed(2);
      elem.levels.middle.questionRate = (
        (100 * elem.levels.middle.questionCount) /
        elem.questionCount
      ).toFixed(2);
      elem.levels.low.questionRate = (
        (100 * elem.levels.low.questionCount) /
        elem.questionCount
      ).toFixed(2);

      elem.difficulty = tofixed(elem.difficulty, 2);
      return elem;
    });

    return item;
  });
}
function parseRangeScoreLevel(datas) {
  return datas.map(function(item) {
    item.rangeRate = tofixed(item.rangeRate, 2);
    item.sumRate = tofixed(item.sumRate, 2);
    return item;
  });
}
function parseRangeTotalScore(datas) {
  return {
    total: parseRangeScoreLevel(datas.total),
    classes: datas.classes.map(function(item) {
      return {
        name: item.name,
        ranges: parseRangeScoreLevel(item.ranges)
      };
    })
  };
}
function parseRangeSegment(datas) {
  return datas.map(function(item) {
    return {
      name: item.name,
      groups: item.groups.map(function(elem) {
        return {
          name: elem.name,
          segments: parseRangeScoreLevel(elem.segments)
        };
      })
    };
  });
}

// data-parse
function dataParse(dataStatic) {
  var cover = {
    examName: dataStatic.examName,
    schoolName: dataStatic.schoolName,
    subjectName: dataStatic.subjectName
  };
  var basic_question_objective = parseBaseQuestion(
    dataStatic.basic_question_objective
  );
  var basic_question_subjective = parseBaseQuestion(
    dataStatic.basic_question_subjective
  );
  var basic_class = parseBaseClass(dataStatic.basic_class);
  var basic_teacher = parseBaseTeacher(dataStatic.basic_teacher);
  var basic_college = parseBaseClass(dataStatic.basic_college);
  var basic_main_question = parseBaseMainQuestion(
    dataStatic.basic_main_question
  );
  var basic_question = basic_question_objective.concat(
    basic_question_subjective
  );

  var discrimination_level = parseDiscriminationLevel(
    dataStatic.discrimination_level
  );
  var difficulty_level = parseDifficultyLevel(dataStatic.difficulty_level);
  var basic_question_option = parseBaseQuestionOption(
    dataStatic.basic_question_option
  );
  var range_level = dataStatic.range_level;
  var range_10_totalScore = parseRangeTotalScore(
    dataStatic.range_10_totalScore
  );
  var range_1_totalScore = parseRangeTotalScore(dataStatic.range_1_totalScore);
  var range_segment_6 = parseRangeSegment(dataStatic.range_segment_6);

  return {
    cover: cover,
    basic_question_objective: basic_question_objective,
    basic_question_subjective: basic_question_subjective,
    basic_class: basic_class,
    basic_teacher: basic_teacher,
    basic_college: basic_college,
    basic_main_question: basic_main_question,
    basic_question: basic_question,
    discrimination_level: discrimination_level,
    difficulty_level: difficulty_level,
    basic_question_option: basic_question_option,
    range_level: range_level,
    range_10_totalScore: range_10_totalScore,
    range_1_totalScore: range_1_totalScore,
    range_segment_6: range_segment_6
  };
}
