#include <SDL.h>
#include <map>
#include <string>
#include <vector>
#include "vector.h"
#pragma once

class Sprite
{
    private:
    std::map<std::string, SDL_Rect> frames;
    SDL_Surface* surface;
    SDL_Texture* Texture;
    int_vec2 size;
    
    public:
    SDL_Rect dstrect;
    int_vec2 scale = { 1, 1 };
    double angle = 0;
    Sprite(SDL_Renderer* renderer, SDL_Surface* surface);
    void draw(SDL_Renderer* renderer, std::string frame, bool autoRect = true, bool flip = false);
    void addFrame(std::string name, SDL_Rect rect);
    void move(int_vec2 position);
    void setPos(int_vec2 position);
    int_vec2 getSize(std::string frame);
    void autorect(std::string frame);
    ~Sprite();
};

class Animation
{
    private:
    std::vector<std::string> frames;
    Sprite* sprite;
    int frameDuration = 0;
    int frameCounter = 0;
    std::string currentFrameName;

    public:
    bool end = false;
    int frameDirection = 1;
    int frameIndex = 0;
    bool loop = true;
    bool pingpong = false;
    Animation(std::map<std::string, SDL_Rect> frames, Sprite* sprite, int frameDuration);
    void update();
    void draw(SDL_Renderer* renderer, bool autoRect = true, bool flip = false);
    void reset();
};

class AnimationManager
{
    private:
    std::map<std::string, Animation*> animations;
    std::map<std::string, bool> flip;
    std::map<std::string, bool> autoRect;
    std::string currentAnimation;
    bool playing = false;
    Sprite* sprite;

    public:
    AnimationManager(Sprite* sprite);
    void addAnimation(std::string name, std::map<std::string, SDL_Rect> animation, int frameDuration = 10, bool flip = false, bool autoRect = true, bool loop = true, bool pingpong = false);
    void setAnimation(std::string name);
    void playAnimation(std::string name);
    void update();
    void draw(SDL_Renderer* renderer);
};