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

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

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

function base.GetLocationValue(value)
    local result

    if value == "bottom" then
        result = BOTTOM
    elseif value == "bottomleft" then
        result = BOTTOMLEFT
    elseif value == "bottomright" then
        result = BOTTOMRIGHT
    elseif value == "center" then
        result = CENTER
    elseif value == "left" then
        result = LEFT
    elseif value == "right" then
        result = RIGHT
    elseif value == "top" then
        result = TOP
    elseif value == "topleft" then
        result = TOPLEFT
    elseif value == "topright" then
        result = TOPRIGHT
    end

    return result
end

function base.GetLocationText(value)
    local result

    if value == BOTTOM then
        result = "bottom"
    elseif value == BOTTOMLEFT then
        result = "bottomleft"
    elseif value == BOTTOMRIGHT then
        result = "bottomright"
    elseif value == CENTER then
        result = "center"
    elseif value == LEFT then
        result = "left"
    elseif value == RIGHT then
        result = "right"
    elseif value == TOP then
        result = "top"
    elseif value == TOPLEFT then
        result = "topleft"
    elseif value == TOPRIGHT then
        result = "topright"
    end

    return result
end

function base.RestorePosition(_type)
    _type.Frame:ClearAnchors()
    local combineFrame = base.Global.Combine.Frame
    if not _type.Saved.Combine or not combineFrame then
        if base.Global.IsDebugEnabled then
            d(_type.Name .. "-Combine-" .. tostring(_type.Saved.Combine) .. "-IsEmpty-" .. tostring(_type.IsEmpty) .. "-Hideall-" .. tostring(_type.Saved.HideAll))
            d(string.format("RestorePosition: %s-%s-%s-%s", _type.Name, tostring(_type.Saved.AnchorXY), tostring(_type.Saved.X), tostring(_type.Saved.Y)))
        end
        _type.Frame:SetAnchor(_type.Saved.AnchorXY, GuiRoot, TOPLEFT, _type.Saved.X, _type.Saved.Y)
    elseif _type.Saved.Combine then
        if not _type.Saved.HideAll then
            _type.Frame:SetAnchorFill(combineFrame)
        else
            _type.Frame:SetAnchor(_type.Saved.AnchorXY, combineFrame, _type.Saved.AnchorXY, 0, 0)
        end
    end
end

function base.IsAllSelected(_type)
    local isAllSelected = true
    for _, collection in pairs(_type.Collection) do
        if _type.Saved.Selected[collection.Id] == nil then
            isAllSelected = false
            break
        end
    end

    return isAllSelected
end

function base.SelectAll(_type, newValue)
    for _, collection in pairs(_type.Collection) do
        if newValue == true then
            _type.Saved.Selected[collection.Id] = newValue
        else
            _type.Saved.Selected[collection.Id] = nil
        end
    end
end

function base.GetVersion(showMinor)
    if showMinor == false or ADDON_MINOR_VERSION == nil then
        return tostring(base.Addon.Version)
    end

    return tostring(base.Addon.Version) .. "." .. tostring(base.Addon.MinorVersion)
end

function base.SetAndUpdateAccountSettings(newUseAccountSettings)
    base.Saved = ZO_SavedVars:New(base.Addon.Name .. "_Character", base.Addon.Version, nil, base.Default)

    if newUseAccountSettings ~= nil then
        base.Saved.UseAccountSettings = newUseAccountSettings
    end

    if base.Saved.UseAccountSettings then
        base.Saved = ZO_SavedVars:NewAccountWide(base.Addon.Name .. "_Account", base.Addon.Version, nil, base.Default)
    end
end

function base.GetBarWidthHeight(_type)
    local width, height, count = 0, 0, 0

    if not _type.Saved.HideAll then
        for key, value in pairs(_type.Saved.Selected) do
            if IsCollectibleUnlocked(key) and IsCollectibleValidForPlayer(key) and value then
                count = count + 1
            end
        end
    end
    local function GetBarMaxCount(type)
        local barConstraintXY = _type.Saved[type]

        if _type.Saved.Combine then
            barConstraintXY = base.Saved.Combine[type]
        end

        if barConstraintXY == 0 then
            barConstraintXY = base.Saved[type]
        end

        if count < barConstraintXY then
            barConstraintXY = count
        end
        return barConstraintXY
    end

    _type.BarDepth, _type.BarWidth = GetBarMaxCount("BarDepth"), GetBarMaxCount("BarWidth")

    if base.Global.IsDebugEnabled then
        d("SetButtonFrameSize:Type: " .. _type.Name .. "-BarDepth: " .. _type.BarDepth .. "-BarWidth: " .. _type.BarWidth)
    end

    if count > 0 then
        local barWidth = math.ceil(count / _type.BarDepth)
        local maxBarWidth = _type.BarWidth
        if maxBarWidth > 0 and maxBarWidth < barWidth then
            barWidth = maxBarWidth
        end
        local isHorizontal = _type.Saved.Horizontal
        width = base.Saved.ButtonXY * (not isHorizontal and barWidth or _type.BarDepth)
        height = base.Saved.ButtonXY * (isHorizontal and barWidth or _type.BarDepth)
    end

    return width, height
end

function base.SetFrameSizeIfExists(frame, width, height)
    if frame ~= nil then
        frame:SetHeight(height)
        frame:SetWidth(width)
    end
end