// zig fmt: off
const Store = @import("../store.zig");
const std = @import("std");
const json_utils = @import("../json_utils.zig");
const SimpleItem = @import("./simple_item.zig");
const EnumStringUtils = @import("../enum_string_utils.zig").EnumStringUtils;

const Craft = @This();

pub const Items = SimpleItem.BoundedArray(8);

pub const Skill = enum {
    weaponcrafting,
    gearcrafting,
    jewelrycrafting,
    cooking,
    woodcutting,
    mining,
    alchemy,

    const Utils = EnumStringUtils(Skill, .{
        .{ "weaponcrafting" , Skill.weaponcrafting  },
        .{ "gearcrafting"   , Skill.gearcrafting    },
        .{ "jewelrycrafting", Skill.jewelrycrafting },
        .{ "cooking"        , Skill.cooking         },
        .{ "woodcutting"    , Skill.woodcutting     },
        .{ "mining"         , Skill.mining          },
        .{ "alchemy"        , Skill.alchemy         },
    });

    pub const toString = Utils.toString;
    pub const fromString = Utils.fromString;
};

skill: Skill,
level: u64,
quantity: u64,
items: Items,

pub fn parse(store: *Store, obj: std.json.ObjectMap) !Craft {
    const skill = json_utils.getString(obj, "skill") orelse return error.MissingProperty;
    const level = json_utils.getInteger(obj, "level") orelse return error.MissingProperty;
    if (level < 1) return error.InvalidLevel;

    const quantity = json_utils.getInteger(obj, "quantity") orelse return error.MissingProperty;
    if (quantity < 1) return error.InvalidQuantity;

    const items = json_utils.getArray(obj, "items") orelse return error.MissingProperty;

    return Craft{
        .skill = Skill.fromString(skill) orelse return error.InvalidSkill,
        .level = @intCast(level),
        .quantity = @intCast(quantity),
        .items = try Items.parse(store, items)
    };
}
