LeoAltholic.hidden = true
LeoAltholic.listingInventoryFor = 0

function LeoAltholic:OnWindowMoveStop()
    LeoAltholic.savedVariables.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.savedVariables.position or { left = 100; top = 100; }
    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)
end

function LeoAltholic.ShowUI()
    LeoAltholic.hidden = false;
    LeoAltholic.ShowTab(LeoAltholic.savedVariables.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.savedVariables.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.GetTime(seconds, short)
    if short == nil then short = false end
    local formats = {
        day = SI_TIME_FORMAT_DDHHMM_DESC_SHORT,
        hour = SI_TIME_FORMAT_HHMMSS_DESC_SHORT,
        minute = SI_TIME_FORMAT_MMSS_DESC_SHORT,
    }
    if short then
        formats = {
            day = SI_TIME_FORMAT_DDHH_DESC_SHORT,
            hour = SI_TIME_FORMAT_HHMM_DESC_SHORT,
            minute = SI_TIME_FORMAT_MMSS_DESC_SHORT,
        }
    end
    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 result = zo_strformat(GetString(formats.day), dn, hh - (dn*24), mm)
        elseif hh > 0 then result = zo_strformat(GetString(formats.hour), hh, mm, ss)
        elseif mm > 0 then result = zo_strformat(GetString(formats.minute), mm, ss)
        end
        return result
    else return '|cFF4020'..GetString(LEOALT_FINISHED)..'|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