#include "ta_parameter.hpp"
#include "texture.hpp"
#include "icons.hpp"

namespace icons {

  constexpr float texture_width = 1.0 / 63.0;
  constexpr float texture_height = 1.0 / 31.0;

  const icon icons[] = {
    [ff] = icon(18, 19, 9, 8),
    [fff] = icon(14, 19, 13, 8),
    [next] = icon(41, 12, 20, 11),
    [prev] = icon(41, 0, 20, 11),
    [rr] = icon(0, 19, 9, 8),
    [rrr] = icon(0, 19, 13, 8),
    [play] = icon(0, 0, 18, 18),
    [pause] = icon(18, 0, 18, 18),
  };

  constexpr inline vec3 transform_position(const vec2& p, icon_type type,
                                           float x, float y, float z)
  {
    return {
      x + p.x * icons[type].width,
      y + p.y * icons[type].height,
      z
    };
  }

  constexpr inline vec2 transform_texture(const vec2& t, icon_type type)
  {
    return {
      (icons[type].x + t.x * icons[type].width) * texture_width,
      (icons[type].y + t.y * icons[type].height) * texture_height,
    };
  }

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

  void draw(ta_parameter_writer& writer, icon_type type,
            float x, float y, float z,
            uint32_t base_color)
  {
    vec3 ap = transform_position(vtx[0], type, x, y, z);
    vec3 bp = transform_position(vtx[1], type, x, y, z);
    vec3 cp = transform_position(vtx[2], type, x, y, z);
    vec3 dp = transform_position(vtx[3], type, x, y, z);

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

    quad_type_3(writer,
                ap, at,
                bp, bt,
                cp, ct,
                dp, dt,
                base_color);
  }

}
