// zig fmt: off
const std = @import("std");
const json_utils = @import("../json_utils.zig");
const Store = @import("../store.zig");
const Character = @import("./character.zig");
const Cooldown = @import("./cooldown.zig");
const Item = @import("./item.zig");

const EquipResult = @This();

pub const Slot = Character.Equipment.SlotId;

cooldown: Cooldown,
slot: Slot,
item: Item,
character: Character,

fn parse(store: *Store, obj: std.json.ObjectMap) !EquipResult {
    const cooldown = json_utils.getObject(obj, "cooldown") orelse return error.MissingProperty;
    const item = json_utils.getObject(obj, "item") orelse return error.MissingProperty;
    const character = json_utils.getObject(obj, "character") orelse return error.MissingProperty;
    const slot = try json_utils.getStringRequired(obj, "slot");

    return EquipResult{
        .character = try Character.parse(store, character),
        .cooldown = try Cooldown.parse(store, cooldown),
        .item = try Item.parse(store, item),
        .slot = Slot.fromString(slot) orelse return error.InvalidSlot
    };
}

pub fn parseAndUpdate(store: *Store, obj: std.json.ObjectMap) !EquipResult {
    const result = try parse(store, obj);
    _ = try store.characters.appendOrUpdate(result.character);
    _ = try store.items.appendOrUpdate(result.item);
    return result;
}
