<template>
  <div>

    <Edit_Reponse_Courte @actionMoodle="passerMoodle" v-show="type_question === 'shortanswer'"
                         v-bind:isVersionCourante="isVersionCourante" v-bind:questionParsee="questionParsee"></Edit_Reponse_Courte>
    <Edit_Vrai_Faux @actionMoodle="passerMoodle" v-show="type_question === 'truefalse'"
                    v-bind:isVersionCourante="isVersionCourante"  v-bind:questionParsee="questionParsee"></Edit_Vrai_Faux>
    <Edit_Choix_Multiple @actionMoodle="passerMoodle" v-show="type_question === 'multichoice'"
                         v-bind:isVersionCourante="isVersionCourante"  v-bind:questionParsee="questionParsee"></Edit_Choix_Multiple>
    <Edit_Numerique @actionMoodle="passerMoodle" v-show="type_question === 'numerical'"
                    v-bind:isVersionCourante="isVersionCourante"  v-bind:questionParsee="questionParsee"></Edit_Numerique>
    <Edit_Calculee_Simple @actionMoodle="passerMoodle" v-show="type_question === 'calculatedsimple'"
                          v-bind:isVersionCourante="isVersionCourante"  v-bind:questionParsee="questionParsee"></Edit_Calculee_Simple>
  </div>
</template>

<script>
import Service from "../../service/Service";
import Edit_Reponse_Courte from "./Edit_Reponse_Courte";
import Edit_Vrai_Faux from "./Edit_Vrai_Faux";
import Edit_Choix_Multiple from "./Edit_Choix_Multiple";
import Edit_Numerique from "./Edit_Numerique";
import Edit_Calculee_Simple from "./Edit_Calculee_Simple";

export default {
  name: "Edit_Contenus",
  components: {Edit_Calculee_Simple, Edit_Numerique, Edit_Choix_Multiple, Edit_Vrai_Faux, Edit_Reponse_Courte},
  props: {
    // mode sombre activé ou non
    darktheme: {},
    //l'id du contenu clique
    idContenu: {},
    //la provenance du contenu
    provenance: {},
    // la source du contenu
    source: {},
    // declencheur de changement de version
    changerVersion: {},
    // version courante affichée ?
    versionCourante: {}
  },
  methods: {
    passerMoodle: function (event) {
      if (event.action === 'arriere')
        this.$emit("actionMoodle", {action: event.action})
      else
        this.$emit("actionMoodle", {action: event.action, questionParsee: event.questionParsee})
    }
  },
  data: function () {
    return {
      questionParsee: '',
      type_question: '',
      isVersionCourante: true
    }
  },
  watch: {
    /**
     * @vuese
     *Dès que l'id du contenu cliqué change, on recupere le nouveau texte
     */
    idContenu: function () {
      let that = this

      if (this.source === 'moodle') {
        if (this.provenance !== 'en-cours') {
          Service.parserTexte(JSON.stringify(this.idContenu)).then(function (response) {
            that.questionParsee = response.data
            that.type_question = that.questionParsee.type_question

          })
        } else {
          Service.parserTexteEnCours(JSON.stringify(this.idContenu)).then(function (response) {
            that.questionParsee = response.data
            that.type_question = that.questionParsee.type_question

          })
        }
      }
    },
    /**
     * @vuese
     *  Si l'utilisateur navigue entre les versions, permet de récupérer le texte associé
     */
    changerVersion: function () {
      let that = this
      this.isVersionCourante = this.versionCourante.toString() === this.changerVersion.toString()

      if (this.provenance !== 'en-cours') {
        Service.parserTexteVersion(JSON.stringify(this.idContenu), this.changerVersion).then(function (response) {
          that.questionParsee = response.data
          //that.type_question = that.questionParsee.type_question

        })
      } else {
        Service.parserTexteEnCoursVersion(JSON.stringify(this.idContenu), this.changerVersion).then(function (response) {
          that.questionParsee = response.data
          //that.type_question = that.questionParsee.type_question

        })
      }
    }
  }
}
</script>

<style scoped>

</style>