#pragma once

#include <cstdint>
#include <string>
#include <unordered_map>
#include <vector>

struct AVFormatContext;
struct AVCodecContext;
struct AVFrame;
struct AVPacket;
struct SwsContext;

class AnimationCatalog {
public:
    bool Load(const std::string& assets_dir);
    std::string PathFor(const std::string& id) const;
    std::string PosterPathFor(const std::string& id) const;

private:
    std::string assets_dir_;
    std::unordered_map<std::string, std::string> videos_;
    std::unordered_map<std::string, std::string> posters_;
};

// Desktop FFmpeg adapter. The LVGL View accepts raw BGRA frames and has no codec dependency.
class VideoPlayer {
public:
    ~VideoPlayer();
    bool Open(const std::string& path, int width, int height, bool letterbox = false);
    bool NextFrame();
    void Close();
    const std::vector<uint8_t>& pixels() const { return pixels_; }

private:
    bool DecodeOne();
    AVFormatContext* format_ = nullptr;
    AVCodecContext* codec_ = nullptr;
    AVFrame* frame_ = nullptr;
    AVPacket* packet_ = nullptr;
    SwsContext* scaler_ = nullptr;
    int stream_ = -1;
    int width_ = 0;
    int height_ = 0;
    int scaled_width_ = 0;
    int scaled_height_ = 0;
    int skip_ = 3;
    bool letterbox_ = false;
    std::vector<uint8_t> pixels_;
    std::vector<uint8_t> scaled_pixels_;
};
