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


local tags = {
    bag = "chaos_bag",
    provider = "token_list_provider",
    tokens = "token_image_provider"
}

---@class ChaosToken
---@field name string
---@field image string
local _ = {}

---@type table<string|number, ChaosToken>
local sources

---@type table<string, any[]>
local tokens

---@param source TTSObject
local LoadSource = function (source)
    tokens = source:getTable("chaos_tokens")
end

---@param token ChaosToken
---@param position Vector
---@param chaos_bag TTSObject
---@return number
local SpawnToken = function (token, position, chaos_bag)
    spawnObject {
        type = "Custom_Tile",
        position = position,
        scale = { 0.81, 1.0, 0.81 },
        rotation = { 0, 270, 0 },
        ---@param object TTSObject
        callback_function = function(object, _, _)
            object:setName(token.name)
            chaos_bag:putObject(object)
        end
    }:setCustomObject {
        image = token.image,
        type = 2,
        thickness = 0.1
    }
end

---@param chaos_bag TTSObject
local emptyBag= function (chaos_bag)
    local object = chaos_bag:getObjects()
    local pos = self:getPosition()
    pos.y = pos.y+1

    for _, object in ipairs(object) do
        chaos_bag:takeObject {
            guid = object.guid,
            position = pos,
            ---@param item TTSObject
            callback_function = function (item) item:destruct() end
        }
    end
end

---@param difficulty string
local clickFun= function (difficulty)
    local chaos_bag = getObjectsWithTag(tags.bag)[1]

    emptyBag(chaos_bag)

    local loading = tokens[difficulty]
    local pos = self:getPosition()

    for _, token_id in ipairs(loading) do
        if type(token_id)=="string" then token_id = token_id:lower() end

        local token = sources[token_id]

        if (token==nil) then
            error("Could not find token \"" .. token_id .. "\".")
            return
        end

        SpawnToken(token, pos, chaos_bag)
    end
end


---@param difficulty string
local MakeClickFun = function(difficulty)
    return function ()
        clickFun(difficulty)
    end
end

---@param label string
---@param z_offset number|nil
local makeButton = function(label , z_offset)
    z_offset = z_offset or -0.15

    _G[label:lower() .. "Click"] = MakeClickFun(label)

    self:createButton({
        label = label,
        function_owner = self,
        click_function = label:lower() .. "Click",
        position = {0, 0.1, z_offset},
        rotation = {0, 0, 0},
        scale = {0.47, 1, 0.47},
        height = 200,
        width = 1150,
        font_size = 100,
        color = {0.87, 0.8, 0.70},
        font_color = {0, 0, 0}
    })
    return z_offset + 0.20
end

---@param source TTSObject
local CreateButtons= function(source)
    self:clearButtons()

    local z_offset
    for difficulty,_ in pairs(tokens) do
        z_offset = makeButton(difficulty, z_offset)
    end
end


function LoadTokens()
    ---@type TTSObject[]
    local image_sources = getObjectsWithTag(tags.tokens)

    if (#image_sources<=0) then
        error("Cannot find images_sources")
        return
    end

    sources = image_sources[1]:getTable("sources")

    if (tokens~=nil) then CreateButtons() return end

    local source = getObjectsWithTag(tags.provider)

    if (#source<=0) then return end

    LoadSource(source[1])

    CreateButtons()
end

function onload()
    Timer.create {
        identifier = self:getGUID(),
        function_name = "LoadTokens"
    }
end

---@param provider TTSObject
function onObjectSpawn(provider)
    if (not provider:hasTag(tags.provider)) then return end

    LoadSource(provider)

    CreateButtons()
end