-- Existance in this list signifies that it is an emote, the value indicates if it is a looping emote

local emoteNames = { wave = false, point = false, dance1 = true, dance2 = true, dance3 = true, laugh = false, cheer = false}

function configureAnimationSet(name, fileList)
    if (animTable[name] ~= nil) then
        for _, connection in pairs(animTable[name].connections) do
            connection:disconnect()
        end
    end
    animTable[name] = {}
    animTable[name].count = 0
    animTable[name].totalWeight = 0
    animTable[name].connections = {}

    -- check for config values
    local config = script:FindFirstChild(name)
    if (config ~= nil) then
        --print("Loading anims " .. name)
        table.insert(animTable[name].connections, config.ChildAdded:connect(function(child) configureAnimationSet(name, fileList) end))
        table.insert(animTable[name].connections, config.ChildRemoved:connect(function(child) configureAnimationSet(name, fileList) end))
        local idx = 1
        for _, childPart in pairs(config:GetChildren()) do
            if (childPart:IsA("Animation")) then
                table.insert(animTable[name].connections, childPart.Changed:connect(function(property) configureAnimationSet(name, fileList) end))
                animTable[name][idx] = {}
                animTable[name][idx].anim = childPart
                local weightObject = childPart:FindFirstChild("Weight")
                if (weightObject == nil) then
                    animTable[name][idx].weight = 1
                else
                    animTable[name][idx].weight = weightObject.Value
                end
                animTable[name].count = animTable[name].count + 1
                animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
                --print(name .. " [" .. idx .. "] " .. animTable[name][idx].anim.AnimationId .. " (" .. animTable[name][idx].weight .. ")")
                idx = idx + 1
            end
        end
    end

    -- fallback to defaults
    if (animTable[name].count <= 0) then
        for idx, anim in pairs(fileList) do
            animTable[name][idx] = {}
            animTable[name][idx].anim = Instance.new("Animation")
            animTable[name][idx].anim.Name = name
            animTable[name][idx].anim.AnimationId = anim.id
            animTable[name][idx].weight = anim.weight
            animTable[name].count = animTable[name].count + 1
            animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
            --print(name .. " [" .. idx .. "] " .. anim.id .. " (" .. anim.weight .. ")")
        end
    end
end
