#include "backend.hpp"
#include "shaders.hpp"
#include "sound.hpp"
#include "textures.hpp"
#include "config.hpp"
#include "palette.hpp"

namespace sfml {
  using namespace nlohmann;

  guecs::SpriteTexture Backend::get_sprite(const string& name) {
    auto sp = textures::get_sprite(name);
    return {sp.sprite, sp.texture, sp.frame_size};
  }

  guecs::SpriteTexture Backend::get_icon(const string& name) {
    auto sp = textures::get_icon(name);
    return {sp.sprite, sp.texture, sp.frame_size};
  }

  Backend::Backend() {
    sound::init();
    shaders::init();
    textures::init();
  }

  void Backend::sound_play(const string& name) {
    sound::play(name);
  }

  void Backend::sound_stop(const string& name) {
    sound::stop(name);
  }

  std::shared_ptr<sf::Shader> Backend::get_shader(const std::string& name) {
    return shaders::get(name);
  }

  bool Backend::shader_updated() {
    if(shaders::updated($shaders_version)) {
      $shaders_version = shaders::version();
      return true;
    } else {
      return false;
    }
  }

  guecs::Theme Backend::theme() {
    palette::init();
    auto config = Config("assets/config.json")["theme"];

    guecs::Theme theme {
      .BLACK=palette::get("gui/theme:black"),
      .DARK_DARK=palette::get("gui/theme:dark_dark"),
      .DARK_MID=palette::get("gui/theme:dark_mid"),
      .DARK_LIGHT=palette::get("gui/theme:dark_light"),
      .MID=palette::get("gui/theme:mid"),
      .LIGHT_DARK=palette::get("gui/theme:light_dark"),
      .LIGHT_MID=palette::get("gui/theme:light_mid"),
      .LIGHT_LIGHT=palette::get("gui/theme:light_light"),
      .WHITE=palette::get("gui/theme:white"),
      .TRANSPARENT = palette::get("color:transparent")
    };

    theme.PADDING = config["padding"];
    theme.BORDER_PX = config["border_px"];
    theme.TEXT_SIZE = config["text_size"];
    theme.LABEL_SIZE = config["label_size"];
    theme.FILL_COLOR = palette::get("gui/theme:fill_color");
    theme.TEXT_COLOR = palette::get("gui/theme:text_color");
    theme.BG_COLOR = palette::get("gui/theme:bg_color");
    theme.BORDER_COLOR = palette::get("gui/theme:border_color");
    theme.BG_COLOR_DARK = palette::get("gui/theme:bg_color_dark");
    theme.FONT_FILE_NAME = Config::path_to(config["font_file_name"]).string();

    return theme;
  }
}
