--[[
Author: Jarth
Filename: CollectionBars.lua
]] --
-------------------------------------------------------------------------------------------------
-- VARIABLES --
-------------------------------------------------------------------------------------------------
local base = CollectionBars
local texts = base.Texts

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

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

function base.Initialize()
    base.SetAndUpdateAccountSettings()
    base.InitializeCombineFrame()
    base.InitializeBindings()
    base.InitializeSettingsSlash()
    base.InitializeReverseBinding()
    base.InitializeWithSavedData()

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

function base.InitializeWithSavedData()
    for _, _type in pairs(base.Types) do
        _type.Saved = base.Saved[_type.Name]
        if _type.Saved.Enabled then base.InitializeType(_type) end
    end

    base.InitializeCombine()
    base.RestoreCombine()
end

function base.InitializeType(_type)
    base.CreateCategory(_type)
    base.SetupLabel(_type)
    base.RestoreFrame(_type)
end

function base.InitializeCombineFrame()
    local frameName = base.Addon.Abbreviation .. texts.Helpers.Lowdash .. texts.Components.Combine .. texts.Components.Frame
    base.Global.Combine.Frame = base.GetFrame(frameName, frameName)
    local frameNameHideAll = frameName .. texts.Components.HideAll
    base.Global.Combine.HideAll = base.GetFrame(frameNameHideAll, frameNameHideAll)
end

function base.CreateCategory(_type)
    local unlocked = GetTotalUnlockedCollectiblesByCategoryType(_type.CategoryType)
    _type.Category = {}
    _type.CategoryOrdered = {}
    _type.Unlocked = unlocked
    _type.Total = GetTotalCollectiblesByCategoryType(_type.CategoryType)
    _type.IsUsable = IsCollectibleCategoryUsable(_type.CategoryType)

    if unlocked > base.Global.HighestUnlocked then base.Global.HighestUnlocked = unlocked end

    for index = 1, _type.Total do
        local id = GetCollectibleIdFromType(_type.CategoryType, index)
        local name, description, icon, _, _unlocked, _, _, _, hint, _ = GetCollectibleInfo(id)
        if _unlocked or _type.Saved.MenuShowDisabled then
            _type.Category[id] = {Id = id, Name = name, EnabledTexture = icon, Disabled = not _unlocked, Tooltip = string.format(texts.Tooltip.Main, description, hint)}
            table.insert(_type.CategoryOrdered, _type.Category[id])
        end
    end

    _type.Frame = base.GetFrame(_type.Name, base.Addon.Abbreviation .. texts.Helpers.Lowdash .. texts.Components.Frame)
end

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

function base.HideOthers(newType)
    for _, _type in pairs(base.Types) do
        if _type.Saved.Enabled and _type.Saved.IsCombined 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
        base.RestoreLabel(_type)
        base.SetFrameAndCombineSize(_type)
        base.SetupButtons(_type)
        base.RestorePosition(_type)
        base.UpdateToggleSettings(_type)
        base.UpdateMoveFrame(_type)
        base.UpdateFragment(_type)
    else
        base.RemoveLabel(_type)
        base.RemoveFrame(_type)
    end
end

function base.RemoveFrame(_type)
    base.RemoveFragments(_type)
    for _, _value in ipairs(_type.CategoryOrdered) do
        local button = base.Buttons[_value.Id]
        if button ~= nil then button:SetHidden(true) end
    end

    if _type.Frame ~= nil then _type.Frame:SetHidden(true) 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)