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

namespace scene::tracker::cover {

  struct cover {
    uint32_t texture_offset;
    uint32_t texture_size;
    float texture_width;
    float texture_height;
    float width;
    float height;
    float scale;
  };

  int cover_ix = -1;

  float zoom_interp = 0.0;

  static const cover covers[] = {
    [thebeach] = {
      .texture_offset = texture::offset::thebeach,
      .texture_size = tsp_instruction_word::texture_u_size::from_int(128)
                    | tsp_instruction_word::texture_v_size::from_int(128),
      .texture_width = 1.0f / 128.0f,
      .texture_height = 1.0f / 128.0f,
      .width = 72,
      .height = 72,
      .scale = 6,
    },
    [silvertrees] = {
      .texture_offset = texture::offset::silvertrees,
      .texture_size = tsp_instruction_word::texture_u_size::from_int(128)
                    | tsp_instruction_word::texture_v_size::from_int(128),
      .texture_width = 1.0f / 128.0f,
      .texture_height = 1.0f / 128.0f,
      .width = 72,
      .height = 72,
      .scale = 6,
    },
    [redtree] = {
      .texture_offset = texture::offset::redtree,
      .texture_size = tsp_instruction_word::texture_u_size::from_int(128)
                    | tsp_instruction_word::texture_v_size::from_int(128),
      .texture_width = 1.0f / 128.0f,
      .texture_height = 1.0f / 128.0f,
      .width = 72,
      .height = 72,
      .scale = 6,
    },
    [mountain] = {
      .texture_offset = texture::offset::mountain,
      .texture_size = tsp_instruction_word::texture_u_size::from_int(128)
                    | tsp_instruction_word::texture_v_size::from_int(128),
      .texture_width = 1.0f / 128.0f,
      .texture_height = 1.0f / 128.0f,
      .width = 72,
      .height = 72,
      .scale = 6,
    },
    [mossycottage] = {
      .texture_offset = texture::offset::mossycottage,
      .texture_size = tsp_instruction_word::texture_u_size::from_int(128)
                    | tsp_instruction_word::texture_v_size::from_int(128),
      .texture_width = 1.0f / 128.0f,
      .texture_height = 1.0f / 128.0f,
      .width = 72,
      .height = 72,
      .scale = 6,
    },
    [clocks] = {
      .texture_offset = texture::offset::clocks,
      .texture_size = tsp_instruction_word::texture_u_size::from_int(128)
                    | tsp_instruction_word::texture_v_size::from_int(128),
      .texture_width = 1.0f / 128.0f,
      .texture_height = 1.0f / 128.0f,
      .width = 72,
      .height = 72,
      .scale = 6,
    },
  };

  constexpr inline vec3 transform_position_fs(const cover& cover,
                                              const vec2& p,
                                              float z)
  {
    float width = cover.width * cover.scale;
    float height = cover.height * cover.scale;
    float x = (framebuffer.px_width - width) / 2;
    float y = (framebuffer.px_height - height) / 2;

    return {
      x + p.x * width,
      y + p.y * height,
      z};
  }

  constexpr inline vec3 transform_position(const cover& cover,
                                           const vec2& p,
                                           float x, float y, float z)
  {
    float width = 128;
    float height = 128;
    float x_offset = (128.0f - width) * 0.5f;
    float y_offset = (128.0f - height) * 0.5f;
    if (x_offset < 0) x_offset = 0;
    if (y_offset < 0) y_offset = 0;

    return {
      x + x_offset + p.x * width,
      y + y_offset + p.y * height,
      z
    };
  }

  constexpr inline vec2 transform_texture(const cover& cover,
                                          const vec2& t)
  {
    return {
      (t.x * cover.width) * cover.texture_width,
      (t.y * cover.height) * cover.texture_height,
    };
  }

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

  static inline vec3 lerp(const vec3& a, const vec3& b, float f)
  {
    float dx = b.x - a.x;
    float dy = b.y - a.y;
    float dz = b.z - a.z;

    return {
      a.x + dx * f,
      a.y + dy * f,
      a.z + dz * f,
    };
  }

  const static float alpha = 207;

  void draw_shroud(ta_parameter_writer& writer)
  {
    global_polygon_translucent(writer);

    uint32_t a = ((uint32_t)(zoom_interp * alpha)) << 24;
    float z = 1.0 / 3.5f;

    quad_type_0(writer,
                {0, 0, z},
                {(float)framebuffer.px_width, 0, z},
                {(float)framebuffer.px_width, (float)framebuffer.px_height, z},
                {0, (float)framebuffer.px_height, z},
                a | 0x202020);
  }

  void draw(ta_multiwriter& multi, float x, float y, bool zoom)
  {
    if (cover_ix < 0)
      return;

    const cover& cover = covers[cover_ix];

    uint32_t texture_size = cover.texture_size
                          | tsp_instruction_word::dst_alpha_instr::zero;

    global_polygon_textured(multi.op,
                            para_control::list_type::translucent,
                            cover.texture_offset,
                            texture_size,
                            texture_control_word::pixel_format::_565);

    float z = 1.0 / 3.0f;

    int base_color = 0xffffff;

    if (zoom) {
      zoom_interp += 0.05;
      if (zoom_interp > 1)
        zoom_interp = 1;
    } else {
      zoom_interp -= 0.05;
      if (zoom_interp < 0)
        zoom_interp = 0;
    }

    vec3 ap0 = transform_position(cover, vtx[0], x, y, z);
    vec3 bp0 = transform_position(cover, vtx[1], x, y, z);
    vec3 cp0 = transform_position(cover, vtx[2], x, y, z);
    vec3 dp0 = transform_position(cover, vtx[3], x, y, z);

    vec3 ap1 = transform_position_fs(cover, vtx[0], z);
    vec3 bp1 = transform_position_fs(cover, vtx[1], z);
    vec3 cp1 = transform_position_fs(cover, vtx[2], z);
    vec3 dp1 = transform_position_fs(cover, vtx[3], z);

    vec3 ap = lerp(ap0, ap1, zoom_interp);
    vec3 bp = lerp(bp0, bp1, zoom_interp);
    vec3 cp = lerp(cp0, cp1, zoom_interp);
    vec3 dp = lerp(dp0, dp1, zoom_interp);

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

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

    if (zoom_interp > 0)
      draw_shroud(multi.op);
  }
}
