---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by Whimsical.
--- DateTime: 2021-08-30 5:05 p.m.
---

command_name = "include"

---@type ArkhamImport_Command_RunDirectives
runOn = {
    instructions = true,
    handlers = true
}

local back_image_default = "https://images-ext-2.discordapp.net/external/QY_dmo_UnAHEi1pgWwaRr1-HSB8AtrAv0W74Mh_Z6vg/https/i.imgur.com/EcbhVuh.jpg"

---@param parameters ArkhamImport_Command_DescriptionInstructionArguments
---@return ArkhamImport_Command_DescriptionInstructionResults
function do_instruction(parameters)
    local args = parameters.arguments
    local arg_count = #args

    if (arg_count<4 or arg_count>6) then
        return {
            is_successful = false,
            error_message = "Include Command requires between 4 and 6 arguments. " .. arg_count .. " were provided."
        }
    end

    if not parameters.command_state["include-command"] then
        parameters.command_state["include-command"] = {
            has_run = false,
            includes = {},
            index = 1
        }
    end

    local index = parameters.command_state["include-command"].index
    parameters.command_state["include-command"].index = index + 1

    parameters.command_state["include-command"].includes[index] = {
        count = args[1],
        name = args[2],
        subtitle = args[3],
        image_uri = args[4],
        zone = args[5] or "default",
        back_image_uri = args[6] or back_image_default
    }

    return { is_successful = true, command_state = parameters.command_state }
end

---@param parameters ArkhamImport_Command_HandlerArguments
---@return ArkhamImport_Command_HandlerResults
function handle_card(parameters)
    local state = parameters.command_state["include-command"]

    if state.has_run then return { is_successful = true } end

    state.has_run = true

    local source = getObjectFromGUID(parameters.source_guid)
    local offset = 0.1

    for _, include in pairs(state.includes) do
        local zone_name = include.zone
        local zone = parameters.configuration.zones[zone_name]

        if not zone then
            return {
                is_successful = false,
                command_state = parameters.command_state,
                error_message = "Include Card [" .. include.name .. "]: Zone \"" .. tostring(zone_name) .. "\" was not found."
            }
        end

        local position = zone.is_absolute and zone.position or source:positionToWorld(zone.position)

        for _=1, include.count do
            local new = spawnObject {
                type = "CardCustom",
                position = position + Vector(0, offset, 0),
                rotaiton = source:getRotation() + Vector(0, 0, zone.is_facedown and 180 or 0),
                ---@param card TTSObject
                callback_function = function (card)
                    card:setName(include.name)
                    card:setDescription(include.subtitle)
                end
            }

            new:setCustomObject {
                type = 0,
                face = include.image_uri,
                back = include.back_image_uri
            }
            offset = offset + 1
        end
    end

    return {
        handled = false, -- This is adding cards without respect to the deck content. So the card we're using to fire this command still needs proper handling
        command_state = parameters.command_state,
        is_successful = true
    }
end