LeoAltholic.hidden = true
LeoAltholic.listingInventoryFor = 0

function LeoAltholic:OnWindowMoveStop()
    LeoAltholic.globalData.position = {
        left = LeoAltholicWindow:GetLeft(),
        top = LeoAltholicWindow:GetTop()
    }
end

function LeoAltholicUI:OnHide(control, hidden)
    if hidden then LeoAltholic.HideUI() end
end

function LeoAltholicUI:OnShow(control, hidden)
    if not hidden then LeoAltholic.ShowUI() end
end

function LeoAltholic:isHidden()
    return LeoAltholic.hidden
end

function LeoAltholic.RestorePosition()
    local position = LeoAltholic.globalData.position or { left = 200; top = 200; }
    local left = position.left
    local top = position.top

    LeoAltholicWindow:ClearAnchors()
    LeoAltholicWindow:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, left, top)
end

function LeoAltholic.ChangeInventoryUI(bagId)
    LeoAltholic.ShowInventoryUI(LeoAltholic.listingInventoryFor, bagId)
end

function LeoAltholic.ShowInventoryUI(charName, bagId)
    LeoAltholic.listingInventoryFor = charName
    SCENE_MANAGER:ShowTopLevel(LeoAltholicInventoryWindow)
    local char = LeoAltholic.GetCharByName(charName)
    local sc = WINDOW_MANAGER:GetControlByName("LeoAltholicInventoryWindowListScrollChild")
    sc:SetHidden(false)

    local i = 1
    for x = 1, sc:GetNumChildren() do
        sc:GetChild(x):SetHidden(true)
    end
    for _, item in pairs(LeoAltholic.GetItems(char, bagId)) do
        local row = WINDOW_MANAGER:GetControlByName("LeoAltholicInventoryWindowRow" .. i)
        if not row then
            row = CreateControlFromVirtual("LeoAltholicInventoryWindowRow" .. i, sc, "LeoAltholicInventoryTemplate")
        end
        local control = row:GetNamedChild("Item")
        control:SetHidden(false)
        local qty = ""
        if item.count > 1 then
            qty = " (" .. item.count ..")"
        end
        control:SetText(zo_iconFormat(GetItemLinkIcon(item.link), 25, 25) .." ".. item.link .. qty)
        control:SetHandler("OnMouseEnter",
                function(self)
                    InitializeTooltip(LeoAltholicItemToolTip, LeoAltholicInventoryWindow, TOPLEFT, -450, 50, TOPLEFT)
                    LeoAltholicItemToolTip:SetLink(item.link)
                end
        )
        control:SetHandler("OnMouseExit",
                function(self)
                    ClearTooltip(LeoAltholicItemToolTip)
                end
        )
        row:SetAnchor(TOPLEFT, sc, TOPLEFT, 2, (i - 1) * 30)
        row:SetHidden(false)
        i = i + 1
    end
    sc:SetHeight(i * 30)
end

function LeoAltholic.HideInventoryUI()
    SCENE_MANAGER:HideTopLevel(LeoAltholicInventoryWindow)
end

function LeoAltholic.CloseUI()
    SCENE_MANAGER:HideTopLevel(LeoAltholicWindow)
    SCENE_MANAGER:HideTopLevel(LeoAltholicInventoryWindow)
end

function LeoAltholic.ShowUI()
    LeoAltholic.hidden = false;
    LeoAltholic.ShowTab(LeoAltholic.globalData.activeTab or "Bio")
end

function LeoAltholic.HideUI()
    LeoAltholic.hidden = true;
    LeoAltholic.HideInventoryUI()
end

function LeoAltholic.ToggleUI()
    SCENE_MANAGER:ToggleTopLevel(LeoAltholicWindow)
end

