#include "graphics_primitive.hpp"
#include "cover.hpp"
#include "ta_parameter.hpp"
#include "playlist.hpp"
#include "texture.hpp"

namespace scene::tracker::cover {

  constexpr float texture_width = 1.0 / 128.0;
  constexpr float texture_height = 1.0 / 128.0;

  constexpr inline vec3 transform_position(const vec2& p,
                                           float x, float y, float z)
  {
    return {
      x + p.x * 128,
      y + p.y * 128,
      z
    };
  }

  constexpr inline vec2 transform_texture(const vec2& t)
  {
    return {
      (0 + t.x * 72) * texture_width,
      (0 + t.y * 72) * texture_height,
    };
  }

  constexpr vec2 vtx[] = {
    {0, 0},
    {1, 0},
    {1, 1},
    {0, 1},
  };

  void draw(ta_multiwriter& multi, float x, float y)
  {
    uint32_t texture_size = tsp_instruction_word::texture_u_size::from_int(128)
                          | tsp_instruction_word::texture_v_size::from_int(128)
                          | tsp_instruction_word::dst_alpha_instr::zero;

    global_polygon_textured(multi.op,
                            para_control::list_type::opaque,
                            texture::offset::cover1,
                            texture_size,
                            texture_control_word::pixel_format::_565);

    float z = 1.0 / 10.0f;

    int base_color = 0xffffff;

    vec3 ap = transform_position(vtx[0], x, y, z);
    vec3 bp = transform_position(vtx[1], x, y, z);
    vec3 cp = transform_position(vtx[2], x, y, z);
    vec3 dp = transform_position(vtx[3], x, y, z);

    vec2 at = transform_texture(vtx[0]);
    vec2 bt = transform_texture(vtx[1]);
    vec2 ct = transform_texture(vtx[2]);
    vec2 dt = transform_texture(vtx[3]);

    quad_type_3(multi.op,
                ap, at,
                bp, bt,
                cp, ct,
                dp, dt,
                base_color);
  }
}
