#pragma once

#include "engine/prefabs/includes.h"
#include <algorithm>

class Background : public GameObject
{
public:
    AnimationController* animation = nullptr;

    void init() override
    {
        animation = add_component<AnimationController>();
        animation->add_animation("skelly", std::vector<std::string>{
            "assets/skelly/skel_1.png",
            "assets/skelly/skel_2.png",
            "assets/skelly/skel_3.png",
            "assets/skelly/skel_4.png",
            "assets/skelly/skel_5.png",
            "assets/skelly/skel_6.png",
            "assets/skelly/skel_7.png",
            "assets/skelly/skel_8.png",
            "assets/skelly/skel_9.png",
            "assets/skelly/skel_10.png",
            "assets/skelly/skel_11.png",
            "assets/skelly/skel_12.png",
            "assets/skelly/skel_13.png",
            "assets/skelly/skel_14.png",
            "assets/skelly/skel_15.png",
            "assets/skelly/skel_16.png",
            "assets/skelly/skel_17.png",
            "assets/skelly/skel_18.png",
            "assets/skelly/skel_19.png",
            "assets/skelly/skel_20.png",
            "assets/skelly/skel_21.png",
            "assets/skelly/skel_22.png"
        }, 10.0f);
    }

    void draw() override
    {
        if (!animation || !animation->current_animation || animation->current_animation->frames.empty())
        {
            return;
        }
        const Texture2D& tex =
            animation->current_animation->frames[animation->current_animation->current_frame];
        float tw = static_cast<float>(tex.width);
        float th = static_cast<float>(tex.height);
        float sw = static_cast<float>(GetScreenWidth());
        float sh = static_cast<float>(GetScreenHeight());
        float scale = std::max(sw / tw, sh / th);
        animation->position = {sw / 2.0f, sh / 2.0f};
        animation->origin = {tw / 2.0f, th / 2.0f};
        animation->scale = scale;
    }
};

