LeoAltholic.hidden = true
LeoAltholic.listingInventoryFor = 0

function LeoAltholic:OnWindowMoveStop()
    LeoAltholic.savedVariables.position = {
        left = LeoAltholicWindow:GetLeft(),
        top = LeoAltholicWindow:GetTop()
    }
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(charId, bagId)
    LeoAltholic.listingInventoryFor = charId
    SCENE_MANAGER:ShowTopLevel(LeoAltholicInventoryWindow)
    LeoAltholicInventoryWindow:SetHidden(false)
    local char = LeoAltholic.GetCharacters()[charId]
    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)
    LeoAltholicInventoryWindow:SetHidden(true)
end

function LeoAltholic.ShowUI()
    SCENE_MANAGER:ShowTopLevel(LeoAltholicWindow)
    LeoAltholicWindow:SetHidden(false)
    LeoAltholic.hidden = false;
    LeoAltholic.ShowTab(LeoAltholic.savedVariables.activeTab or "Bio")
end

function LeoAltholic.HideUI()
    SCENE_MANAGER:HideTopLevel(LeoAltholicWindow)
    LeoAltholicWindow:SetHidden(true)
    LeoAltholic.HideInventoryUI()
    LeoAltholic.hidden = true;
end

function LeoAltholic.ToggleUI()
    SCENE_MANAGER:ToggleTopLevel(LeoAltholicWindow)
    if LeoAltholic:isHidden() then
        LeoAltholic.ShowUI()
    else
        LeoAltholic.HideUI()
    end
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.InitializeCharacterFrames()
    for x,char in pairs(LeoAltholic.GetCharacters()) do

        for _,panel in ipairs(LeoAltholic.panelList) do
            local bg
            local row
            local control
            if panel == "Daily" then
                local sc = WINDOW_MANAGER:GetControlByName("LeoAltholicWindowDailyPanel")
                row = CreateControlFromVirtual("LeoAltholic"..panel.."Row" .. x, sc, "LeoAltholic"..panel.."RowTemplate")
                row:SetAnchor(TOPLEFT,sc,TOPLEFT,0,22 + ((x - 1) * 40))
                bg = row:GetNamedChild("BG")
            else
                local sc = WINDOW_MANAGER:GetControlByName("LeoAltholicWindow"..panel.."PanelScrollChild")
                row = CreateControlFromVirtual("LeoAltholic"..panel.."Row" .. x, sc, "LeoAltholic"..panel.."RowTemplate")
                row:SetHidden(false)
                local anchorY = 109;
                if panel == "Champion" then
                    anchorY = 139
                elseif panel == "Tracked" then
                    anchorY = 169
                elseif panel == "Research" then
                    anchorY = 262
                elseif panel == "Skills" then
                    anchorY = 522
                end
                row:SetAnchor(TOPLEFT,sc,TOPLEFT,0,(x - 1) * anchorY)
                bg = row:GetNamedChild("BG")
            end
            if char.bio.name == LeoAltholic.CharName then
                bg:SetCenterColor(unpack({0.2,0.2,0.2,1 }))
                bg:SetEdgeColor(0.22,0.77,0.22,1)
            end

            if panel == "Champion" then
                for i = 1, GetNumChampionDisciplines() do
                    local dispName = GetChampionDisciplineName(i)
                    local dAttribute = GetChampionDisciplineAttribute(i)
                    local label = row:GetNamedChild("Disc"..i.."Label")
                    label:SetText(dispName)
                    label:SetHandler('OnMouseEnter', function(self) LeoAltholic.TooltipChampionSkill(self, true, x, dAttribute, i) end)
                    label:SetHandler('OnMouseExit', function(self) LeoAltholic.TooltipChampionSkill(self, false) end)
                end
            elseif panel == "Inventory" then
                control = row:GetNamedChild("ListButton")
                control:SetHandler('OnClicked', function() LeoAltholic.ShowInventoryUI(x, BAG_BACKPACK) end)
            elseif panel == "Research" then
                for _,craft in pairs(LeoAltholic.craftResearch) do
                    local label = row:GetNamedChild("Craft"..craft.."Label")
                    label:SetText(GetCraftingSkillName(craft))
                end
            elseif panel == "Skills" then
                local c = 1
                for i = 1, 3 do
                    if char.skills.class[i] then
                        control = row:GetNamedChild("Class"..c.."Label")
                        control:SetText(char.skills.class[i].name)
                        control:SetHandler('OnMouseEnter', function(self) LeoAltholic.TooltipSkill(self, true, x, "class", i) end)
                        control:SetHandler('OnMouseExit', function(self) LeoAltholic.TooltipSkill(self, false) end)
                        control = row:GetNamedChild("Class"..c.."Rank")
                        control:SetText(char.skills.class[i].rank)
                        c = c + 1
                    end
                end
                c = 1
                for i = 1, 3 do
                    if char.skills.armor[i] then
                        control = row:GetNamedChild("Armor"..c.."Label")
                        control:SetText(char.skills.armor[i].name)
                        control:SetHandler('OnMouseEnter', function(self) LeoAltholic.TooltipSkill(self, true, x, "armor", i) end)
                        control:SetHandler('OnMouseExit', function(self) LeoAltholic.TooltipSkill(self, false) end)
                        control = row:GetNamedChild("Armor"..c.."Rank")
                        control:SetText(char.skills.armor[i].rank)
                        c = c + 1
                    end
                end
                c = 1
                for i = 1, 6 do
                    if char.skills.weapon[i] then
                        control = row:GetNamedChild("Weapon"..c.."Label")
                        control:SetText(char.skills.weapon[i].name)
                        control:SetHandler('OnMouseEnter', function(self) LeoAltholic.TooltipSkill(self, true, x, "weapon", i) end)
                        control:SetHandler('OnMouseExit', function(self) LeoAltholic.TooltipSkill(self, false) end)
                        control = row:GetNamedChild("Weapon"..c.."Rank")
                        control:SetText(char.skills.weapon[i].rank)
                        c = c + 1
                    end
                end
                c = 1
                for i = 1, 7 do
                    if char.skills.craft[i] then
                        control = row:GetNamedChild("Craft"..c.."Label")
                        control:SetText(char.skills.craft[i].name)
                        control:SetHandler('OnMouseEnter', function(self) LeoAltholic.TooltipSkill(self, true, x, "craft", i) end)
                        control:SetHandler('OnMouseExit', function(self) LeoAltholic.TooltipSkill(self, false) end)
                        control = row:GetNamedChild("Craft"..c.."Rank")
                        control:SetText(char.skills.craft[i].rank)
                        c = c + 1
                    end
                end
                c = 1
                for i = 1, 6 do
                    if char.skills.guild[i] then
                        control = row:GetNamedChild("Guild"..c.."Label")
                        control:SetText(char.skills.guild[i].name)
                        control:SetHandler('OnMouseEnter', function(self) LeoAltholic.TooltipSkill(self, true, x, "guild", i) end)
                        control:SetHandler('OnMouseExit', function(self) LeoAltholic.TooltipSkill(self, false) end)
                        control = row:GetNamedChild("Guild"..c.."Rank")
                        control:SetText(char.skills.guild[i].rank)
                        c = c + 1
                    end
                end
                c = 1
                for i = 1, 4 do
                    if char.skills.world[i] then
                        control = row:GetNamedChild("World"..c.."Label")
                        control:SetText(char.skills.world[i].name)
                        control:SetHandler('OnMouseEnter', function(self) LeoAltholic.TooltipSkill(self, true, x, "world", i) end)
                        control:SetHandler('OnMouseExit', function(self) LeoAltholic.TooltipSkill(self, false) end)
                        control = row:GetNamedChild("World"..c.."Rank")
                        control:SetText(char.skills.world[i].rank)
                        c = c + 1
                    end
                end
                c = 1
                for i = 1, 3 do
                    if char.skills.ava[i] then
                        control = row:GetNamedChild("Alliance"..c.."Label")
                        control:SetText(char.skills.ava[i].name)
                        control:SetHandler('OnMouseEnter', function(self) LeoAltholic.TooltipSkill(self, true, x, "ava", i) end)
                        control:SetHandler('OnMouseExit', function(self) LeoAltholic.TooltipSkill(self, false) end)
                        control = row:GetNamedChild("Alliance"..c.."Rank")
                        control:SetText(char.skills.ava[i].rank)
                        c = c + 1
                    end
                end
                if char.skills.racial[1] then
                    control = row:GetNamedChild("Racial1Label")
                    control:SetText(char.skills.racial[1].name)
                    control:SetHandler('OnMouseEnter', function(self) LeoAltholic.TooltipSkill(self, true, x, "racial", 1) end)
                    control:SetHandler('OnMouseExit', function(self) LeoAltholic.TooltipSkill(self, false) end)
                    control = row:GetNamedChild("Racial1Rank")
                    control:SetText(char.skills.racial[1].rank)
                end
            elseif panel == "Daily" then
                if char.quests ~= nil and char.quests.tracked ~= nil then
                    for _, craft in pairs({CRAFTING_TYPE_ALCHEMY,CRAFTING_TYPE_BLACKSMITHING,CRAFTING_TYPE_CLOTHIER,CRAFTING_TYPE_ENCHANTING,CRAFTING_TYPE_JEWELRYCRAFTING,CRAFTING_TYPE_PROVISIONING,CRAFTING_TYPE_WOODWORKING}) do
                        control = row:GetNamedChild("Craft"..craft.."Status")
                        local shown = false
                        for i = 1, 10 do
                            if shown == false and char.quests.tracked[i] ~= nil and char.quests.tracked[i].questType == QUEST_TYPE_CRAFTING then
                                local color = {1,0,0,1}
                                if (craft == CRAFTING_TYPE_ALCHEMY and char.quests.tracked[i].name == GetString(LEOALT_ALCHEMY_WRIT)) or
                                    (craft == CRAFTING_TYPE_BLACKSMITHING and char.quests.tracked[i].name == GetString(LEOALT_BLACKSMITHING_WRIT)) or
                                    (craft == CRAFTING_TYPE_CLOTHIER and char.quests.tracked[i].name == GetString(LEOALT_CLOTHIER_WRIT)) or
                                    (craft == CRAFTING_TYPE_ENCHANTING and char.quests.tracked[i].name == GetString(LEOALT_ENCHANTING_WRIT)) or
                                    (craft == CRAFTING_TYPE_JEWELRYCRAFTING and char.quests.tracked[i].name == GetString(LEOALT_JEWELRYCRAFTING_WRIT)) or
                                    (craft == CRAFTING_TYPE_PROVISIONING and char.quests.tracked[i].name == GetString(LEOALT_PROVISIONING_WRIT)) or
                                    (craft == CRAFTING_TYPE_WOODWORKING and char.quests.tracked[i].name == GetString(LEOALT_WOODWORKING_WRIT)) then

                                    if char.quests.tracked[i].lastDone ~= nil and char.quests.tracked[i].lastDone > LeoAltholic.TodayReset() then
                                        color = {0,1,0,1}
                                    end
                                    control:SetTexture("esoui/art/buttons/accept_up.dds")
                                    control:SetColor(unpack(color))
                                    if char.quests.tracked[i].lastDone ~= nil then
                                        local diff = GetTimeStamp() - char.quests.tracked[i].lastDone
                                        if diff < 3600 then
                                            control.data = zo_strformat(GetString(LEOALT_MINUTES_AGO),  math.floor(diff / 60))
                                        elseif diff < 86400 then
                                            control.data = zo_strformat(GetString(LEOALT_HOURS_AGO),  math.floor(diff / 3600))
                                        else
                                            control.data = zo_strformat(GetString(LEOALT_DAYS_AGO),  math.floor(diff / 86400))
                                        end
                                    else
                                        control.data = "|cCB110E"..GetString(LEOALT_NOT_DONE_TODAY).."|r"
                                    end
                                    shown = true
                                end
                            end
                        end
                        if shown == false then
                            control:SetTexture("esoui/art/tutorial/menubar_help_up.dds")
                            control:SetColor(unpack({1,1,1,1}))
                            control.data = ""
                        end
                    end
                end
            end
        end
    end
