/*****************************************************************************
 * Copyright (c) 2014-2021 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.
 *****************************************************************************/

#ifdef ENABLE_SCRIPTING

#    include "ScPlayer.hpp"

#    include "../../../Context.h"
#    include "../../../actions/PlayerSetGroupAction.h"
#    include "../../../network/NetworkAction.h"
#    include "../../../network/network.h"

namespace OpenRCT2::Scripting
{
    ScPlayer::ScPlayer(int32_t id)
        : _id(id)
    {
    }

    int32_t ScPlayer::id_get() const
    {
        return _id;
    }

    std::string ScPlayer::name_get() const
    {
#    ifndef DISABLE_NETWORK
        auto index = network_get_player_index(_id);
        if (index == -1)
            return {};
        return network_get_player_name(index);
#    else
        return {};
#    endif
    }

    int32_t ScPlayer::group_get() const
    {
#    ifndef DISABLE_NETWORK
        auto index = network_get_player_index(_id);
        if (index == -1)
            return {};
        return network_get_player_group(index);
#    else
        return 0;
#    endif
    }
    void ScPlayer::group_set(int32_t value)
    {
#    ifndef DISABLE_NETWORK
        auto playerSetGroupAction = PlayerSetGroupAction(_id, value);
        GameActions::Execute(&playerSetGroupAction);
#    endif
    }

    int32_t ScPlayer::ping_get() const
    {
#    ifndef DISABLE_NETWORK
        auto index = network_get_player_index(_id);
        if (index == -1)
            return {};
        return network_get_player_ping(index);
#    else
        return 0;
#    endif
    }

    int32_t ScPlayer::commandsRan_get() const
    {
#    ifndef DISABLE_NETWORK
        auto index = network_get_player_index(_id);
        if (index == -1)
            return {};
        return network_get_player_commands_ran(index);
#    else
        return 0;
#    endif
    }

    int32_t ScPlayer::moneySpent_get() const
    {
#    ifndef DISABLE_NETWORK
        auto index = network_get_player_index(_id);
        if (index == -1)
            return {};
        return network_get_player_money_spent(index);
#    else
        return 0;
#    endif
    }

    std::string ScPlayer::ipAddress_get() const
    {
        return network_get_player_ip_address(_id);
    }

    std::string ScPlayer::publicKeyHash_get() const
    {
        return network_get_player_public_key_hash(_id);
    }

    void ScPlayer::Register(duk_context* ctx)
    {
        dukglue_register_property(ctx, &ScPlayer::id_get, nullptr, "id");
        dukglue_register_property(ctx, &ScPlayer::name_get, nullptr, "name");
        dukglue_register_property(ctx, &ScPlayer::group_get, &ScPlayer::group_set, "group");
        dukglue_register_property(ctx, &ScPlayer::ping_get, nullptr, "ping");
        dukglue_register_property(ctx, &ScPlayer::commandsRan_get, nullptr, "commandsRan");
        dukglue_register_property(ctx, &ScPlayer::moneySpent_get, nullptr, "moneySpent");
        dukglue_register_property(ctx, &ScPlayer::ipAddress_get, nullptr, "ipAddress");
        dukglue_register_property(ctx, &ScPlayer::publicKeyHash_get, nullptr, "publicKeyHash");
    }

} // namespace OpenRCT2::Scripting

#endif