function LeoAltholic.ShowTab(tab)
    LeoAltholic.globalData.activeTab = tab
    LeoAltholicWindowTitle:SetText(LeoAltholic.displayName .. " v" .. LeoAltholic.version .. " - " .. tab)
    local control
    for _,panel in ipairs(LeoAltholic.panelList) do
        control = WINDOW_MANAGER:GetControlByName('LeoAltholicWindow' .. panel .. 'Panel')
        control:SetHidden(true)
    end
    control = WINDOW_MANAGER:GetControlByName("LeoAltholicWindow" .. tab .. "Panel")
    control:SetHidden(false)
end

function LeoAltholic.FormatTime(seconds, short, colorizeCountdown)
    if short == nil then short = false end
    local formats = {
        dhm = SI_TIME_FORMAT_DDHHMM_DESC_SHORT,
        day = SI_TIME_FORMAT_DAYS,
        hm = SI_TIME_FORMAT_HHMM_DESC_SHORT,
        hms = SI_TIME_FORMAT_HHMMSS_DESC_SHORT,
        hour = SI_TIME_FORMAT_HOURS,
        ms = SI_TIME_FORMAT_MMSS_DESC_SHORT,
    }
    if seconds and seconds > 0 then
        local ss = seconds % 60
        local mm = math.floor(seconds / 60)
        local hh = math.floor(mm / 60)
        mm = mm % 60
        local dn = math.floor(hh / 24)
        local result = ''
        if dn > 0 then
            if short then
                result = zo_strformat(GetString(formats.day), dn) .." "..zo_strformat(GetString(formats.hour), hh - (dn*24))
            else
                result = zo_strformat(GetString(formats.dhm), dn, hh - (dn*24), mm)
            end
        elseif hh > 0 then
            if short then
                result = zo_strformat(GetString(formats.hm), hh, mm)
            else
                result = zo_strformat(GetString(formats.hms), hh, mm, ss)
            end
        elseif mm >= 0 then result = zo_strformat(GetString(formats.ms), mm, ss)
        end
        if colorizeCountdown == true then
            if seconds < 3600 then result = '|cFF4020'..result..'|r'
            elseif seconds < 86400 then result = '|cCCCC00'..result..'|r'
            elseif seconds < 604800 then result = '|cFFFFFF'..result..'|r'
            else result = '|c00FF00'..result..'|r' end
        end
        return result
    else return '|cFF4020'..GetString(SI_GAMEPAD_CAMPAIGN_SCORING_DURATION_REMAINING_DONE)..'|r' end
end

function LeoAltholicUI.InitPanels()
    LeoAltholicUI.bioList = LeoAltholicBioList:New(LeoAltholicWindowBioPanelListScroll)
    LeoAltholicUI.bioList:RefreshData()

    LeoAltholicUI.statsList = LeoAltholicStatsList:New(LeoAltholicWindowStatsPanelListScroll)
    LeoAltholicUI.statsList:RefreshData()

    LeoAltholicUI.championList = LeoAltholicChampionList:New(LeoAltholicWindowChampionPanelListScroll)
    LeoAltholicUI.championList:RefreshData()

    LeoAltholicUI.skillsList = LeoAltholicSkillsList:New(LeoAltholicWindowSkillsPanelListScroll)
    LeoAltholicUI.skillsList:RefreshData()

    LeoAltholicUI.skills2List = LeoAltholicSkills2List:New(LeoAltholicWindowSkills2PanelListScroll)
    LeoAltholicUI.skills2List:RefreshData()

    LeoAltholicUI.writsList = LeoAltholicWritsList:New(LeoAltholicWindowWritsPanelListScroll)
    LeoAltholicUI.writsList:RefreshData()

    LeoAltholicUI.invList = LeoAltholicInventoryList:New(LeoAltholicWindowInventoryPanelListScroll)
    LeoAltholicUI.invList:RefreshData()

    LeoAltholicUI.researchList = LeoAltholicResearchList:New(LeoAltholicWindowResearchPanelListScroll)
    LeoAltholicUI.researchList:RefreshData()

    LeoAltholicUI.InitTrackedPanel()
end