--[[
Author: Jarth
Filename: CollectionBars.lua
]] --

-------------------------------------------------------------------------------------------------
-- VARIABLES --
-------------------------------------------------------------------------------------------------
local base = CollectionBars

-------------------------------------------------------------------------------------------------
--  FUNCTIONS --
-------------------------------------------------------------------------------------------------

function base.OnAddOnLoaded(_, addonName)
    if addonName == base.Addon.Name then
        base.Initialize()
    end
end

function base.Initialize()
    -- Load saved values..
    base.SetAndUpdateAccountSettings()
    -- Initialize frames
    base.InitializeCombineFrame()
    -- Initialize bindings
    base.InitializeBindings()
    base.InitializeReverseBinding()

    -- Initialize and setup types
    for _, _type in pairs(base.Type) do
        _type.Saved = base.Saved[_type.Name]
        if _type.Saved.Enabled then
            base.CreateCollection(_type)
            base.SetupLabel(_type)
            base.RestoreFrame(_type)
        end
    end

    -- Initialize and setup Combine
    base.InitializeCombine()
    base.RestoreCombine()

    base.CreateSettingsWindow()

    EVENT_MANAGER:UnregisterForEvent(base.Addon.Name, EVENT_ADD_ON_LOADED)
end

-------------------------------------------------------------------------------------------------
--  PRIVATE FUNCTIONS --
-------------------------------------------------------------------------------------------------

function base.InitializeCombineFrame()
    local name = base.Addon.Abbreviation .. "_CombineFrame"
    base.Global.Combine.Frame = base.GetFrame(name, name)
    name = name .. "HideAll"
    base.Global.Combine.HideAll = base.GetFrame(name, name)
end

function base.CreateCollection(_type)
    local elementsAdded = 1
    for index = 1, _type.Total do
        local id = GetCollectibleIdFromType(_type.TypeId, index)
        local name, description, icon, _, unlocked, _, _, _, hint, _ = GetCollectibleInfo(id)
        if unlocked or _type.Saved.MenuShowDisabled then
            _type.Collection[id] = {
                Id = id,
                Name = name,
                EnabledTexture = icon,
                Disabled = not unlocked,
                Tooltip = string.format("Description: %s \nHint: %s", description, hint)
            }
            _type.OrderedCollection[elementsAdded] = _type.Collection[id]
            elementsAdded = elementsAdded + 1
        end
    end
    _type.Frame = base.GetFrame(_type.Name, base.Addon.Abbreviation .. "_Frame")
end

function base.RestoreFrames()
    for _, _type in pairs(base.Type) do
        base.RestoreFrame(_type)
    end
    base.RestoreCombine()
end

function base.HideOther(newType)
    for _, _type in pairs(base.Type) do
        if _type.Saved.Enabled and _type.Saved.Combine and not _type.Saved.HideAll and _type ~= newType then
            _type.Saved.HideAll = not _type.Saved.HideAll
            base.RestoreFrame(_type)
        end
    end
end

function base.RestoreFrame(_type)
    if _type.Saved.Enabled then
        --Label
        base.RestoreLabel(_type)
        --Combine and Frame
        base.SetFrameAndCombineSize(_type)
        --Frame
        base.SetupButtons(_type)
        base.RestorePosition(_type)
        base.UpdateMoveFrame(_type)
        base.UpdateFragment(_type)
    end
end

function base.InitializeCombine()
    base.Global.Combine.Saved = base.Saved.Combine
end

function base.RestoreCombine()
    base.RestoreCombineLabels()
    base.RestorePosition(base.Global.Combine)
    base.UpdateMoveFrame(base.Global.Combine)
    base.UpdateFragment(base.Global.Combine)
end

-------------------------------------------------------------------------------------------------
-- FUNCTIONS --
-------------------------------------------------------------------------------------------------

EVENT_MANAGER:RegisterForEvent(base.Addon.Name, EVENT_ADD_ON_LOADED, base.OnAddOnLoaded)