#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.hpp"
#include "widget/left_aligned.hpp"
#include "widget/top_aligned.hpp"

#include "cursor.hpp"

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

widget::button prev_button(50, 50, "prev", prev_click);
widget::button play_button(50, 50, "play");
widget::button pause_button(50, 50, "pause");
widget::button stop_button(50, 50, "stop");
widget::button next_button(50, 50, "next");

widget::widget * left1_children[] = {
  &prev_button,
  &play_button,
  &pause_button,
  &stop_button,
  &next_button,
};
int left1_length = (sizeof (left1_children)) / (sizeof (left1_children[0]));

widget::button up_button(50, 50, "up");
widget::button down_button(50, 50, "down");

widget::widget * left2_children[] = {
  &up_button,
  &down_button,
};
int left2_length = (sizeof (left2_children)) / (sizeof (left2_children[0]));

widget::left_aligned left_container1(0, 0, 5, left1_children, left1_length);
widget::left_aligned left_container2(0, 0, 5, left2_children, left2_length);

widget::widget * top_children[] = {
  &left_container1,
  &left_container2,
};
int top_length = (sizeof (top_children)) / (sizeof (top_children[0]));

widget::top_aligned top = widget::top_aligned(100, 100, 5, 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(0, 0);
  }

  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();

    const int x = 3;
    const int y = 100;

    { // punch-through
      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(multi.pt,
                              para_control::list_type::punch_through,
                              texture::offset::tandy1k,
                              texture_size,
                              texture_control_word::pixel_format::_4bpp_palette);
      */

      //tracker::notes::transfer_lines(multi.pt, x, y);
    }

    /*
    { // translucent
      global_polygon_untextured(writer,
                                para_control::list_type::translucent,
                                tsp_instruction_word::dst_alpha_instr::one);

      tracker::notes::transfer_middle_line(writer, x, y);

      writer.append<ta_global_parameter::end_of_list>() =
        ta_global_parameter::end_of_list(para_control::para_type::end_of_list);
    }
    */

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

      //tracker::notes::borders(multi.op, x, y);

      //tracker::channel_status::transfer(multi.op, 0, 0);

      //test_container.draw(multi);
      top.draw(multi);
     }
  }
}
