LeoAltholicWritsList = ZO_SortFilterList:Subclass()
function LeoAltholicWritsList:New(control)

    ZO_SortFilterList.InitializeSortFilterList(self, control)

    local sorterKeys =
    {
        ["name"] = {},
    }

    self.masterList = {}
    self.currentSortKey = "name"
    self.currentSortOrder = ZO_SORT_ORDER_UP
    ZO_ScrollList_AddDataType(self.list, 1, "LeoAltholicWritsListTemplate", 32, function(control, data) self:SetupEntry(control, data) end)

    self.sortFunction = function(listEntry1, listEntry2)
        return ZO_TableOrderingFunction(listEntry1.data, listEntry2.data, self.currentSortKey, sorterKeys, self.currentSortOrder)
    end

    return self
end

function LeoAltholicWritsList:SetupEntry(control, data)

    control.data = data

    control.name = GetControl(control, "Name")
    control.name:SetText(data.name)

    local child
    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
        child = GetControl(control, "Craft"..craft.."Status")
        local shown = false
        child:SetTexture("esoui/art/tutorial/menubar_help_up.dds")
        child:SetColor(unpack({1,1,1,1}))
        child.data = ""
        if data.writs[craft] ~= nil then
            local color = {1,0,0,1}
            if
            (craft == CRAFTING_TYPE_ALCHEMY and string.find(zo_strformat("<<z:1>>",data.writs[craft].name), zo_strformat("<<z:1>>",GetString(LEOALT_ALCHEMIST)))) or
                    (craft == CRAFTING_TYPE_BLACKSMITHING and string.find(zo_strformat("<<z:1>>",data.writs[craft].name), zo_strformat("<<z:1>>",GetString(LEOALT_BLACKSMITH)))) or
                    (craft == CRAFTING_TYPE_CLOTHIER and string.find(zo_strformat("<<z:1>>",data.writs[craft].name), zo_strformat("<<z:1>>",GetString(LEOALT_CLOTHIER)))) or
                    (craft == CRAFTING_TYPE_ENCHANTING and string.find(zo_strformat("<<z:1>>",data.writs[craft].name), zo_strformat("<<z:1>>",GetString(LEOALT_ENCHANTER)))) or
                    (craft == CRAFTING_TYPE_JEWELRYCRAFTING and string.find(zo_strformat("<<z:1>>",data.writs[craft].name), zo_strformat("<<z:1>>",GetString(LEOALT_JEWELRY)))) or
                    (craft == CRAFTING_TYPE_PROVISIONING and string.find(zo_strformat("<<z:1>>",data.writs[craft].name), zo_strformat("<<z:1>>",GetString(LEOALT_PROVISIONER)))) or
                    (craft == CRAFTING_TYPE_WOODWORKING and string.find(zo_strformat("<<z:1>>",data.writs[craft].name), zo_strformat("<<z:1>>",GetString(LEOALT_WOODWORKER)))) then
                if data.writs[craft].lastDone ~= nil and data.writs[craft].lastDone > LeoAltholic.TodayReset() then
                    color = {0,1,0,1}
                end
                child:SetTexture("esoui/art/buttons/accept_up.dds")
                child:SetColor(unpack(color))
                if data.writs[craft].lastDone ~= nil then
                    local diff = GetTimeStamp() - data.writs[craft].lastDone
                    if diff < 3600 then
                        child.data = zo_strformat(GetString(LEOALT_MINUTES_AGO),  math.floor(diff / 60))
                    elseif diff < 86400 then
                        child.data = zo_strformat(GetString(LEOALT_HOURS_AGO),  math.floor(diff / 3600))
                    else
                        child.data = zo_strformat(GetString(LEOALT_DAYS_AGO),  math.floor(diff / 86400))
                    end
                else
                    child.data = "|cCB110E"..GetString(LEOALT_NOT_DONE_TODAY).."|r"
                end
                shown = true
            end
        end
        if shown == false then
            child:SetTexture("esoui/art/tutorial/menubar_help_up.dds")
            child:SetColor(unpack({1,1,1,1}))
            child.data = ""
        end
    end

    ZO_SortFilterList.SetupRow(self, control, data)
