function initTimer() {
  document.querySelector(".timer_txt").innerHTML = timeEnd - timer.getTimeValues().seconds;
  document.querySelector(".progress_bar").style.animationPlayState = 'paused';
}

function move() {
  document.querySelector(".timer_txt").innerHTML = timeEnd - timer.getTimeValues().seconds;
  switch (timeEnd - timer.getTimeValues().seconds) {
    case 5:
    case 4:
      document.querySelector(".timer_txt").style.color = "#eca1a6";
      break;
    case 3:
    case 2:
    case 1:
    case 0:
      document.querySelector(".timer_txt").style.color = "#c94c4c";
      break;
    default:
      document.querySelector(".timer_txt").style.color = document.querySelector("#modeDeJeu").style.color;
  }
}

function demarrerTimer() {
  timer.start();
  document.querySelector(".progress_bar").style.animationDuration = timeEnd + "s";
  document.querySelector(".progress_bar").style.animationPlayState = 'running';
}

var timer = new easytimer.Timer();
timer.start();
timer.addEventListener('secondsUpdated', function (e) {
  if (timer.getTimeValues().seconds <= timeEnd) {
    move();
  } else {

    timeTot = timeTot + timer.getTimeValues().seconds;
    endTime();
    timer.stop();
  }
});
function endTime() {
  if (!isTuto) {
    closeGame();
    var score = (niveauActuel * (50 / timeTot)).toFixed(2);
    document.querySelector("#scoreTotGameOver").innerHTML = score;
    document.querySelector("#niveauGameOver").innerHTML = niveauActuel;
    document.querySelector("#TempsTotGameOver").innerHTML = timeTot;
    //calcul stars
    resetStar();
    if (score < 10) {
      document.querySelector("#star1").style.color = "#fe8a71";
    } else if (score > 10 || score < 50) {
      document.querySelector("#star1").style.color = "#fe8a71";
      document.querySelector("#star2").style.color = "#fe8a71";
    } else if (score > 50 || score < 200) {
      document.querySelector("#star1").style.color = "#fe8a71";
      document.querySelector("#star2").style.color = "#fe8a71";
      document.querySelector("#star3").style.color = "#fe8a71";
    }
    else if (score > 200 || score < 400) {
      document.querySelector("#star1").style.color = "#fe8a71";
      document.querySelector("#star2").style.color = "#fe8a71";
      document.querySelector("#star3").style.color = "#fe8a71";
      document.querySelector("#star4").style.color = "#fe8a71";
    } else {
      document.querySelector("#star1").style.color = "#fe8a71";
      document.querySelector("#star2").style.color = "#fe8a71";
      document.querySelector("#star3").style.color = "#fe8a71";
      document.querySelector("#star4").style.color = "#fe8a71";
      document.querySelector("#star5").style.color = "#fe8a71";
    }
    openModal('gameOver');
  }
}

function resetStar() {
  document.querySelector("#star1").style.color = document.body.style.color;
  document.querySelector("#star2").style.color = document.body.style.color;
  document.querySelector("#star3").style.color = document.body.style.color;
  document.querySelector("#star4").style.color = document.body.style.color;
  document.querySelector("#star5").style.color = document.body.style.color;
}
