// zig fmt: off
const std = @import("std");
const Api = @import("artifacts-api");

const Artificer = @import("./artificer.zig");
const Context = Artificer.GoalContext;
const Requirements = Artificer.Requirements;

const Goal = @This();

slot: Api.Character.Equipment.SlotId,
item: Api.Store.Id,
quantity: u64 = 1,

pub fn tick(self: *Goal, ctx: *Context) void {
    const character = ctx.character;

    const equipment_slot = character.equipment.slots.get(self.slot);
    if (equipment_slot.item) |equiped_item|{
        if (equiped_item == self.item and !self.slot.canHoldManyItems()) {
            ctx.completed = true;
        } else {
            ctx.queueAction(.{
                .unequip = .{
                    .slot = self.slot,
                    .quantity = self.quantity
                }
            });
        }
        return;
    }

    ctx.queueAction(.{
        .equip = .{
            .slot = self.slot,
            .item = self.item,
            .quantity = self.quantity
        }
    });
}

pub fn requirements(self: Goal, ctx: *Context) Requirements {
    _ = ctx;

    var reqs: Artificer.Requirements = .{};
    reqs.items.addAssumeCapacity(self.item, self.quantity);
    return reqs;
}

pub fn onActionCompleted(self: *Goal, ctx: *Context, result: Artificer.ActionResult) void {
    _ = self;

    if (result == .equip) {
        ctx.completed = true;
    }
}
