#pragma once
#include <SFML/Graphics.hpp>
#include "guecs/lel.hpp"
#include <string>
#include <memory>
#include <functional>
#include <any>
#include "guecs/theme.hpp"

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

  struct Textual {
    std::wstring content;
    unsigned int size = THEME.TEXT_SIZE;
    sf::Color color = THEME.TEXT_COLOR;
    int padding = THEME.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);
    void render(sf::RenderWindow& window, sf::Shader *shader_ptr);
  };

  struct Label : public Textual {
    template<typename... Args>
      Label(Args... args) : Textual(args...)
    {
      centered = true;
      size = THEME.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 = THEME.PADDING;
    std::shared_ptr<sf::Sprite> sprite = nullptr;

    void init(lel::Cell &cell);
    void update(const string& new_name);
    void render(sf::RenderWindow& window, sf::Shader *shader_ptr);
  };

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

    void init(lel::Cell& cell);
    void render(sf::RenderWindow& window, sf::Shader *shader_ptr);
  };

  struct Meter {
    float percent = 1.0f;
    sf::Color color = THEME.BG_COLOR_DARK;
    Rectangle bar;

    void init(lel::Cell& cell);
    void init(lel::Cell& cell, Rectangle& bg);
    void render(lel::Cell& cell, sf::RenderWindow& window, sf::Shader *shader_ptr);
  };

  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 render();
    sf::Shader* get_shader(bool is_shape);

  };

  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=THEME.BG_COLOR;
    shared_ptr<sf::RectangleShape> shape = nullptr;

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

    Background() {}

    void init();
    void render(sf::RenderWindow& window);
  };
}