---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by Whimsical.
--- DateTime: 2021-08-25 2:32 p.m.
---

command_name = "proxy-investigator"

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

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

    if (#args~=6 and #args~=7) then
        return {
            is_successful = false,
            error_message = "Proxy Investigator command requires either 7 or 8 arguments. " .. #args .. " were provided."
        }
    end

    parameters.command_state["proxy-investigator"] = {
        name = args[1],
        subtitle = args[2],
        front_uri = args[3],
        back_uri = args[4],
        mini_front_uri = args[5],
        mini_back_uri = args[6],
        zone = args[7] or "investigator"
    }

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

---@param source TTSObject
---@param name string
---@param subtitle string
---@param offset number
---@param zone ArkhamImportZone
---@param front string
---@param back string
---@param use_minicard_scaling boolean
local function create_card(source, name, subtitle, offset, zone, front, back, use_minicard_scaling)
    local position = zone.is_absolute and zone.position or source:positionToWorld(zone.position)

    local card = spawnObject {
        type = "CardCustom",
        position = position + Vector(0, offset, 0),
        rotation = source:getRotation() + Vector(0, 0, zone.is_facedown and 180 or 0),
        scale = use_minicard_scaling and Vector(0.6, 1, 0.6) or Vector(1,1,1),
        callback_function = function (card) card:setName(name) card:setDescription(subtitle) end
    }

    card:setCustomObject {
        type = 0,
        face = front,
        back = back
    }
end

---@param parameters ArkhamImport_Command_HandlerArguments
---@return ArkhamImport_Command_HandlerResults
function handle_card(parameters)
    if parameters.card.type_code ~= "investigator" then return {is_successful = true } end

    local card_data = parameters.command_state["proxy-investigator"] or {}

    if not card_data then return { is_successful = true } end

    local zone = parameters.configuration.zones[card_data.zone]

    if not zone then
        return {
            is_successful = false,
            command_state = parameters.command_state,
            error_message = "Proxy Investigator [" .. tostring(parameters.card.code) .. "]: Zone \"" .. tostring(card_data.zone) .. "\" was not found."
        }
    end

    local source = getObjectFromGUID(parameters.source_guid)

    for _=1, parameters.card.count do
        create_card(source, card_data.name, card_data.subtitle, 10, zone, card_data.front_uri, card_data.back_uri, false)
        create_card(source, card_data.name, card_data.subtitle, 20, zone, card_data.mini_front_uri, card_data.mini_back_uri, true)
    end

    return { handled = true, is_successful = true}
end