"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = createHtmlQuizAtom;

var _handlebars = _interopRequireDefault(require("handlebars"));

var _templates = require("../templates");

var _quizAtom = require("./quizAtom");

var _utils = require("../utils");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Create HTML content for QuizAtom
 * @param {object} atom atom json
 * @param {string} targetDir path to save the assets folder for videos
 * @param {string} prefix prefix for file name
 * @returns {string} HTML content
 */
async function createHtmlQuizAtom(atom, targetDir, prefix) {
  try {
    const {
      question
    } = atom;
    const semanticType = question.semantic_type;
    let promiseQuizQuestion;
    let promiseQuizUserAnswer; // download instruction video if available

    const youtubeIdQuestion = atom.instruction && atom.instruction.video ? atom.instruction.video.youtube_id : '';
    const videoQuestion = await (0, _utils.createHtmlVideo)(youtubeIdQuestion, targetDir, prefix, atom.title); // process different semantic types of QuizAtom

    if (semanticType === 'ProgrammingQuestion' || semanticType === 'IFrameQuestion') {
      promiseQuizQuestion = (0, _quizAtom.createHtmlQuizProgrammingQuestion)(atom);

      if (global.optRenderUserQuizAnswer) {
        promiseQuizUserAnswer = (0, _quizAtom.createHtmlProgrammingQuestionUserAnswer)(atom.user_state);
      }
    } else if (semanticType === 'ImageFormQuestion') {
      promiseQuizQuestion = (0, _quizAtom.createHtmlQuizImageFormQuestion)(atom, targetDir, prefix);
    } else {
      const msg = 'Unknown quiz type. Please contact the developer to make it compatible with this atom type!';
      promiseQuizQuestion = new Promise(resolve => resolve(msg));
    }

    const htmlQuiz = await promiseQuizQuestion;
    const htmlQuizUserAnswer = await promiseQuizUserAnswer; // all other promises

    const youtubeIdAnswer = atom.answer && atom.answer.video ? atom.answer.video.youtube_id : '';
    const promiseDownloadYoutubeAnswer = (0, _utils.createHtmlVideo)(youtubeIdAnswer, targetDir, prefix, atom.title);
    const promiseLoadTemplate = (0, _templates.loadTemplate)('atom.quiz');
    const [videoAnswer, html] = await Promise.all([promiseDownloadYoutubeAnswer, promiseLoadTemplate]);
    const instruction = atom.instruction ? await (0, _utils.createHtmlText)(atom.instruction.text, targetDir, atom.id) : '';
    const answerText = atom.answer ? await (0, _utils.createHtmlText)(atom.answer.text, targetDir, atom.id) : '';
    const hasSolution = videoAnswer || answerText;
    const hasInstruction = videoQuestion || instruction;
    const dataTemplate = {
      answerText,
      instruction,
      videoQuestion,
      videoAnswer,
      hasSolution,
      hasInstruction,
      htmlQuiz,
      htmlQuizUserAnswer
    };

    const template = _handlebars.default.compile(html);

    return template(dataTemplate);
  } catch (error) {
    throw error;
  }
}