class ControllTracker {
  constructor() {
    this.scormApi = pipwerks.SCORM.API;
    this.scorm = pipwerks.SCORM;
    this.api = pipwerks.SCORM.API.getHandle();
    //config = manifestJSON;
    this.lmsConnected = null;
    this.statusCurso = null;
  }

  async renderControllerTracker(objeto) {
    this.statusCurso = objeto.checkProject.completed;
    //Pegar o id na URL
    //Verificar se o scorm se encontra conectado com LMS
    if (this.scormApi.isFound) {
      objeto.config.LMSGetValue = true;
      this.initCourse();
      //Se estiver no LMS, o SCORM, se conecta com a API para inserir dados no LMS
      this.scorm.connection.isActive = true;
    } else {
      objeto.config.LMSGetValue = false;
    }

    const arrModulos = this.generateTracker(objeto);

    if (
      objeto.config.LMSGetValue === false ||
      objeto.config.LMSGetValue === undefined
    ) {
      objeto.config.controlTrackModulo = arrModulos;
    }

    if (
      this.scorm.get("cmi.suspend_data") === "" &&
      objeto.config.LMSGetValue === true
    ) {
      objeto.config.controlTrackModulo = arrModulos;
    }

    if (objeto.config.LMSGetValue === true) {
      objeto.config.controlTrackModulo = this.scorm.get("cmi.suspend_data")
        ? JSON.parse(this.scorm.get("cmi.suspend_data"))
        : objeto.config.controlTrackModulo;
      objeto.config.controllerStatusTracker = false;
    }

    return config;
  }

  generateTracker(objeto) {
    let arrModulos = [];

    objeto.config.modulos.map((modulo, index_modulo) => {
      const trackModulo = objeto.checkProject.controllArticle[index_modulo];
      const sectionCheck = objeto.checkProject.sectionCheck[index_modulo];
      const status = objeto.checkProject.status[index_modulo];

      arrModulos.push({
        trackModulo: trackModulo.article,
        sectionCheck: sectionCheck,
        status: status,
      });
    });

    return arrModulos;
  }

  initCourse() {
    //this.scorm.init returns a boolean
    this.lmsConnected = this.api.LMSInitialize("");
    this.scorm.init();
    //If the this.scorm.init function succeeded...
    if (this.lmsConnected) {
      console.log("lmsConnected", this.lmsConnected);
      console.log("Curso Conectado");
      console.log("Versão do scorm", this.scorm.version);

      //Let's get the completion status to see if the course has already been completed
      var completionstatus = this.scorm.get("cmi.core.lesson_status");
      //If the course has already been completed...
      if (completionstatus == "completed" || completionstatus == "passed") {
        //...let's display a message and close the browser window
        this.handleError(
          "You have already completed this course. You do not need to continue."
        );

        config.producao = this.statusCurso ? true : false;
      }
    } else {
      //... let's alert the user then close the window.
      this.handleError("Error: Course could not connect with the LMS");
    }
  }

  finalizarCurso(objeto) {
    //Se o aluno chegar na ultima página o status muda true.
    //Se status for true, o aluno concluiu o curso
    console.log("Concluido");
    if (config.LMSGetValue === true) {
      this.api.LMSSetValue("cmi.core.lesson_status", "completed");
      this.api.LMSCommit("");
      window.controllerLocalStorage({
        action: "home",
        actionModulo: 0,
        completed: true,
        controllArticle: objeto.controllArticle,
        status: objeto.status,
        sectionCheck: objeto.sectionCheck,
      });
    }
  }

  sairCurso() {
    //Let's get the completion status to see if the course has already been completed
    var completionstatus = this.scorm.get("cmi.core.lesson_status");
    //If the course has already been completed...
    if (completionstatus == "completed" || completionstatus == "passed") {
      console.log("Curso concluido");
      this.api.LMSFinish();
    } else {
      console.log("Curso não concluido");
    }
  }

  handleError(msg) {
    console.log(msg);
    // window.close();
  }

  async updateTracker(dados) {
    var arrModulos = this.generateTracker(dados);
    dados.config.controlTrackModulo = arrModulos;
    if (this.scormApi.isFound == false) {
      return;
    } else {
      this.api.LMSSetValue("cmi.suspend_data", JSON.stringify(arrModulos));
      this.api.LMSCommit("");
      return dados.config;
    }
  }
}
