#include "../../ta_parameter.hpp"
#include "../../graphics_primitive.hpp"
#include "../../interpreter.hpp"
#include "../../framebuffer.hpp"

#include "channel_status.hpp"
#include "texture.hpp"

static inline int round_up_div(int n, int m)
{
  int div = n / m;
  int rem = n % m;
  if (rem == 0)
    return div;
  return div + 1;
}

static inline int round_up(int n, int m)
{
  int rem = n % m;
  if (rem == 0)
    return n;
  return n + m - rem;
}

namespace scene::tracker::channel_status {

void transfer(ta_parameter_writer& writer, int x, int y)
{
  using namespace interpreter;

  const int channel_status_height = 50;

  int max_channels_per_row = framebuffer.px_width / 40;
  int rows = round_up_div(state.xm.number_of_channels, max_channels_per_row);
  assert(rows > 0);
  int number_of_channels = round_up(state.xm.number_of_channels, rows);
  int channels_per_row = number_of_channels / rows;
  int width_per_col = (framebuffer.px_width - 3) / channels_per_row;
  int offset = (framebuffer.px_width - (width_per_col * channels_per_row)) / 2;

  int inner_width = width_per_col - 3;

  int height_per_row = channel_status_height / rows;
  int vert_center = height_per_row / 2 - glyph::vert_advance / 2;

  int ch = 0;
  for (int row = 0; row < rows; row++) {
    int xi = x + offset - 2;

    global_polygon_untextured(writer,
                              para_control::list_type::opaque,
                              tsp_instruction_word::dst_alpha_instr::zero);

    transfer_horizontal_border(writer, xi, y, width_per_col * channels_per_row);

    for (int col = 0; col < channels_per_row; col++) {

      global_polygon_untextured(writer,
                                para_control::list_type::opaque,
                                tsp_instruction_word::dst_alpha_instr::zero);

      transfer_vertical_border(writer, xi, y, height_per_row);

      int keyon = 128 * (state.channel[ch].keyon - 224) / 16;
      if (keyon < 0) keyon = 0;
      uint32_t base_color = (keyon << 16) | (keyon << 8) | (keyon << 0);
      transfer_rectangle(writer,
                         xi, y, 1.0 / 10000.0,
                         width_per_col, height_per_row,
                         base_color);

      if (ch < state.xm.number_of_channels) {
        uint32_t texture_size = tsp_instruction_word::texture_u_size::from_int(128)
                              | tsp_instruction_word::texture_v_size::from_int(256);

        global_polygon_textured(writer,
                                para_control::list_type::opaque,
                                texture::offset::tandy1k,
                                texture_size,
                                texture_control_word::pixel_format::_4bpp_palette);

        int hori_center = inner_width / 2 - (glyph::hori_advance * (ch >= 10)) / 2;
        transfer_integer(writer, ch,
                         xi + hori_center, y + vert_center, 0.0001f,
                         0, 0,
                         0xa7a7a7);
      }

      xi += width_per_col;
      ch += 1;
    }

    global_polygon_untextured(writer,
                              para_control::list_type::opaque,
                              tsp_instruction_word::dst_alpha_instr::zero);

    transfer_vertical_border(writer, xi, y, height_per_row);

    y += height_per_row;
  }

  int xi = x + offset - 2;
  transfer_horizontal_border(writer, xi, y, width_per_col * channels_per_row);

  //borders(writer, x, y);
}

}
