#include "components.hpp"
#include "point.hpp"
#include "easings.hpp"

namespace components {
  void configure_entity(const ComponentMap& component_map, DinkyECS::World& world, DinkyECS::Entity ent, json& data) {
    for (auto &i : data) {
      dbc::check(i.contains("_type") && i["_type"].is_string(), fmt::format("component has no _type: {}", data.dump()));
      dbc::check(component_map.contains(i["_type"]), fmt::format("component_map doesn't have type {}", std::string(i["_type"])));
      component_map.at(i["_type"])(world, ent, i);
    }
  }

  void configure(ComponentMap& component_map) {
    components::enroll<BossFight>(component_map);
    components::enroll<Combat>(component_map);
    components::enroll<Loot>(component_map);
    components::enroll<Position>(component_map);
    components::enroll<Weapon>(component_map);
    components::enroll<Curative>(component_map);
    components::enroll<EnemyConfig>(component_map);
    components::enroll<Tile>(component_map);
    components::enroll<Motion>(component_map);
    components::enroll<LightSource>(component_map);
    components::enroll<Device>(component_map);
    components::enroll<Sprite>(component_map);
    components::enroll<Animation>(component_map);
    components::enroll<Sound>(component_map);
  }

  void Animation::play() {
    if(!playing) {
      current = 0;
      playing = true;
    }
  }

  float Animation::twitching() {
    switch(easing) {
      case ease::NONE:
        return 0.0;
      case ease::SINE:
        return ease::sine(float(frames) / subframe * ease_rate);
      case ease::OUT_CIRC:
        return ease::sine(ease::out_circ(float(frames) / subframe * ease_rate));
      case ease::OUT_BOUNCE:
        return ease::sine(ease::out_bounce(float(frames) / subframe * ease_rate));
      case ease::IN_OUT_BACK:
        return ease::sine(ease::in_out_back(float(frames) / subframe * ease_rate));
      default:
        dbc::sentinel(
            fmt::format("Invalid easing {} given to animation",
              int(easing)));
    }
  }

  void Animation::step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out) {
    if(playing && current < frames) {
      float tick = twitching();
      scale_out.x = std::lerp(scale_out.x, scale_out.x + scale, tick);
      scale_out.y = std::lerp(scale_out.y, scale_out.y + scale, tick);

      if(stationary) {
        pos_out.y = pos_out.y - (pos_out.y * scale_out.y - pos_out.y);
      }

      if(!simple) {
        rect_out.position.x += current * texture_width;
      }

      subframe += speed;
      current = int(subframe);
    } else {
      playing = false;
      current = 0;
      subframe = 0.0f;
    }
  }
}
