function formatTime( time ) {
  let minutes = Math.floor( time / 60 )
  let timeForSeconds = time - ( minutes * 60 ) // seconds without counted minutes
  let seconds = Math.floor( timeForSeconds )
  let miliseconds = Math.floor(( timeForSeconds * 1000 ) % 9 )
  let secondsReadable = seconds > 9 ? seconds : `0${seconds}` // To change 2:2 into 2:02
  let minutesReadable = minutes > 9 ? minutes : `00:0${minutes}` // To change 2:02 into 02:02
  return `${minutesReadable}:${secondsReadable}.${miliseconds}`
}

function playCart1() {
    document.getElementById("cartaudio1").play(),
    document.getElementById("playStopCart1").setAttribute("onclick", "stopCart1()"),
    $("#cart1 .headerCart").removeClass("activeCart"),
    $("#cart1 .headerCart").addClass("playCart"),
    document.getElementById("timecart1").style.display = "none",
    document.getElementById("cur1").style.display = "block"
}

function stopCart1() {
    var l = document.getElementById("cartaudio1");
    l.pause(), l.currentTime = 0,
    document.getElementById("playStopCart1").setAttribute("onclick", "playCart1()"),
    $("#cart1 .headerCart").removeClass("playCart"),
    $("#cart1 .headerCart").addClass("activeCart"),
    document.getElementById("timecart1").style.display = "block",
    document.getElementById("cur1").style.display = "none"
}

var music1 = document.getElementById("cartaudio1"),
    progressed1 = document.getElementById("progressed1"),
    progress_bar1 = document.getElementById("progress_bar1");
music1.ontimeupdate = function(e) {
    progressed1.style.width = Math.floor(100 * music1.currentTime / music1.duration) + "%", document.getElementById("cur1").innerHTML = "" + formatTime(this.currentTime)
}, progress_bar1.onclick = function(e) {
    music1.currentTime = e.offsetX / progress_bar1.offsetWidth * music1.duration
};

function endcartaudio1() {
  var l = document.getElementById("cartaudio1");
  l.pause(), l.currentTime = 0,
  document.getElementById("playStopCart1").setAttribute("onclick", "playCart1()"),
  $("#cart1 .headerCart").removeClass("playCart"),
  $("#cart1 .headerCart").addClass("activeCart"),
  document.getElementById("timecart1").style.display = "block",
  document.getElementById("cur1").style.display = "none"
}

$(document).ready(function() {
  getAPIsongID();
  refreshAudioCart1();
});

function getAPIsongID() {
let url = `https://dev.enzomichaud.fr/radiosoft/multidiff-soft/feed.php`;
fetch(url).then((response) =>
  response.json().then((data) => {
    console.log(data);
    document.getElementById("cart1ID").innerHTML = data.ID_5;
  })
);
}
getAPIsongID();
setInterval(getAPIsongID, 4000);

function refreshAudioCart1() {
  const cart1ID = document.getElementById('cart1ID');
  var audioID1 = document.getElementById('cart1').getAttribute('data-cart1-id');
  if (cart1ID.textContent === audioID1) {
  }else{
    let url = `https://dev.enzomichaud.fr/radiosoft/multidiff-soft/feed.php`;
    fetch(url).then((response) =>
      response.json().then((data) => {
        console.log(data);
        $('#cart1').attr('data-cart1-id', data.ID_5);
        $('#titleCart1').attr('data-cd1-title-cart', data.title_5);
        $('#artistCart1').attr('data-cd1-artist-cart', data.artist_5);
        $('#timecart1').attr('data-cd1-time-cart', data.duration_5);
        $('#fullTimeCart1').attr('data-cd1-fulltime-cart', data.origdur_5);
        document.querySelector("#cartaudio1").currentTime=data.cue_5;
        $('#cartaudio1').attr('src', 'http://127.0.0.1:8887/'+data.path_5);
        $("#cart1 .headerCart").addClass("activeCart show");
        $("#cart1 .footerHS").addClass("show");
      })
    );
  }
}
refreshAudioCart1();
setInterval(refreshAudioCart1, 1000);


document.addEventListener('keydown', function(event) {
    var ekey = event.keyCode
    if( 49 === ekey ) {
     $("#playStopCart1").click();
    }
});
