#include "obj_data.h"

#include "json_helper.h"
#include "utility.h"

size_t obj_data::g_world_noobjects; // number of objects in the world

obj_data::obj_data(file_index_type *fi)
    : unit_data(UNIT_ST_OBJ, fi)
    , m_value{0}
    , m_cost{0}
    , m_cost_per_day{0}
    , m_flags{0}
    , m_type{ITEM_TRASH}
    , m_equip_pos{0}
    , m_resistance{0}
{
    g_world_noobjects++;
}

obj_data::~obj_data()
{
    g_world_noobjects--;
}

sbit32 obj_data::getValueAtIndex(size_t index) const
{
    return m_value.at(index);
}

sbit32 *obj_data::getValueAtIndexPtr(size_t index)
{
    return &m_value.at(index);
}

size_t obj_data::getValueArraySize() const
{
    return m_value.size();
}

void obj_data::setValueAtIndexTo(size_t index, sbit32 value)
{
    m_value.at(index) = value;
}

ubit32 obj_data::getPriceInGP() const
{
    return m_cost;
}

ubit32 *obj_data::getPriceInGPPtr()
{
    return &m_cost;
}

void obj_data::setPriceInGP(ubit32 value)
{
    m_cost = value;
}

ubit32 obj_data::getPricePerDay() const
{
    return m_cost_per_day;
}

ubit32 *obj_data::getPricePerDayPtr()
{
    return &m_cost_per_day;
}

void obj_data::setPricePerDay(ubit32 value)
{
    m_cost_per_day = value;
}

ubit8 obj_data::getObjectFlags() const
{
    return m_flags;
}

ubit8 *obj_data::getObjectFlagsPtr()
{
    return &m_flags;
}

void obj_data::setObjectFlag(ubit8 value)
{
    m_flags |= value;
}

void obj_data::removeObjectFlag(ubit8 value)
{
    m_flags &= ~value;
}

void obj_data::setAllObjectFlags(ubit32 value)
{
    m_flags = static_cast<ubit8>(value);
}

ubit8 obj_data::getObjectItemType() const
{
    return m_type;
}

ubit8 *obj_data::getObjectItemTypePtr()
{
    return &m_type;
}

void obj_data::setObjectItemType(ubit8 value)
{
    m_type = value;
}

ubit8 obj_data::getEquipmentPosition() const
{
    return m_equip_pos;
}

void obj_data::setEquipmentPosition(ubit8 value)
{
    m_equip_pos = value;
}

ubit8 obj_data::getMagicResistance() const
{
    return m_resistance;
}

ubit8 *obj_data::getMagicResistancePtr()
{
    return &m_resistance;
}

void obj_data::setMagicResistance(ubit8 value)
{
    m_resistance = value;
}

void obj_data::toJSON(rapidjson::PrettyWriter<rapidjson::StringBuffer> &writer) const
{
    writer.StartObject();
    {
        json::write_unit_id_kvp("id", this, writer);

        writer.String("obj_data");
        writer.StartObject();
        {
            writer.String("value");
            writer.StartArray();
            for (auto &v : m_value)
            {
                writer.Int(v);
            }
            writer.EndArray();

            json::write_kvp("cost", m_cost, writer);
            json::write_kvp("cost_per_day", m_cost_per_day, writer);
            std::string bits;
            json::write_kvp("flags", sprintbit(bits, m_flags, g_obj_flags), writer);
            json::write_kvp("type", m_type, writer);
            json::write_kvp("equip_pos", m_equip_pos, writer);
            json::write_kvp("resistance", m_resistance, writer);
        }

        writer.EndObject();

        writer.String("unit_data");
        unit_data::toJSON(writer);
    }
    writer.EndObject();
}
