#pragma once

#include "core/script.hpp"
#include "geometry/paddle.hpp"

#include "constants.hpp"
#include "bounce_script.hpp"


struct wall_script : frog::script
{
    int element;
    int level;
    bounce_script* bounce = nullptr;

    wall_script(int element, int level)
        : element(element), level(level) {}

    void init(frog::game_object& object, frog::engine&) override
    {
        using namespace frog;

        bounce = object.get_script<bounce_script>();
    }

    void destroyed(frog::game_object&, frog::engine& engine)
    {
        auto broadcast = [&](auto& wall)
        {
            auto* script = wall.get_script<wall_script>();
            if (script)
            {
                script->wall_destroyed(wall, element, level);
            }
        };

        engine.scenes->current()
            .for_each_object(frog::by_tag("wall"), broadcast);
    }

    void wall_destroyed(frog::game_object& obj, int el, int lvl)
    {
        // check if the destroyed wall was below us
        if (el == element && lvl < level)
        {
            bounce->move_down(obj, Wall::Height);
            level--;
        }
    }
};