end

function LeoAltholic.GetTime(seconds)
    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 = tostring(dn) .. GetString(LEOALT_D) .." " .. tostring (hh - (dn*24)) .. GetString(LEOALT_H)
        elseif hh > 0 then result = tostring (hh) .. GetString(LEOALT_H) .." " .. tostring (mm) .. GetString(LEOALT_M)
        elseif mm > 0 then result = mm .. GetString(LEOALT_M) .." " .. ss .. GetString(LEOALT_S)
        end
        return result
    else return '|cFF4020'..GetString(LEOALT_FINISHED)..'|r' end
end

function LeoAltholic.TooltipChampionSkill(control, visible, charId, attribute, discipline)

    if visible then
        if not parent then parent = control end

        control.text = InformationTooltip
        InitializeTooltip(control.text,control,2,1,0,8)

        local char = LeoAltholic.GetCharacters()[charId]
        local added = false
        for j = 1, GetNumChampionDisciplineSkills(discipline) do
            local skillName = GetChampionSkillName(discipline, j)
            local points = char.champion[attribute].disciplines[discipline].skills[j]
            if type(points) == 'number' and points > 0 then
                control.text:AddLine("|cE8DFAF" .. skillName .. "|r " .. points)
                added = true
            elseif points == true then
                control.text:AddLine("|cE8DFAF" .. skillName .. "|r |c21A121"..GetString(LEOALT_UNLOCKED).."|r")
                added = true
            end
        end
        if added then
            control.text:SetHidden(false)
        else
            control.text:SetHidden(true)
        end
    else
        ClearTooltip(control.text)
        control.text:SetHidden(true)
        control.text = nil
    end
