#pragma once
#include "Component.h"
#include "../core/types/Vec2.h"
#include <string>
#include <vector>
#include <memory>
#include "../core/utils/Texture.h"
#include "../core/utils/TextureAtlas.h"
#include "../core/utils/AssetManager.h"
#include "../core/utils/Logging.h"

class AnimationComponent : public Component
{
public:
    AnimationComponent(Object *owner);

    // Initialize with texture atlas path or UAID
    void SetTextureAtlasPath(const std::string &path,
                             int texelWidth,
                             int texelHeight,
                             float frameDuration,
                             int startFrame = 0,
                             int endFrame = INT_MAX);
    void SetTextureAtlas(uint64_t uaid,
                         int texelWidth,
                         int texelHeight,
                         float frameDuration,
                         int startFrame = 0,
                         int endFrame = INT_MAX);

    // Playback controls
    void Play();
    void Stop();
    void SetLooping(bool loop);
    bool IsLooping() const;
    void SetSpeed(float speed);
    float GetSpeed() const;

    void SetPlaying(bool play);
    bool IsPlaying() const;

    // Frame controls
    void SetFrame(int frame);
    int GetCurrentFrame() const;
    int GetStartFrame() const;
    void SetStartFrame(int start);
    int GetEndFrame() const;
    void SetEndFrame(int end);
    int GetTotalFrames() const;

    // Texture UAID
    uint64_t GetTextureUAID() const;
    void SetTextureUAID(uint64_t uaid);

    // Timing
    float GetFrameDuration() const;
    void SetFrameDuration(float duration);

    // Access atlas
    TextureAtlas* GetAtlas();
    const TextureAtlas* GetAtlas() const;

    // Update per tick
    void Update(float dt);

    // Component overrides
    std::string GetName() const override;
    void Save(YAML::Emitter &out) const override;
    void Load(const YAML::Node &node) override;

    RenderType renderType;

private:
    std::shared_ptr<Texture> texture;
    TextureAtlas atlas;

    int columns = 1;
    int rows = 1;
    int totalFrames = 1;

    int startFrame = 0;
    int endFrame = 0;

    float frameDuration = 0.1f;
    float time = 0.0f;
    int currentFrame = 0;

    bool playing = true;
    bool loop = true;
    float speed = 1.0f;

    uint64_t textureUAID = 0;
};
