#include "graphics_primitive.hpp"
#include "tracklist.hpp"
#include "ta_parameter.hpp"
#include "playlist.hpp"

namespace scene::tracker::tracklist {

  const float width = glyph::hori_advance * 20 + 3 * 2;
  const int line_rows_half = 5;
  const int line_rows = line_rows_half * 2 + 1;
  const float height = glyph::vert_advance * line_rows + 3 * 2;

  void draw_borders(ta_parameter_writer& writer, float x, float y)
  {
    transfer_horizontal_border(writer, x, y, width);
    transfer_horizontal_border(writer, x, y + height - 3, width);

    transfer_vertical_border(writer, x, y, height);
    transfer_vertical_border(writer, x + width - 3, y, height);
  }

  void draw_track_names(ta_parameter_writer& writer, float x, float y)
  {
    for (int i = 0; i < line_rows; i++) {
      int track_ix = playlist::state.playlist_ix - line_rows_half + i;

      if (track_ix >= 0 && track_ix < playlist::playlist_length) {
        const char * title = playlist::playlist[track_ix].title;
        transfer_string(writer, title,
                        x, y, 1.0 / 10.0,
                        0xffffff);
      }
      y += glyph::vert_advance;
    }
  }

  void draw_middle_line(ta_parameter_writer& writer, float x, float y)
  {
    int middle_width = width;
    int middle_height = glyph::vert_advance - 1;

    y += 2;

    y += glyph::vert_advance * (line_rows_half + 1);

    quad_type_0(writer,
                {x + 0, y + 0, 1.0 / 20.0},
                {x + 0, y + 1, 1.0 / 20.0},
                {x + middle_width, y + 1, 1.0 / 20.0},
                {x + middle_width, y + 0, 1.0 / 20.0},
                0x555555);

    y += 1;

    quad_type_0(writer,
                {x + 0, y + 0, 1.0 / 20.0},
                {x + 0, y + middle_height, 1.0 / 20.0},
                {x + middle_width, y + middle_height, 1.0 / 20.0},
                {x + middle_width, y + 0, 1.0 / 20.0},
                0x404040);

    y += middle_height;

    quad_type_0(writer,
                {x + 0, y + 0, 1.0 / 20.0},
                {x + 0, y + 1, 1.0 / 20.0},
                {x + middle_width, y + 1, 1.0 / 20.0},
                {x + middle_width, y + 0, 1.0 / 20.0},
                0x202020);
  }

  void draw_heading(ta_parameter_writer& writer, float x, float y)
  {
    transfer_string(writer, "playlist:",
                    x, y, 1.0 / 10.0,
                    0xa7a7a7);

    transfer_string(writer, "(    /    )",
                    x + glyph::hori_advance * 10, y, 1.0 / 11.0,
                    0xa7a7a7);

    transfer_integer(writer, playlist::state.playlist_ix + 1,
                     x + glyph::hori_advance * (10 + 2), y, 1.0 / 10.0,
                     2, ' ',
                     0xffffff);

    transfer_integer(writer, playlist::playlist_length,
                     x + glyph::hori_advance * (10 + 7), y, 1.0 / 10.0,
                     2, ' ',
                     0xffffff);
  }

  void draw(ta_multiwriter& multi, float x, float y)
  {
    transfer_global_polygon_glyph(multi.pt);

    draw_heading(multi.pt, x, y);

    draw_middle_line(multi.op, x, y + 3);

    global_polygon_untextured(multi.op,
                              para_control::list_type::opaque,
                              tsp_instruction_word::dst_alpha_instr::zero);

    float yi = y + glyph::vert_advance + 3;
    draw_borders(multi.op, x, yi);

    draw_track_names(multi.pt, x + 3, yi + 3);
  }
}
