#include "guecs.hpp"

namespace guecs {
  UI::UI() {
    $font = make_shared<sf::Font>(FONT_FILE_NAME);
  }

  void UI::position(int x, int y, int width, int height) {
    $parser.position(x, y, width, height);
  }

  void UI::layout(std::string grid) {
    $grid = grid;
    $parser.parse($grid);

    for(auto& [name, cell] : $parser.cells) {
      auto ent = entity(name);
      $world.set<lel::Cell>(ent, cell);
    }
  }

  DinkyECS::Entity UI::entity(std::string name) {
    if($name_ents.contains(name)) {
      // already exists so just return it
      return $name_ents.at(name);
    } else {
      auto entity = $world.entity();
      // this lets you look up an entity by name
      $name_ents.insert_or_assign(name, entity);
      // this makes it easier to get the name during querying
      $world.set<CellName>(entity, {name});
      return entity;
    }
  }

  void UI::init() {
    if($world.has_the<Background>()) {
      auto& bg = $world.get_the<Background>();
      bg.init();
    }

    $world.query<Background>([](auto, auto& bg) {
        bg.init();
    });

    $world.query<lel::Cell, Rectangle>([](auto, auto& cell, auto& rect) {
      rect.init(cell);
    });

    $world.query<Rectangle, Meter>([](auto, auto& bg, auto &) {
        bg.shape->setFillColor(ColorValue::BLACK);
    });

    $world.query<lel::Cell, Meter>([](auto, auto &cell, auto& meter) {
        meter.init(cell);
    });

    $world.query<lel::Cell, Textual>([this](auto, auto& cell, auto& text) {
      text.init(cell, $font);
    });

    $world.query<lel::Cell, Label>([this](auto, auto& cell, auto& text) {
      text.init(cell, $font);
    });

    $world.query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) {
        sprite.init(cell);
    });
  }

  void UI::render(sf::RenderWindow& window) {
    if($world.has_the<Background>()) {
      auto& bg = $world.get_the<Background>();
      window.draw(*bg.shape);
    }

    $world.query<Rectangle>([&](auto, auto& rect) {
        window.draw(*rect.shape);
    });

    $world.query<lel::Cell, Meter>([&](auto, auto& cell, const auto &meter) {
        float level = std::clamp(meter.percent, 0.0f, 1.0f) * float(cell.w);
        // ZED: this 6 is a border width, make it a thing
        meter.bar.shape->setSize({std::max(level, 0.0f), float(cell.h - 6)});
        window.draw(*meter.bar.shape);
    });

    $world.query<Sprite>([&](auto, auto& sprite) {
        window.draw(*sprite.sprite);
    });

    $world.query<Label>([&](auto, auto& text) {
        window.draw(*text.text);
    });

    $world.query<Textual>([&](auto, auto& text) {
        window.draw(*text.text);
    });
  }

  bool UI::mouse(float x, float y) {
    int action_count = 0;

    $world.query<lel::Cell, Clickable>([&](auto ent, auto& cell, auto &clicked) {
      if((x >= cell.x && x <= cell.x + cell.w) &&
          (y >= cell.y && y <= cell.y + cell.h))
      {
        if(auto action_data = get_if<ActionData>(ent)) {
          clicked.action(ent, action_data->data);
        } else {
          clicked.action(ent, {});
        }

        action_count++;
      }
    });

    return action_count > 0;
  }

  void UI::show_sprite(string region, string sprite_name) {
    auto ent = entity(region);

    if(!has<Sprite>(ent)) {
      Sprite to_show{sprite_name};
      auto& cell = cell_for(ent);
      to_show.init(cell);
      set<guecs::Sprite>(ent, to_show);
    }
  }

  void UI::show_text(string region, string content) {
    auto ent = entity(region);

    if(auto text = get_if<Textual>(ent)) {
      text->text->setString(content);
    } else {
      auto &cell = cell_for(ent);
      Textual to_set{content, 20};
      to_set.init(cell, $font);
      to_set.text->setFillColor(ColorValue::LIGHT_MID);
      set<Textual>(ent, to_set);
    }
  }

  void UI::show_label(string region, string content) {
    auto ent = entity(region);

    if(auto text = get_if<Label>(ent)) {
      text->text->setString(content);
    } else {
      auto &cell = cell_for(ent);
      Label to_set{content, 20};
      to_set.init(cell, $font);
      to_set.text->setFillColor(ColorValue::LIGHT_MID);
      set<Label>(ent, to_set);
    }
  }

  Clickable make_action(DinkyECS::World& target, Events::GUI event) {
    return {[&, event](auto ent, auto data){
      // remember that ent is passed in from the UI::mouse handler
      target.send<Events::GUI>(event, ent, data);
    }};
  }

}
