#pragma once
#include <SFML/Graphics.hpp>

#include "dbc.hpp"
#include "sfml/color.hpp"
#include "lel.hpp"
#include <string>
#include <memory>
#include <functional>
#include <any>

namespace guecs {
  using std::shared_ptr, std::wstring, std::string;

  constexpr const int PADDING = 3;
  constexpr const int BORDER_PX = 1;
  constexpr const int TEXT_SIZE = 30;
  constexpr const int LABEL_SIZE = 20;
  constexpr const sf::Color FILL_COLOR = ColorValue::DARK_MID;
  constexpr const sf::Color TEXT_COLOR = ColorValue::LIGHT_LIGHT;
  constexpr const sf::Color BG_COLOR = ColorValue::MID;
  constexpr const sf::Color BORDER_COLOR = ColorValue::MID;
  constexpr const char *FONT_FILE_NAME="assets/text.otf";

  struct Textual {
    std::wstring content;
    unsigned int size = TEXT_SIZE;
    sf::Color color = TEXT_COLOR;
    int padding = PADDING;
    bool centered = false;
    shared_ptr<sf::Font> font = nullptr;
    shared_ptr<sf::Text> text = nullptr;

    void init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr);
    void update(const std::wstring& new_content);
  };

  struct Label : public Textual {
    template<typename... Args>
      Label(Args... args) : Textual(args...)
    {
      centered = true;
      size = LABEL_SIZE;
    }

    Label() {
      centered = true;
    };
  };

  struct Sprite {
    // either you set a filename here,
    // or some kind of config,
    // or a callback that does the loading,
    // or a virtual function and you subclass
    // or there's a static config function you call once,
    // that's passed an object with all the necessary gear
    string name;
    int padding = PADDING;
    std::shared_ptr<sf::Sprite> sprite = nullptr;

    void init(lel::Cell &cell);
    void update(const string& new_name);
  };

  struct Rectangle {
    int padding = PADDING;
    sf::Color color = FILL_COLOR;
    sf::Color border_color = BORDER_COLOR;
    int border_px = BORDER_PX;
    shared_ptr<sf::RectangleShape> shape = nullptr;

    void init(lel::Cell& cell);
  };

  struct Meter {
    float percent = 1.0f;
    sf::Color color = ColorValue::BLACK;
    Rectangle bar;

    void init(lel::Cell& cell);
    void render(lel::Cell& cell);
  };

  struct Effect {
    float duration = 0.1f;
    string name{"ui_shader"};
    float $u_time_end = 0.0;
    bool $active = false;
    std::shared_ptr<sf::Clock> $clock = nullptr;
    std::shared_ptr<sf::Shader> $shader = nullptr;
    int $shader_version = 0;

    void init(lel::Cell &cell);
    void run();
    void stop();
    void step();
    shared_ptr<sf::Shader> checkout_ptr();
  };

  struct Sound {
    string on_click{"ui_click"};
    void play(bool hover);
    void stop(bool hover);
  };

  struct Background {
    float x = 0.0f;
    float y = 0.0f;
    float w = 0.0f;
    float h = 0.0f;
    sf::Color color = BG_COLOR;
    shared_ptr<sf::RectangleShape> shape = nullptr;

    Background(lel::Parser& parser, sf::Color bg_color=BG_COLOR) :
      x(parser.grid_x),
      y(parser.grid_y),
      w(parser.grid_w),
      h(parser.grid_h),
      color(bg_color)
    {}

    Background() {}

    void init();
  };
}
