<template>
  <div>
    <Question_Vrai_Faux v-show="questionParsee.type_question === 'truefalse'" v-bind:questionParsee="questionParsee"></Question_Vrai_Faux>
    <Question_Reponse_Courte v-show="questionParsee.type_question === 'shortanswer'" v-bind:questionParsee="questionParsee"></Question_Reponse_Courte>
    <Question_Choix_Multiple v-show="questionParsee.type_question === 'multichoice'" v-bind:questionParsee="questionParsee"></Question_Choix_Multiple>
    <Question_Numerique v-show="questionParsee.type_question === 'numerical'" v-bind:questionParsee="questionParsee"></Question_Numerique>
    <Question_Calculee_Simple v-show="questionParsee.type_question === 'calculatedsimple'" v-bind:questionParsee="questionParsee" ></Question_Calculee_Simple>
  </div>
</template>

<script>
import Service from '../src/service/Service'
import Question_Vrai_Faux from "./components/moodle/Question_Vrai_Faux";
import Question_Reponse_Courte from "./components/moodle/Question_Reponse_Courte";
import Question_Choix_Multiple from "./components/moodle/Question_Choix_Multiple";
import Question_Numerique from "./components/moodle/Question_Numerique";
import Question_Calculee_Simple from "./components/moodle/Question_Calculee_Simple";

export default {
//calculatedsimple
  name: "Previsualiser_Moodle",
  components: {
    Question_Numerique,
    Question_Choix_Multiple,
    Question_Vrai_Faux,
    Question_Reponse_Courte,
    Question_Calculee_Simple
  },
  data: function () {
    return {
      questionParsee: ''
    }
  },
  props: {
    // texte du contenu survolé
    id: {},
    // provenance du contenu survolé
    provenance: {},
    // source du contenu survole
    source: {},
    // declencheur de changement de version
    changerVersion: {}
  },
  mounted() {
    /**
     * @vuese
     * A chaque fois que prévisualiser_moodle est appelé on fait appel a parserTexte pour permettre d'appeler le bon composant d'affichage
     */
    let that = this

    if (this.source === 'moodle') {
      if (!(this.provenance === 'en-cours' || this.provenance === 'maj')) {
        Service.parserTexte(JSON.stringify(this.id)).then(function (response) {
          that.questionParsee = response.data
        })
      } else {
        Service.parserTexteEnCours(JSON.stringify(this.id)).then(function (response) {
          that.questionParsee = response.data
        })
      }
    }
  },
  watch: {
    /** @vuese
     *  Si l'utilisateur navigue entre les versions, permet de récupérer le texte associé
     */
    changerVersion: function () {
      let that = this

      if (this.id !== undefined) {
        if (this.provenance !== 'en-cours' && this.provenance !== 'maj') {
          Service.parserTexteVersion(JSON.stringify(this.id), this.changerVersion).then(function (response) {
            that.questionParsee = response.data
          })
        } else {
          Service.parserTexteEnCours(JSON.stringify(this.id), this.changerVersion).then(function (response) {
            that.questionParsee = response.data
          })
        }
      }
    }
  }
}
</script>

<style scoped>

</style>