/*****************************************************************************
 * Copyright (c) 2014-2026 OpenRCT2 developers
 *
 * For a complete list of all authors, please refer to contributors.md
 * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
 *
 * OpenRCT2 is licensed under the GNU General Public License version 3.
 *****************************************************************************/

#pragma once

#ifdef ENABLE_SCRIPTING

    #include <openrct2/scripting/Duktape.hpp>
    #include <openrct2/world/MapSelection.h>

namespace OpenRCT2::Scripting
{
    class ScTileSelection
    {
    private:
        duk_context* _ctx{};

    public:
        ScTileSelection(duk_context* ctx)
            : _ctx(ctx)
        {
        }

        DukValue range_get() const
        {
            if (gMapSelectFlags.has(MapSelectFlag::enable))
            {
                DukObject range(_ctx);

                DukObject leftTop(_ctx);
                leftTop.Set("x", gMapSelectPositionA.x);
                leftTop.Set("y", gMapSelectPositionA.y);
                range.Set("leftTop", leftTop.Take());

                DukObject rightBottom(_ctx);
                rightBottom.Set("x", gMapSelectPositionB.x);
                rightBottom.Set("y", gMapSelectPositionB.y);
                range.Set("rightBottom", rightBottom.Take());
                return range.Take();
            }

            duk_push_null(_ctx);
            return DukValue::take_from_stack(_ctx);
        }

        void range_set(DukValue value)
        {
            if (value.type() == DukValue::Type::OBJECT)
            {
                auto range = GetMapRange(value);
                if (range)
                {
                    gMapSelectPositionA.x = range->GetX1();
                    gMapSelectPositionA.y = range->GetY1();
                    gMapSelectPositionB.x = range->GetX2();
                    gMapSelectPositionB.y = range->GetY2();
                    gMapSelectType = MapSelectType::full;
                    gMapSelectFlags.set(MapSelectFlag::enable);
                }
            }
            else
            {
                gMapSelectFlags.unset(MapSelectFlag::enable);
            }
        }

        DukValue tiles_get() const
        {
            duk_push_array(_ctx);
            if (gMapSelectFlags.has(MapSelectFlag::enableConstruct))
            {
                duk_uarridx_t index = 0;
                for (const auto& tile : MapSelection::getSelectedTiles())
                {
                    duk_push_object(_ctx);
                    duk_push_int(_ctx, tile.x);
                    duk_put_prop_string(_ctx, -2, "x");
                    duk_push_int(_ctx, tile.y);
                    duk_put_prop_string(_ctx, -2, "y");
                    duk_put_prop_index(_ctx, -2, index);
                    index++;
                }
            }
            return DukValue::take_from_stack(_ctx);
        }

        void tiles_set(DukValue value)
        {
            MapSelection::clearSelectedTiles();
            if (value.is_array())
            {
                value.push();
                auto arrayLen = duk_get_length(_ctx, -1);
                for (duk_uarridx_t i = 0; i < arrayLen; i++)
                {
                    if (duk_get_prop_index(_ctx, -1, i))
                    {
                        auto dukElement = DukValue::take_from_stack(_ctx);
                        auto coords = GetCoordsXY(dukElement);
                        if (coords)
                        {
                            MapSelection::addSelectedTile(*coords);
                        }
                    }
                }
                duk_pop(_ctx);
            }

            if (MapSelection::getSelectedTiles().empty())
            {
                gMapSelectFlags.unset(MapSelectFlag::enableConstruct);
                gMapSelectFlags.unset(MapSelectFlag::green);
            }
            else
            {
                gMapSelectFlags.set(MapSelectFlag::enableConstruct);
            }
        }

        static void Register(duk_context* ctx)
        {
            dukglue_register_property(ctx, &ScTileSelection::range_get, &ScTileSelection::range_set, "range");
            dukglue_register_property(ctx, &ScTileSelection::tiles_get, &ScTileSelection::tiles_set, "tiles");
        }

    private:
        static std::optional<CoordsXY> GetCoordsXY(const DukValue& dukCoords)
        {
            if (dukCoords.type() == DukValue::Type::OBJECT)
            {
                auto dukX = dukCoords["x"];
                if (dukX.type() == DukValue::Type::NUMBER)
                {
                    auto dukY = dukCoords["y"];
                    if (dukY.type() == DukValue::Type::NUMBER)
                    {
                        return CoordsXY(dukX.as_int(), dukY.as_int());
                    }
                }
            }
            return std::nullopt;
        }

        static std::optional<MapRange> GetMapRange(const DukValue& dukMapRange)
        {
            if (dukMapRange.type() == DukValue::Type::OBJECT)
            {
                auto leftTop = GetCoordsXY(dukMapRange["leftTop"]);
                if (leftTop.has_value())
                {
                    auto rightBottom = GetCoordsXY(dukMapRange["rightBottom"]);
                    if (rightBottom.has_value())
                    {
                        return MapRange(leftTop->x, leftTop->y, rightBottom->x, rightBottom->y);
                    }
                }
            }
            return std::nullopt;
        }
    };
} // namespace OpenRCT2::Scripting

#endif
