#include "holly/ta_bits.hpp"

#include "ta_parameter.hpp"

#include "scene/tracker/scene.hpp"

#include "notes.hpp"
#include "channel_status.hpp"
#include "texture.hpp"
#include "widget/button_label.hpp"
#include "widget/button_icon.hpp"
#include "widget/left_aligned.hpp"
#include "widget/top_aligned.hpp"
#include "playlist.hpp"
#include "interpreter.hpp"
#include "icons.hpp"

#include "cursor.hpp"

void prev_click()
{
  printf("prev\n");
  playlist::prev();
}

void next_click()
{
  printf("next\n");
  playlist::next();
}

#define __length(c) ((sizeof (c)) / (sizeof (c[0])))

//widget::button_label play_button(75, 60, "play", nullptr);
widget::button_icon play_button(75, 60, icons::play, nullptr);
widget::button_icon pause_button(75, 33, icons::pause, nullptr);

widget::button_icon prev_button(79, 30, icons::prev, prev_click);
widget::button_icon next_button(79, 30, icons::next, next_click);

widget::button_icon rrr_button(39, 21, icons::rrr, nullptr);
widget::button_icon rr_button(39, 21, icons::rr, nullptr);
widget::button_icon ff_button(39, 21, icons::ff, nullptr);
widget::button_icon fff_button(39, 21, icons::fff, nullptr);

widget::widget * play_pause_children[] = {
  &play_button,
  &pause_button,
};
int play_pause_length = __length(play_pause_children);

widget::widget * prev_next_children[] = {
  &prev_button,
  &next_button,
};
int prev_next_length = __length(prev_next_children);

widget::widget * ff_rr_children[] = {
  &rrr_button,
  &rr_button,
  &ff_button,
  &fff_button,
};
int ff_rr_length = __length(ff_rr_children);

widget::top_aligned play_pause(0, 0, 1, play_pause_children, play_pause_length);

widget::left_aligned prev_next(0, 15, 1, prev_next_children, prev_next_length);

widget::left_aligned ff_rr(0, 67, 1, ff_rr_children, ff_rr_length);

widget::widget * prev_next_ff_rr_children[] = {
  &prev_next,
  &ff_rr,
};
int prev_next_ff_rr_length = __length(prev_next_ff_rr_children);

widget::container prev_next_ff_rr(0, 0, 159, 85, prev_next_ff_rr_children, prev_next_ff_rr_length);

widget::widget * top_children[] = {
  &play_pause,
  &prev_next_ff_rr,
};
int top_length = __length(top_children);

widget::left_aligned top(0, 0, 20, top_children, top_length);

namespace scene::tracker {

  const struct scene::scene scene = {
    .ta_alloc = ta_alloc_ctrl::pt_opb::_32x4byte
              | ta_alloc_ctrl::tm_opb::no_list
              | ta_alloc_ctrl::t_opb::_8x4byte
              | ta_alloc_ctrl::om_opb::no_list
              | ta_alloc_ctrl::o_opb::_32x4byte,
    .opb_size = {
      .opaque = 32 * 4,
      .opaque_modifier = 0,
      .translucent = 8 * 4,
      .translucent_modifier = 0,
      .punch_through = 32 * 4
    },
    .transfer = transfer,
    .init = init,
  };

  void init()
  {
    top.freeze(5, 5);
    playlist::next();
  }

  void update()
  {
    for (int i = 0; i < 4; i++) {
      cursor::cursor& c = cursor::state[i];
      if (c.active && c.a) {
        widget::widget * w = top.pick(c.x, c.y);
        if (w != nullptr) {
          w->click();
        }
      }
    }
  }

  void transfer(ta_multiwriter& multi)
  {
    update();

    top.draw(multi);

    float y = top.y() + top.height + 5;

    channel_status::draw(multi, 5, y);

    y += channel_status::height + 5;

    notes::draw(multi, 5, y);
  }
}
