// Album player for DGDN

const tracks = [
  { title: "Dan Beat (bonus)", filename: "Dan Beat (bonus).flac" },
  { title: "Neigh V3", filename: "Neigh V3.flac" },
  { title: "On Ice", filename: "On Ice.flac" },
  { title: "Otish", filename: "Otish.flac" },
  { title: "Scan V2", filename: "Scan V2.flac" },
  { title: "balance v2", filename: "balance v2.flac" },
  { title: "eh ee uhn V2", filename: "eh ee uhn V2.flac" },
  { title: "held v3", filename: "held v3.flac" }
];

const audioPlayer = document.getElementById('audio-player');
const tracksList = document.getElementById('tracks-list');
const nowPlaying = document.getElementById('now-playing');

let currentTrackIndex = 0;

// Build tracklist
function initTracklist() {
  tracks.forEach((track, index) => {
    const li = document.createElement('li');
    li.textContent = track.title;
    li.dataset.index = index;
    li.addEventListener('click', () => playTrack(index));
    tracksList.appendChild(li);
  });
}

// Play track
function playTrack(index) {
  currentTrackIndex = index;
  const track = tracks[index];
  
  // Update audio source
  audioPlayer.src = `/src/audio/DGDN/${track.filename}`;
  audioPlayer.play();
  
  // Update UI
  updateNowPlaying(track.title);
  updateActiveTrack();
}

// Update now playing display
function updateNowPlaying(title) {
  nowPlaying.classList.add('playing');
  nowPlaying.innerHTML = `<p>▶ Now playing: ${title}</p>`;
}

// Update active track in list
function updateActiveTrack() {
  document.querySelectorAll('.tracklist li').forEach((li, i) => {
    li.classList.toggle('active', i === currentTrackIndex);
  });
}

// Auto-play next track when current ends
audioPlayer.addEventListener('ended', () => {
  if (currentTrackIndex < tracks.length - 1) {
    playTrack(currentTrackIndex + 1);
  }
});

// Update active track when manually seeking/playing
audioPlayer.addEventListener('play', () => {
  updateActiveTrack();
});

// Initialize
initTracklist();
