--[[
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 _, category in pairs(base.Categories) do
        category.Saved = base.Saved[category.Name]
        if category.Saved.Enabled then base.InitializeCategory(category) end
    end

    base.InitializeCombine()
    base.RestoreCombine()
end

function base.InitializeCategory(category)
    base.CreateCategory(category)
    base.SetupLabel(category)
    base.RestoreFrame(category)
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(category)
    category.Collection = {}
    category.CollectionOrdered = {}
    category.Unlocked = GetTotalUnlockedCollectiblesByCategoryType(category.Type)

    if category.Unlocked > base.Global.HighestUnlocked then base.Global.HighestUnlocked = category.Unlocked end

    for index, collectibleData in ZO_CollectibleCategoryData.SortedCollectibleIterator(category.CategoryData, {ZO_CollectibleData.IsShownInCollection}) do
        local isUnlocked = collectibleData:IsUnlocked()
        if isUnlocked or category.Saved.MenuShowDisabled then
            local collectibleId = collectibleData:GetId()
            -- TODO: IMPLEMENT BETTER TOOLTIPS...     function ZO_Tooltip:LayoutCollectible(collectibleId, deprecatedCollectionName, collectibleName, collectibleNickname, isPurchasable, description, hint, deprecatedArg, categoryType, showVisualLayerInfo, cooldownSecondsRemaining, showBlockReason)
            category.Collection[collectibleId] = {
                Id = collectibleId,
                Name = collectibleData:GetName(),
                EnabledTexture = collectibleData:GetIcon(),
                Disabled = not isUnlocked,
                Tooltip = string.format(texts.Tooltip.Main, collectibleData:GetDescription(), collectibleData:GetHint()),
                CollectibleData = collectibleData
            }
            table.insert(category.CollectionOrdered, category.Collection[collectibleId])
        end
    end

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

function base.RestoreFrames()
    for _, category in pairs(base.Categories) do base.RestoreFrame(category) end
    base.RestoreCombine()
end

function base.HideOthers(newCategory)
    for _, category in pairs(base.Categories) do
        if category.Saved.Enabled and category.Saved.IsCombined and not category.Saved.HideAll and category ~= newCategory then
            category.Saved.HideAll = not category.Saved.HideAll
            base.RestoreFrame(category)
        end
    end
end

function base.RestoreFrame(category)
    if category.Saved.Enabled then
        base.RestoreLabel(category)
        base.SetFrameAndCombineSize(category)
        base.SetupButtons(category)
        base.RestorePosition(category)
        base.UpdateToggleSettings(category)
        base.UpdateMoveFrame(category)
        base.UpdateFragment(category)
    else
        base.RemoveLabel(category)
        base.RemoveFrame(category)
    end
end

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

    if category.Frame ~= nil then category.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)