#include "animation.hpp"

namespace components {
  void Animation::play() {
    if(!playing) {
      current = 0;
      subframe = 0.0f;
      playing = true;
    }
  }

  float Animation::twitching() {
    float tick = ease::sine(float(frames) / subframe * ease_rate);

    switch(easing) {
      case ease::NONE:
        return 0.0;
      case ease::SINE:
        return tick;
      case ease::OUT_CIRC:
        return ease::out_circ(tick);
      case ease::OUT_BOUNCE:
        return ease::sine(ease::out_bounce(tick));
      case ease::IN_OUT_BACK:
        return ease::sine(ease::in_out_back(tick));
      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 * frame_width;
      }

      subframe += speed;
      current = int(subframe);
    } else if(!looped) {
      playing = false;
      current = frames - 1;
      subframe = float(frames - 1);

      if(!simple) {
        rect_out.position.x += current * frame_width;
      }
    } else {
      playing = false;
      current = 0;
      subframe = 0.0f;
    }
  }
}


namespace animation {
  using namespace components;
  using namespace textures;

  static AnimationManager MGR;
  static bool initialized = false;

  bool apply(Animation& anim, SpriteTexture& target) {
    auto size = target.texture->getSize();
    anim.frame_width = int(size.x) / (unsigned int)anim.frames;
    sf::IntRect rect{{0,0}, {anim.frame_width, int(size.y)}};
    sf::Vector2f scale{1.0, 1.0};
    sf::Vector2f pos{0, 0};

    anim.step(scale, pos, rect);

    target.sprite->setTextureRect(rect);
    target.sprite->setPosition(pos);
    target.sprite->setScale(scale);

    return anim.playing;
  }

  void rotate(sf::Sprite& target, float degrees) {
    target.rotate(sf::degrees(degrees));
  }

  void center(sf::Sprite& target, sf::Vector2f pos) {
    auto bounds = target.getLocalBounds();
    target.setPosition({pos.x + bounds.size.x / 2,
        pos.y + bounds.size.y / 2});
    target.setOrigin({bounds.size.x / 2, bounds.size.y / 2});
  }

  void init() {
    if(!initialized) {
      Config config("assets/animations.json");

      for(auto& [name, data] : config.json().items()) {
        auto anim = components::convert<Animation>(data);
        MGR.animations.insert_or_assign(name, anim);
      }

      initialized = true;
    }
  }

  Animation load(std::string name) {
    dbc::check(initialized, "You forgot to initialize animation.");
    return MGR.animations.at(name);
  }
}