#include "app.hh"
#include "input.hh"

#include <array>
#include <cstring>

#include <imgui.h>


namespace pg {
namespace {

class Gradient final : public App, public Input::Data {
 public:
  static inline TypeInfo kType = TypeInfo::Create<Gradient>("Input_Gradient");

  Gradient() noexcept : Data("gradient") {
  }

  void Update() noexcept override {
    const auto id = std::to_string(index())+" Input_Gradient | "+
        std::to_string(reinterpret_cast<uintptr_t>(this));

    if (ImGui::Begin(id.c_str())) {
      ImGui::DragInt("w", &w_, 1, 1, 1024);
      ImGui::DragInt("h", &h_, 1, 1, 1024);
      ImGui::DragInt("dur", &dur_, 1, 1, 1024);
    }
    ImGui::End();
  }

  Frame Fetch(size_t n) noexcept override {
    const auto w = static_cast<size_t>(w_);
    const auto h = static_cast<size_t>(h_);
    buf_.resize(w*h*4);

    const auto f = static_cast<float>(n)/static_cast<float>(dur_);
    std::memset(buf_.data(), static_cast<uint8_t>(f*UINT8_MAX), buf_.size());
    return Frame {.w = w, .h = h, .rgba = buf_.data()};
  }
  size_t frames() noexcept override {
    return static_cast<size_t>(dur_);
  }

 private:
  int w_ = 100, h_ = 100;
  int dur_ = 100;
  std::vector<uint8_t> buf_;
};

}
}  // namespace pg