end


function LeoAltholicWritsList:ColorRow(control, data, mouseIsOver)

    local color = ZO_SECOND_CONTRAST_TEXT
    local r, g, b = GetInterfaceColor(INTERFACE_COLOR_TYPE_ITEM_QUALITY_COLORS, ITEM_QUALITY_MAGIC)

    local child = GetControl(control, "Name")
    if data.name == LeoAltholic.CharName then
        child:SetColor(r, g, b)
    else
        child:SetColor(color:UnpackRGBA())
    end
end

function LeoAltholicWritsList:BuildMasterList()
    self.masterList = {}
    local list = LeoAltholic.ExportCharacters()
    for k, v in ipairs(list) do
        local data = {
            name = v.bio.name,
            writs = v.quests.writs or {}
        }
        data.queueIndex = k
        table.insert(self.masterList, data)
    end
end

function LeoAltholicWritsList:SortScrollList()
    local scrollData = ZO_ScrollList_GetDataList(self.list)
    table.sort(scrollData, self.sortFunction)
end

function LeoAltholicWritsList:FilterScrollList()
    local scrollData = ZO_ScrollList_GetDataList(self.list)
    ZO_ClearNumericallyIndexedTable(scrollData)
    for i = 1, #self.masterList do
        local data = self.masterList[i]
        table.insert(scrollData, ZO_ScrollList_CreateDataEntry(1, data))
    end
end



function LeoAltholicUI.InitWritsPanel()
    local charList = LeoAltholic.ExportCharacters()
    local control
    local anchorY = 40;
    for x,char in pairs(charList) do

        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
            control:SetTexture("esoui/art/tutorial/menubar_help_up.dds")
            control:SetColor(unpack({1,1,1,1}))
            control.data = ""
            if char.quests ~= nil and char.quests.writs ~= nil and char.quests.writs[craft] ~= nil then
                local color = {1,0,0,1}
                if
                (craft == CRAFTING_TYPE_ALCHEMY and string.find(zo_strformat("<<z:1>>",char.quests.writs[craft].name), zo_strformat("<<z:1>>",GetString(LEOALT_ALCHEMIST)))) or
                        (craft == CRAFTING_TYPE_BLACKSMITHING and string.find(zo_strformat("<<z:1>>",char.quests.writs[craft].name), zo_strformat("<<z:1>>",GetString(LEOALT_BLACKSMITH)))) or
                        (craft == CRAFTING_TYPE_CLOTHIER and string.find(zo_strformat("<<z:1>>",char.quests.writs[craft].name), zo_strformat("<<z:1>>",GetString(LEOALT_CLOTHIER)))) or
                        (craft == CRAFTING_TYPE_ENCHANTING and string.find(zo_strformat("<<z:1>>",char.quests.writs[craft].name), zo_strformat("<<z:1>>",GetString(LEOALT_ENCHANTER)))) or
                        (craft == CRAFTING_TYPE_JEWELRYCRAFTING and string.find(zo_strformat("<<z:1>>",char.quests.writs[craft].name), zo_strformat("<<z:1>>",GetString(LEOALT_JEWELRY)))) or
                        (craft == CRAFTING_TYPE_PROVISIONING and string.find(zo_strformat("<<z:1>>",char.quests.writs[craft].name), zo_strformat("<<z:1>>",GetString(LEOALT_PROVISIONER)))) or
                        (craft == CRAFTING_TYPE_WOODWORKING and string.find(zo_strformat("<<z:1>>",char.quests.writs[craft].name), zo_strformat("<<z:1>>",GetString(LEOALT_WOODWORKER)))) then
                    if char.quests.writs[craft].lastDone ~= nil and char.quests.writs[craft].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.writs[craft].lastDone ~= nil then
                        local diff = GetTimeStamp() - char.quests.writs[craft].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
            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