end

function LeoAltholic.TooltipSkill(control, visible, charId, line, lineId)

    if visible then
        if not parent then parent = control end

        control.text = InformationTooltip
        InitializeTooltip(control.text,control,2,1,0,8)

        local char = LeoAltholic.GetCharacters()[charId]
        local added = false
        for _, s in pairs(char.skills[line][lineId].list) do
            local skillName = s.name
            local rank = s.earnedRank
            control.text:AddLine("|cE8DFAF" .. skillName .. "|r " .. rank)
            added = true
        end
        if added then
            control.text:SetHidden(false)
        else
            control.text:SetHidden(true)
        end
    else
        ClearTooltip(control.text)
        control.text:SetHidden(true)
        control.text = nil
    end
end

local function formatNumber(amount)
    if amount == nil then return nil; end
    if type(amount) == "string" then amount = tonumber( amount ) end
    if type(amount) ~= "number" then return amount; end
    if amount < 1000 then return amount; end
    return FormatIntegerWithDigitGrouping( amount, GetString( SI_DIGIT_GROUP_SEPARATOR ) )
end

function LeoAltholic.DisplayCharacterFrames()
    local control
    for _,panel in ipairs(LeoAltholic.panelList) do
        if panel ~= "Daily" then
            control = WINDOW_MANAGER:GetControlByName('LeoAltholicWindow' .. panel .. 'PanelScrollChild')
            control:SetHeight(#LeoAltholic.GetCharacters() * 109)
        end
    end
    control = WINDOW_MANAGER:GetControlByName('LeoAltholicWindowChampionPanelScrollChild')
    control:SetHeight(#LeoAltholic.GetCharacters() * 139)
    control = WINDOW_MANAGER:GetControlByName('LeoAltholicWindowTrackedPanelScrollChild')
    control:SetHeight(#LeoAltholic.GetCharacters() * 169)
    control = WINDOW_MANAGER:GetControlByName('LeoAltholicWindowResearchPanelScrollChild')
    control:SetHeight(#LeoAltholic.GetCharacters() * 262)
    control = WINDOW_MANAGER:GetControlByName('LeoAltholicWindowSkillsPanelScrollChild')
    control:SetHeight(#LeoAltholic.GetCharacters() * 522)

    for x,char in pairs(LeoAltholic.GetCharacters()) do

        for _,panel in ipairs(LeoAltholic.panelList) do
            local row = WINDOW_MANAGER:GetControlByName('LeoAltholic' .. panel .. 'Row'..x)

            control = row:GetNamedChild("Name")
            control:SetText(char.bio.name)

            if panel ~= "Daily" then
                control = row:GetNamedChild("Alliance")
                local icon = ZO_GetAllianceIcon(char.bio.alliance.id)
                control:SetText("|cF1FF77|t50:90:" .. icon .. "|t|r ")
                control.data = char.bio.alliance.name

                control = row:GetNamedChild("RaceClass")
                control:SetText(char.bio.race .. " " .. char.bio.class)
            end
        end

        local row = WINDOW_MANAGER:GetControlByName('LeoAltholicBioRow'..x)

        control = row:GetNamedChild("Level")
        if char.bio.isChampion then
            control:SetText("|t24:24:esoui/art/champion/champion_icon.dds|t" .. char.bio.championPoints)
        else
            control:SetText(char.bio.level)
        end

        control = row:GetNamedChild("AllianceRank")
        control:SetText(GetAvARankName(char.bio.gender, char.bio.alliance.rank))
        control = row:GetNamedChild("AttributePoints")
        control:SetText(char.attributes.unspent)
        control = row:GetNamedChild("Played")
        control:SetText(LeoAltholic.GetTime(char.secondsPlayed))
        control = row:GetNamedChild("Bounty")
        control:SetText(char.bounty .. " |t14:14:esoui/art/currency/currency_gold.dds|t")

        local riding = '|t20:20:esoui/art/mounts/ridingskill_speed.dds|t ' .. char.attributes.riding.speed .. '%' ..
                ' |t20:20:esoui/art/mounts/ridingskill_stamina.dds|t ' .. char.attributes.riding.stamina ..
                ' |t20:20:esoui/art/mounts/ridingskill_capacity.dds|t ' .. char.attributes.riding.capacity ..
                ' |t22:22:esoui/art/miscellaneous/timer_32.dds|t ' .. LeoAltholic.GetTime(char.attributes.riding.time - GetTimeStamp())
        control = row:GetNamedChild("Riding")
        control:SetText(riding)


        row = WINDOW_MANAGER:GetControlByName('LeoAltholicStatsRow'..x)

        control = row:GetNamedChild("AttributePoints")
        control:SetText("|c596cfd" .. char.attributes.magicka.points .. "|r / |cCB110E" .. char.attributes.health.points .. "|r / |c21A121" .. char.attributes.stamina.points .. "|r")

        control = row:GetNamedChild("MaxAttributes")
        control:SetText("|c596cfd" .. char.attributes.magicka.max .. "|r / |cCB110E" .. char.attributes.health.max .. "|r / |c21A121" .. char.attributes.stamina.max .. "|r")

        control = row:GetNamedChild("RecoveryAttributes")
        control:SetText("|c596cfd" .. char.attributes.magicka.recovery .. "|r / |cCB110E" .. char.attributes.health.recovery .. "|r / |c21A121" .. char.attributes.stamina.recovery .. "|r")

        control = row:GetNamedChild("SpellPoints")
        control:SetText(string.format("%.1f%%", char.attributes.spell.criticalChance))

        control = row:GetNamedChild("WeaponPoints")
        control:SetText(string.format("%.1f%%", char.attributes.weapon.criticalChance))

        row = WINDOW_MANAGER:GetControlByName('LeoAltholicChampionRow'..x)
        for _, attribute in ipairs({ATTRIBUTE_MAGICKA, ATTRIBUTE_HEALTH, ATTRIBUTE_STAMINA}) do
            control = row:GetNamedChild("Attribute" .. attribute)
            local total = char.champion[attribute].spent + char.champion[attribute].unspent
            local color = '|c21A121'
            if char.champion[attribute].unspent > 0 then
                color = '|cCB110E'
            end
            control:SetText(color .. char.champion[attribute].spent .. '/' .. total .. '|r')
        end

        for i = 1, GetNumChampionDisciplines() do
            local dAttribute = GetChampionDisciplineAttribute(i)
            control = row:GetNamedChild("Disc" .. i)
            control:SetText(char.champion[dAttribute].disciplines[i].spent)
        end

        --[[
        if char.quests ~= nil and char.quests.tracked ~= nil then
            row = WINDOW_MANAGER:GetControlByName('LeoAltholicTrackedRow'..x)
            for i = 1, 10 do
                local label = row:GetNamedChild("Quest" .. i .. "Label")
                local done = row:GetNamedChild("Quest" .. i .. "Done")
                if char.quests.tracked[i] ~= nil and char.quests.tracked[i].questType ~= QUEST_TYPE_CRAFTING then
                    label:SetText(char.quests.tracked[i].name)
                    label:SetHandler('OnMouseUp', function(control, button, upInside)
                        if upInside == true and button == MOUSE_BUTTON_INDEX_RIGHT then
                            LeoAltholic.log(zo_strformat(GetString(LEOALT_REMOVED_FROM), label:GetText(), char.bio.name))
                            table.remove(LeoAltholic.savedVariables.CharList[char.bio.name].quests.tracked, i)
                            control:SetHidden(true)
                            control:GetParent():GetNamedChild("Quest" .. i .. "Done"):SetHidden(true)
                        end
                    end)
                    if char.quests.tracked[i].lastDone ~= nil then
                        if char.quests.tracked[i].lastDone <= LeoAltholic.TodayReset() then
                            done:SetText("|cCB110Enot done today|r")
                        else
                            local diff = GetTimeStamp() - char.quests.tracked[i].lastDone
                            if diff < 3600 then
                                done:SetText(zo_strformat(GetString(LEOALT_MINUTES_AGO),  math.floor(diff / 60)))
                            elseif diff < 86400 then
                                done:SetText(zo_strformat(GetString(LEOALT_HOURS_AGO),  math.floor(diff / 3600)))
                            else
                                done:SetText(zo_strformat(GetString(LEOALT_DAYS_AGO),  math.floor(diff / 86400)))
                            end
                        end
                    else
                        done:SetText("|cCB110E"..GetString(LEOALT_NOT_DONE_TODAY).."|r")
                    end
                end
            end
        end
        ]]

        row = WINDOW_MANAGER:GetControlByName('LeoAltholicInventoryRow'..x)
        local color = '|c21A121'
        if char.inventory.free <= 25 then color = '|c596cfd' end
        if char.inventory.free <= 10 then color = '|cCB110E' end
        control = row:GetNamedChild("Bag")
        control:SetText(color .. char.inventory.used .. "|r / " .. char.inventory.size)
        control = row:GetNamedChild("SoulGems")
        control:SetText("|c21A121" .. char.inventory.soulGemFilled .. '|r / ' .. char.inventory.soulGemEmpty)
        control = row:GetNamedChild("Gold")
        control:SetText(formatNumber(char.inventory.gold))
        control = row:GetNamedChild("AP")
        control:SetText(formatNumber(char.inventory.ap))
        control = row:GetNamedChild("TelVar")
        control:SetText(formatNumber(char.inventory.telvar))
        control = row:GetNamedChild("Writ")
        control:SetText(formatNumber(char.inventory.writVoucher))

        row = WINDOW_MANAGER:GetControlByName('LeoAltholicResearchRow'..x)
        for _,craft in pairs(LeoAltholic.craftResearch) do
            local i = 1
            local researching = 0
            for line = 1, GetNumSmithingResearchLines(craft) do
                local lineName, lineIcon = GetSmithingResearchLineInfo(craft, line)
                for trait = 1, LeoAltholic.maxTraits do
                    local traitType = GetSmithingResearchLineTraitInfo(craft, line, trait)
                    local traitName = GetString('SI_ITEMTRAITTYPE',traitType)
                    local traitData = char.research[craft][line][trait]
                    if type(traitData) == 'number' then
                        control = row:GetNamedChild("Craft"..craft.."Label"..i)
                        control:SetText(' |t28:28:'..lineIcon..'|t  ' .. traitName)
                        control:SetHandler('OnMouseEnter', function(self) ZO_Tooltips_ShowTextTooltip(self, TOP, lineName) end)
                        control:SetHandler("OnMouseExit", function () ZO_Tooltips_HideTextTooltip() end)
                        control = row:GetNamedChild("Craft"..craft.."Timer"..i)
                        control:SetText(LeoAltholic.GetTime(traitData - GetTimeStamp()))
                        if traitData - GetTimeStamp() <= 3600 then
                            control:SetColor(1, 1, 0, 1)
                        else
                            control:SetColor(1, 1, 1, 1)
                        end
                        i = i + 1
                        if traitData - GetTimeStamp() > 0 then
                            researching = researching + 1
                        end
                    end
                end
            end
            control = row:GetNamedChild("Craft"..craft)
            color = '|c21A121'
            if researching < char.research[craft].max then
                color = '|cCB110E'
            end
            control:SetText(color .. researching .. '/' .. char.research[craft].max .. '|r')
        end

    end
end

function LeoAltholic:OnUpdate()
    if LeoAltholic:isHidden() then
        return
    end

    local control
    for x,char in pairs(LeoAltholic.GetCharacters()) do

        local riding = '|t20:20:esoui/art/mounts/ridingskill_speed.dds|t ' .. char.attributes.riding.speed .. '%' ..
                ' |t20:20:esoui/art/mounts/ridingskill_stamina.dds|t ' .. char.attributes.riding.stamina ..
                ' |t20:20:esoui/art/mounts/ridingskill_capacity.dds|t ' .. char.attributes.riding.capacity ..
                ' |t22:22:esoui/art/miscellaneous/timer_32.dds|t ' .. LeoAltholic.GetTime(char.attributes.riding.time - GetTimeStamp())
        local row = WINDOW_MANAGER:GetControlByName('LeoAltholicBioRow'..x)
        control = row:GetNamedChild('Riding')
        control:SetText(riding)

        row = WINDOW_MANAGER:GetControlByName('LeoAltholicResearchRow'..x)
        for _,craft in pairs(LeoAltholic.craftResearch) do
            local i = 1
            for line = 1, GetNumSmithingResearchLines(craft) do
                for trait = 1, LeoAltholic.maxTraits do
                    local traitData = char.research[craft][line][trait]
                    if type(traitData) == 'number' then
                        control = row:GetNamedChild("Craft"..craft.."Timer"..i)
                        control:SetText(LeoAltholic.GetTime(traitData - GetTimeStamp()))
                        if traitData - GetTimeStamp() <= 3600 then
                            control:SetColor(1, 1, 0, 1)
                        else
                            control:SetColor(1, 1, 1, 1)
                        end
                        i = i + 1
                    end
                end
            end
        end
    end
end