function LeoAltholicChecklistUI:OnWindowMoveStop()
    LeoAltholic.globalData.positionChecklist = {
        left = LeoAltholicChecklist:GetLeft(),
        top = LeoAltholicChecklist:GetTop()
    }
end

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

    LeoAltholicChecklist:ClearAnchors()
    LeoAltholicChecklist:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, left, top)
    if LeoAltholic.charData.settings.checklist.enabled == false then
        LeoAltholicChecklist:SetHidden(true)
    else
        LeoAltholicChecklist:SetHidden(false)
    end
end

local SPACE = 32
local checklist = WINDOW_MANAGER:GetControlByName("LeoAltholicChecklist")
local panel = GetControl(checklist, "Panel")
local height = 0
local anchorY = SPACE + 4
local lastChecklist = GetTimeStamp()
local checklistDone = 0

local function createItem(label, labelText, texture, canShow)
    if canShow then
        texture:SetTexture("esoui/art/buttons/decline_up.dds")
        texture:SetColor(1,0,0,1)
        texture:SetDimensions(32, 32)
        texture:SetAnchor(TOPLEFT, checklist, TOPLEFT, 10, height + anchorY)

        height = height + SPACE
        label:SetText(labelText)
        label:SetHidden(false)
        texture:SetHidden(false)
    else
        label:SetHidden(true)
        texture:SetHidden(true)
    end
end

function LeoAltholicChecklistUI:checkReset()
    if LeoAltholic.IsBeforeReset(lastChecklist) then
        LeoAltholicChecklistUI:update()
        lastChecklist = GetTimeStamp()
    end
end

local function doneItem(label, texture)
    texture:SetTexture("esoui/art/loot/loot_finesseItem.dds")
    texture:SetColor(0,1,0,1)
end

local function startItem(label, texture)
    texture:SetTexture("esoui/art/loot/loot_finesseItem.dds")
    texture:SetColor(1,1,0,1)
end

local function stoppedItem(label, texture)
    texture:SetTexture("esoui/art/buttons/decline_up.dds")
    texture:SetColor(1,0,0,1)
end

local function incrementDone()
    checklistDone = checklistDone + 1
    if checklistDone == 8 then
        LeoAltholicChecklistPanel:SetHidden(true)
        LeoAltholicChecklistMinButton:SetHidden(true)
        LeoAltholicChecklistMaxButton:SetHidden(false)
    end
end

function LeoAltholicChecklistUI:doneWrit(craft)
    if not LeoAltholic.charData.settings.checklist.enabled then return end

    local label = GetControl(panel, "Craft"..craft)
    local texture = GetControl(panel, "Craft"..craft.."Status")
    doneItem(label, texture)
    incrementDone()
end

function LeoAltholicChecklistUI:startedWrit(craft)
    if not LeoAltholic.charData.settings.checklist.enabled then return end

    local label = GetControl(panel, "Craft"..craft)
    local texture = GetControl(panel, "Craft"..craft.."Status")
    startItem(label, texture)
end

function LeoAltholicChecklistUI:stoppedWrit(craft)
    if not LeoAltholic.charData.settings.checklist.enabled then return end

    local label = GetControl(panel, "Craft"..craft)
    local texture = GetControl(panel, "Craft"..craft.."Status")
    stoppedItem(label, texture)
end

function LeoAltholicChecklistUI:doneRiding()
    if not LeoAltholic.charData.settings.checklist.enabled then return end

    local label = GetControl(panel, "Riding")
    local texture = GetControl(panel, "RidingStatus")
    doneItem(label, texture)
    incrementDone()
end

function LeoAltholicChecklistUI:update()

    if not LeoAltholic.charData.settings.checklist.enabled then return end

    local char = LeoAltholic.GetMyself()

    local texture, label
    local done = false

    height = 0
    anchorY = 44

    for _, craft in pairs(LeoAltholic.allCrafts) do
        label = GetControl(panel, "Craft"..craft)
        texture = GetControl(panel, "Craft"..craft.."Status")
        createItem(label, GetCraftingSkillName(craft), texture, LeoAltholic.charData.settings.checklist.craft[craft])
        if LeoAltholic.IsWritDoneToday(craft) then
            doneItem(label, texture)
            done = true
            incrementDone()
        elseif LeoAltholic.IsWritStartedToday(craft) then
            startItem(label, texture)
        end
    end

    label = GetControl(panel, "Riding")
    texture = GetControl(panel, "RidingStatus")
    createItem(label, GetString(SI_STAT_GAMEPAD_RIDING_HEADER_TRAINING), texture, LeoAltholic.charData.settings.checklist.riding)
    if char.attributes.riding.time - GetTimeStamp() > 0 then
        doneItem(label, texture)
        incrementDone()
    end

    panel:SetHeight(10 + height)
    checklist:SetHeight(60 + height)
end