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

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

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

function base.SetupLabel(_type)
    -- SetupLabelFrame
    if _type.FrameLabel == nil then
        _type.FrameLabel = GetControl(base.Global.HideAllId .. _type.Name)
        _type.FrameLabelButton = GetControl(base.Global.HideAllId .. _type.Name .. "Button")
        if not _type.FrameLabel then
            _type.FrameLabel = base.WM:CreateControlFromVirtual(base.Global.HideAllId, _type.Frame, base.Global.HideAllId, _type.Name)
            _type.FrameLabelButton = GetControl(base.Global.HideAllId .. _type.Name .. "Button")
            _type.FrameLabel:SetDrawLevel(2)
        end
    end
    _type.FrameLabel:SetHidden(not _type.Saved.LabelShow and not _type.Saved.Combine)

    -- SetupLabelButton
    local button = GetControl(base.Global.HideAllId .. _type.Name .. "Button")
    if button then
        local hideAllFunc = nil
        if _type.Saved.HideAllEnabled or _type.Saved.Combine then
            hideAllFunc = function()
                _type.Saved.HideAll = not _type.Saved.HideAll
                -- Hide other Combine panels..
                if _type.Saved.Combine then
                    base.HideOther(_type)
                end
                base.RestoreFrame(_type)
                base.RestoreCombine()
            end
            button:SetMouseOverTexture("/EsoUI/Art/Buttons/decline_over.dds")
            button:SetPressedTexture("/EsoUI/Art/Buttons/decline_down.dds")
        end
        button:SetHandler("OnClicked", hideAllFunc)
    end
end

function base.RestoreLabel(_type)
    local frameLabel = _type.FrameLabel
    local frameLabelButton = _type.FrameLabelButton

    if frameLabel ~= nil and frameLabelButton ~= nil then
        local postFix = ""
        if _type.Saved.HideAllEnabled then
            if not _type.Saved.HideAll then
                postFix = " -"
            else
                postFix = " +"
            end
        end
        local label = _type.Saved.Label
        frameLabel:ClearAnchors()
        if not _type.Saved.Combine then
            frameLabel:SetAnchor(label.Position, _type.Frame, label.PositionTarget, label.OffsetX, label.OffsetY)
        end
        frameLabel:SetHeight(label.Height)
        frameLabel:SetWidth(label.Width)
        frameLabelButton:SetFont(label.Font)
        frameLabelButton:SetHorizontalAlignment(TEXT_ALIGN_LEFT)
        frameLabelButton:SetVerticalAlignment(TOP)
        frameLabelButton:SetText(_type.Saved.Display .. postFix)
    end
    frameLabel:SetHidden(not _type.Saved.LabelShow and not _type.Saved.Combine)
end

function base.RestoreCombineLabels()
    local combineLabel = base.Saved.Combine.Label
    local width = 0
    local height = 0
    local positionTarget = combineLabel.PositionTarget
    local position = combineLabel.Position
    for _, _type in pairs(base.TypeOrdered) do
        if _type.Saved.Combine and _type.FrameLabel and not _type.IsEmpty then
            _type.FrameLabel:ClearAnchors()
            _type.FrameLabel:SetHidden(not _type.Saved.LabelShow and not _type.Saved.Combine and not base.HasSelected(_type))
            if not _type.FrameLabel:IsHidden() or (_type.Saved.LabelShow and base.HasSelected(_type)) then
                width = width + _type.Saved.Label.OffsetX
                local offsetY = _type.Saved.Label.OffsetY + combineLabel.OffsetY
                _type.FrameLabel:SetAnchor(TOPLEFT, base.Global.Combine.HideAll, TOPLEFT, width, offsetY)
                width = width + _type.FrameLabel:GetWidth()
                local labelHeight = _type.FrameLabel:GetHeight()
                if labelHeight > height then
                    height = labelHeight
                end
            end
        end
    end

    local frame = base.Global.Combine.HideAll
    if frame ~= nil and base.Global.Combine.Frame ~= nil then
        frame:ClearAnchors()
        frame:SetWidth(width)
        frame:SetHeight(height)
        frame:SetAnchor(position, base.Global.Combine.Frame, positionTarget, combineLabel.OffsetX, combineLabel.OffsetY)
    end
end

function base.HasSelected(_type)
    for _ in pairs(_type.Saved.Selected) do
        return true
    end
    return false
end