function getIdFonograma(url) {
  return url.replace(/\D/g, "").slice(0, -1);
}

function immubPlayer() {
  var hasNext = false;
  var hasPrev = false;

  ap1 = new APlayer({
    element: document.getElementById("player1"),
    narrow: false, // Optional, narrow style
    autoplay: false, // Optional, autoplay song(s), not supported by mobile browsers
    showlrc: 0, // Optional, show lrc, can be 0, 1, 2, see: ###With lrc
    mutex: true, // Optional, pause other players when this player playing
    theme: "#e6d0b2", // Optional, theme color, default: #b7daff
    mode: "circulation", // Optional, play mode, can be `random` `single` `circulation`(loop) `order`(no loop), default: `circulation`
    preload: "metadata", // Optional, the way to load music, can be 'none' 'metadata' 'auto', default: 'auto'
    listmaxheight: "160px", // Optional, max height of play list                                                        // Required, music info, see: ###With playlist
    music: playlist,
  });

  $(".play-pause .play").on("click", function () {
    ap1.play();
  });

  $(".play-pause .pause").on("click", function () {
    ap1.pause();
    onPause();
  });

  $(".speaker").on("click", function () {
    var url = $(this).attr("data-mp3");
    playlist.forEach(function (item, index) {
      if (item.url == url) {
        ap1.setMusic(index);
      }
    });
  });

  $(".next").on("click", next);
  $(".prev").on("click", prev);

  ap1.on("play", onPlay);
  ap1.on("canplay", toggleDirections);

  ap1.on("ended", function () {
    // console.log("ended");
  });

  function onPlay() {
    var currentMusic = ap1.music; 
    var musicDetails = {
        url: currentMusic.url,
        title: currentMusic.title,
        author: currentMusic.author,
        index: ap1.playIndex,
        IIDFONOGRAMA: currentMusic.IIDFONOGRAMA
    };
    // console.log("Tocando a faixa do IIDFONOGRAMA:", musicDetails.IIDFONOGRAMA);
    contabilizador_click_fonograma(musicDetails.IIDFONOGRAMA)

    window.addEventListener("beforeunload", function (e) {
      var confirmationMessage = "VocÃª realmente quer sair?";

      (e || window.event).returnValue = confirmationMessage; //Gecko + IE
      return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
    });

    $(".play-pause .play").hide();
    $(".play-pause .pause").show();
  }

  function onPause() {
    $(".play-pause .pause").hide();
    $(".play-pause .play").show();
  }

  function toggleDirections() {
    toggleNext();
    togglePrev();
  }

  function toggleNext() {
    if (ap1.playIndex < playlist.length - 1) {
      $(" .next").css("visibility", "visible");
    } else {
      $(" .next").css("visibility", "hidden");
    }
  }

  function togglePrev() {
    if (ap1.playIndex > 0) {
      $(" .prev").css("visibility", "visible");
    } else {
      $(" .prev").css("visibility", "hidden");
    }
  }

  function next() {
    ap1.setMusic(ap1.playIndex + 1);
    toggleDirections();
  }

  function prev() {
    ap1.setMusic(ap1.playIndex - 1);
    toggleDirections();
  }
}

$(document).ready(function () {
  if (typeof playlist !== "undefined") {
    if (playlist.length > 0) {
      immubPlayer();
    }
  }
});

