#include "interpreter.hpp"
#include "sound.hpp"
#include "playlist.hpp"

#include "xm.h"
#include "xm/milkypack01.xm.h"
#include "xm/CottageFantasy2.xm.h"
#include "xm/CloudsAhead6.xm.h"

#include "scene/tracker/cover.hpp"

namespace playlist {

  struct state state = {
    .playlist_ix = -1
  };

  const playlist_item playlist[] = {
    {
      .artist = "Shiroiii",
      .title = "CottageFantasy2",
      .start = (int)&_binary_xm_CottageFantasy2_xm_start,
      .cover_ix = scene::tracker::cover::mossycottage,
    },
    {
      .artist = "Shiroiii",
      .title = "CloudsAhead6",
      .start = (int)&_binary_xm_CloudsAhead6_xm_start,
      .cover_ix = scene::tracker::cover::mountain,
    },
  };

  const int playlist_length = (sizeof (playlist)) / (sizeof (playlist[0]));

  void next(bool stop_sound)
  {
    state.playlist_ix += 1;
    if (state.playlist_ix >= playlist_length)
      state.playlist_ix = 0;

    printf("next deferred_load playlist_ix %d\n", state.playlist_ix);
    interpreter::stop_sound();

    const playlist_item& item = playlist[state.playlist_ix];
    interpreter::deferred_load(item.start, item.cover_ix);
  }

  void prev(bool stop_sound)
  {
    state.playlist_ix -= 1;
    if (state.playlist_ix < 0)
      state.playlist_ix = playlist_length - 1;

    printf("prev deferred_load playlist_ix %d\n", state.playlist_ix);
    interpreter::stop_sound();
    const playlist_item& item = playlist[state.playlist_ix];
    interpreter::deferred_load(item.start, item.cover_ix);
  }
}
