local SousChef = SousChef
local u = SousChef.Utility
local m = SousChef.Media

SousChef.ProvisioningButton=
{{
    name = "Mark Recipe", -- or function that returns a name
    keybind = "SC_MARK_RECIPE",
    control = self,
    callback = function(descriptor) SousChef:MarkRecipe() end,
    visible = function(descriptor) return PROVISIONER:GetSelectedRecipeIndex() ~= nil end,
    alignment = KEYBIND_STRIP_ALIGN_RIGHT,
}}

local bGroup = SousChef.ProvisioningButton

function SousChef:HookRecipeTreeFunction()
    if not KEYBIND_STRIP:HasKeybindButtonGroup(bGroup) then
        KEYBIND_STRIP:AddKeybindButtonGroup(bGroup)
    else
        KEYBIND_STRIP:UpdateKeybindButtonGroup(bGroup)
    end

    -- Hook the provisioning panel
    if not SousChef.hookedProvisioningFunction then
        local ref = PROVISIONER.recipeTree.templateInfo.ZO_ProvisionerNavigationEntry
        if ref then
            SousChef.hookedProvisioningFunction = ref.setupFunction
            ref.setupFunction =
            function(...)
                local node, control, data, open, userRequested, enabled = ...
                SousChef.hookedProvisioningFunction(...)
                local link = GetRecipeResultItemLink(data.recipeListIndex, data.recipeIndex, LINK_STYLE_BRACKETS)
                if link then
                    SousChef:AddNotifier(control, SousChef.settings.shoppingList[zo_strformat(SI_TOOLTIP_ITEM_NAME, link)])
                end
            end
        end
    end
    PROVISIONER:DirtyRecipeTree()
end

function SousChef:getMarkedIcon(row)
    local rankIcon = SousChef.slotLines[row:GetName()]
    if(not rankIcon) then
        rankIcon =  WINDOW_MANAGER:CreateControl(row:GetName() .. "SousChef", row, CT_TEXTURE)
        SousChef.slotLines[row:GetName()] = rankIcon
        rankIcon:SetTexture(m.CANLEARN)
        rankIcon:SetDimensions(20, 20)
        rankIcon:SetAnchor(RIGHT, row, LEFT, -10)
        rankIcon:SetMouseEnabled(true)
        rankIcon:SetHandler("OnMouseEnter", function()
                                                InitializeTooltip(InformationTooltip, rankIcon, RIGHT, -15, 0)
                                                InformationTooltip:AddLine("Shopping List")
                                            end)
        rankIcon:SetHandler("OnMouseExit", function()
                                                ClearTooltip(InformationTooltip)
                                            end)
    end
    return rankIcon
end

function SousChef:AddNotifier(control, marked)
    local icon = self:getMarkedIcon(control)
    if marked then
        icon:SetHidden(false)
    else
        icon:SetHidden(true)
    end
end

function SousChef:IsOnShoppingList(id)
    for i,v in ipairs(SousChef.settings.ReverseCookbook[id]) do
        if SousChef.settings.shoppingList[v] then return true end
    end
    return false
end

function SousChef:MarkRecipe()
    local link = GetRecipeResultItemLink(PROVISIONER:GetSelectedRecipeListIndex(), PROVISIONER:GetSelectedRecipeIndex(), LINK_STYLE_BRACKETS)
    SousChef.settings.shoppingList[zo_strformat(SI_TOOLTIP_ITEM_NAME, link)] = not SousChef.settings.shoppingList[zo_strformat(SI_TOOLTIP_ITEM_NAME, link)]
    PROVISIONER:DirtyRecipeTree()
end

function SousChef:HookRecipeTree(...)
    local eventId, craftingTable = ...
    if craftingTable ~= CRAFTING_TYPE_PROVISIONING then return end
    zo_callLater(function() SousChef:HookRecipeTreeFunction() end, 1000)
end

function SousChef:UnhookRecipeTree()
    if KEYBIND_STRIP:HasKeybindButtonGroup(bGroup) then
        KEYBIND_STRIP:RemoveKeybindButtonGroup(bGroup)
    end
end