--UPDATE_BIND_TABLE UPDATE_BINDS UPDATE_BAR_TABLE UPDATE_BARS DELETE_SET LIST_SETS
--[[
UPDATE_BIND_TABLE saves current binds
UPDATE_BINDS restores saved binds
UPDATE_BAR_TABLE saves current bar layout also supports sets
UPDATE_BARS restores bar layout; defaults to the "default" set
DELETE_SET deletes a specified set
LIST_SETS lists all sets
DELETE and LIST only work for bar layouts
 
 
 
/run WeakAuras.ScanEvents("UPDATE_BINDS")
/run WeakAuras.ScanEvents("UPDATE_BIND_TABLE")
 
]]
function(e, ...)
    if InCombatLockdown() ~= 1 then
        if e == "UPDATE_BIND_TABLE" then
            if not WeakAurasSaved.CustomTrash then WeakAurasSaved.CustomTrash = {} end
            if not WeakAurasSaved.CustomTrash.BindTableTwo then WeakAurasSaved.CustomTrash.BindTableTwo = {} end
            for i = 1, GetNumBindings() do
                local comm, bind, bind2 = GetBinding(i)
                if comm and bind2 and not WeakAurasSaved.CustomTrash.BindTableTwo[comm] then
                    print("Adding", comm, "with bind", bind2, "to bind table")
                    WeakAurasSaved.CustomTrash.BindTableTwo[comm] = {}
                    WeakAurasSaved.CustomTrash.BindTableTwo[comm].bind = bind2
                    WeakAurasSaved.CustomTrash.BindTableTwo[comm].index = i
                end
                if comm and not bind2 and WeakAurasSaved.CustomTrash.BindTableTwo[comm] then
                    print("Removing", comm, "with no bind from the bind table")
                    WeakAurasSaved.CustomTrash.BindTableTwo[comm] = nil
                end
            end
        elseif e == "UPDATE_BINDS" then
            for k, v in pairs(WeakAurasSaved.CustomTrash.BindTableTwo) do
                local command, bind, bind2 = GetBinding(v.index)
                if bind2 ~= v.bind then
                    --The key should be aura_env.keyBinds[command] but the key is bind2
                    print("Key", command, "has bind", bind2, "but should have", v.bind)
                    if bind2 then SetBinding(bind2) end
                    if v.bind then SetBinding(v.bind, command) end
                end
            end
            SaveBindings(1)
        elseif e == "UPDATE_BAR_TABLE" then
            local arg2 = "default"
            if ... then arg2 = ... end
            if not WeakAurasSaved.CustomTrash then WeakAurasSaved.CustomTrash = {} end
            if not WeakAurasSaved.CustomTrash.BarTable then WeakAurasSaved.CustomTrash.BarTable = {} end
            if not WeakAurasSaved.CustomTrash.BarTable[arg2] then WeakAurasSaved.CustomTrash.BarTable[arg2] = {} end
            for i = 1, 72 do
                PickupAction(i)
                if GetCursorInfo() then
                    local type = GetCursorInfo()
                    local id, name = nil, nil
                    if not type then WeakAurasSaved.CustomTrash.BarTable[arg2][i] = nil end
                    if type == "spell" then
                        id = select(4, GetCursorInfo())
                    elseif type == "mount" then
                        id = select(2, GetCursorInfo())
                    elseif type == "macro" then
                        id = select(2, GetCursorInfo())
                        name = select(1, GetMacroInfo(id))
                    elseif type == "item" then
                        id = select(2, GetCursorInfo())
                    elseif type == "equipmentset" then
                        name = select(2, GetCursorInfo())
                    end --elseif end
                    if not WeakAurasSaved.CustomTrash.BarTable[arg2][i] then WeakAurasSaved.CustomTrash.BarTable[arg2][i] = {} end
                    if id ~= WeakAurasSaved.CustomTrash.BarTable[arg2][i].id or type ~= WeakAurasSaved.CustomTrash.BarTable[arg2][i].type then
                        WeakAurasSaved.CustomTrash.BarTable[arg2][i].type = type
                        WeakAurasSaved.CustomTrash.BarTable[arg2][i].id = id
                        WeakAurasSaved.CustomTrash.BarTable[arg2][i].name = name
                        if not name then
                            print("Adding", type, "by id", id, "on slot", i, "to the table")
                        elseif name then
                            print("Adding", type, "by id", id, "and name", name, "on slot", i, "to the table")
                        end
                    end
                end
                PickupAction(i)
            end
        elseif e == "UPDATE_BARS" then
            local arg2 = "default"
            if ... then arg2 = ... end
            for k, v in pairs(WeakAurasSaved.CustomTrash.BarTable[arg2]) do
                if v.type == "spell" then
                    if GetCursorInfo() then ClearCursor() end
                    PickupAction(k)
                    if GetCursorInfo() then
                        local id = select(4, GetCursorInfo())
                        if id ~= v.id then
                            ClearCursor()
                            PickupSpell(v.id)
                            PickupAction(k)
                            ClearCursor()
                        end
                        PickupAction(k)
                    else
                        ClearCursor()
                        PickupSpell(v.id)
                        PickupAction(k)
                        ClearCursor()
                    end
                elseif v.type == "mount" then
                    local mid, mindex
                    --Looking for mount in mount journal
                    for i = 1, C_MountJournal.GetNumMounts() do
                        mid = select(12, C_MountJournal.GetDisplayedMountInfo(i))
                        local ishave = select(11, C_MountJournal.GetDisplayedMountInfo(i))
                        if mid == v.id then
                            mindex = i
                            break
                        end
                        if ishave == false then
                            break
                        end
                    end
                    
                    --Checking what's on the bar
                    if GetCursorInfo() then ClearCursor() end
                    PickupAction(k)
                    if GetCursorInfo() then
                        local id = select(2, GetCursorInfo())
                        if id ~= v.id and mindex then
                            ClearCursor()
                            C_MountJournal.Pickup(mindex)
                            PickupAction(k)
                            ClearCursor()
                        end
                        PickupAction(k)
                    elseif not GetCursorInfo() and mindex then
                        ClearCursor()
                        C_MountJournal.Pickup(mindex)
                        PickupAction(k)
                        ClearCursor()
                    end
                elseif v.type == "macro" then
                    if GetCursorInfo() then ClearCursor() end
                    PickupAction(k)
                    if GetCursorInfo() then
                        local id = select(2, GetCursorInfo())
                        local name = select(1, GetMacroInfo(id))
                        if id ~= v.id or name ~= v.name then
                            ClearCursor()
                            PickupMacro(v.id)
                            PickupAction(k)
                            ClearCursor()
                        end
                        PickupAction(k)
                    else
                        ClearCursor()
                        local name = GetMacroInfo(v.id)
                        if name == v.name then
                            PickupMacro(v.id)
                            PickupAction(k)
                            ClearCursor()
                        end
                    end
                elseif v.type == "equipmentset" then
                    if GetCursorInfo() then ClearCursor() end
                    local eqindex
                    for i = 1, GetNumEquipmentSets() do
                        local nam = GetEquipmentSetInfo(i)
                        if nam == v.name then
                            eqindex = i
                            break
                        end
                    end
                    if eqindex then
                        PickupAction(k)
                        if GetCursorInfo() then
                            local name = select(2, GetCursorInfo())
                            if name ~= v.name then
                                ClearCursor()
                                PickupEquipmentSet(eqindex)
                                PickupAction(k)
                                ClearCursor()
                            end
                            PickupAction(k)
                        else
                            ClearCursor()
                            PickupEquipmentSet(eqindex)
                            PickupAction(k)
                            ClearCursor()
                        end
                    end
                elseif v.type == "item" then
                    if GetCursorInfo() then ClearCursor() end
                    local ios, jos
                    for i = 0, 4 do
                        for j = 1, GetContainerNumSlots(i) do
                            local link = select(7, GetContainerItemInfo(i, j))
                            if link then
                                local id = link:match("item:(%d+):")
                                if id then
                                    id = tonumber(id)
                                    if id == v.id then
                                        ios = i
                                        jos = j
                                        break
                                    end
                                end
                            end
                        end
                    end
                    if ios and jos then
                        PickupAction(k)
                        if GetCursorInfo() then
                            local id = select(2, GetCursorInfo())
                            if id ~= v.id then
                                ClearCursor()
                                PickupContainerItem(ios, jos)
                                PickupAction(k)
                                ClearCursor()
                            end
                            PickupAction(k)
                        else
                            ClearCursor()
                            PickupContainerItem(ios, jos)
                            PickupAction(k)
                            ClearCursor()
                        end
                    end
                end --elseif end
            end
        elseif e == "DELETE_SET" then
            local arg2 = ...
            if arg2 and WeakAurasSaved.CustomTrash.BarTable[arg2] then
                WeakAurasSaved.CustomTrash.BarTable[arg2] = nil
            end
        elseif e == "LIST_SETS" then
            for k, v in pairs(WeakAurasSaved.CustomTrash.BarTable) do
                print(k)
            end
        end --elseif end
    end
end
