/* ********************************************************************** */
/* OLD SCHOOL CURRENT PLAYING STUFF */

var LFM_API = "https://ws.audioscrobbler.com/2.0/";
var LFM_KEY = "f6b9c4007f2969e672a18e11aa883392"; // Get one at https://secure.last.fm/login?next=/api/account/create
var LFM_USER = "pogmommy";

function getNowPlaying() {
  var recentTracksUrl =
    LFM_API+"?method=user.getrecenttracks&user="+LFM_USER+"&api_key="+LFM_KEY+"+&format=json&limit=1";

  if (window.XMLHttpRequest) {
      httpRequest = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
      httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  }
  httpRequest.onreadystatechange = function() {
    if (httpRequest.readyState === XMLHttpRequest.DONE) {
      if (httpRequest.status === 200) {
        // All set
        var response = JSON.parse(httpRequest.responseText);
        console.log(response);
        var currentTrack = response.recenttracks.track[0];

        // Check if it's the same, if not then rerender
        if (!window.nowPlaying || window.nowPlaying.mbid != currentTrack.mbid) {
          window.nowPlaying = currentTrack;
          renderNowPlaying(currentTrack);
        }
        setTimeout(getNowPlaying, 60*1000);
      } else {
        console.log('There was a problem with the last.fm request.');
      }
    }
  };
  httpRequest.open('GET', recentTracksUrl, true);
  httpRequest.send();
}


var nowPlayingNode = null;
//var nowPlayingBorder = null;

function renderNowPlaying(track) {
  console.log(track);
//  if (nowPlayingBorder) {
//    nowPlayingBorder.remove();
//  }
  if (nowPlayingNode) {
    nowPlayingNode.remove();
  }
  //nowPlayingBorder = document.createElement("hr");
  nowPlayingNode = document.createElement("a");
  nowPlayingNode.setAttribute("class", "flexrow now-playing");

  var imageurl = track.image.slice(-1)[0]["#text"];
  var nowPlayingImage = document.createElement("img");
  nowPlayingImage.setAttribute("src", imageurl);
  nowPlayingNode.appendChild(nowPlayingImage);

  // Add more stuff to the display

  var currently = track["@attr"] && track["@attr"].nowplaying == "true";

  var metadata = document.createElement("div");
  metadata.setAttribute("class", "np-metadata");
  metadata.innerHTML =
    "<span class=\"sidebar-caption np-heading\">" + (currently ? "<span class=\"breather\">◉</span>  Now Playing" : "Latest Track") + "</span>" +
    "<span class=\"sidebar-caption np-title\"><strong>"+track.name+"</strong></span>" +
    "<span class=\"sidebar-caption np-artist\">"+track.artist["#text"]+"</span>";
  nowPlayingNode.appendChild(metadata);

  nowPlayingNode.setAttribute("href", track.url);

  //document.body.appendChild(nowPlayingNode);
//  document.getElementById("nowplaying-placeholder").appendChild(nowPlayingBorder)
  document.getElementById("nowplaying-placeholder").appendChild(nowPlayingNode);

  setTimeout(function() {
    nowPlayingNode.setAttribute("class", "flexrow now-playing loaded");
  }, 100);
}
