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

local BACKPACK = ZO_PlayerInventoryBackpack
local BANK = ZO_PlayerBankBackpack
local GUILD_BANK = ZO_GuildBankBackpack

local COOKING_RANK_2 = [[SousChef\media\Two.dds]]
local COOKING_RANK_3 = [[SousChef\media\Three.dds]]
local COOKING_RANK_4 = [[SousChef\media\Four.dds]]
local COOKING_RANK_5 = [[SousChef\media\Five.dds]]
local COOKING_RANK_6 = [[SousChef\media\Six.dds]]
local COOKING_FLAVOUR = [[SousChef\media\Flavour.dds]]
local COOKING_SPICE =   [[SousChef\media\Spice.dds]]
local COOKING_SPICEB =   [[SousChef\media\Spice_Flat.dds]]
local COOKING_RANK_1B = [[SousChef\media\One_flat.dds]]
local COOKING_RANK_2B = [[SousChef\media\Two_flat.dds]]
local COOKING_RANK_3B = [[SousChef\media\Three_flat.dds]]
local COOKING_RANK_4B = [[SousChef\media\Four_flat.dds]]
local COOKING_RANK_5B = [[SousChef\media\Five_flat.dds]]
local COOKING_RANK_6B = [[SousChef\media\Six_flat.dds]]
local COOKING_FLAVOURB = [[SousChef\media\flavour_flat.dds]]
local COOKING_RANK_1 = [[SousChef\media\One.dds]]

m.COOKING = { COOKING_RANK_1, COOKING_RANK_2, COOKING_RANK_3, COOKING_RANK_4, COOKING_RANK_5, COOKING_RANK_6, COOKING_FLAVOUR, COOKING_SPICE }
m.COOKINGB = { COOKING_RANK_1B, COOKING_RANK_2B, COOKING_RANK_3B, COOKING_RANK_4B, COOKING_RANK_5B, COOKING_RANK_6B, COOKING_FLAVOURB, COOKING_SPICEB }

m.CANLEARN = [[/esoui/art/loot/loot_finesseitem.dds]]

local rowClicked = {}

function SousChef.HookInventory()
	for _,v in pairs(PLAYER_INVENTORY.inventories) do
		local listView = v.listView
		if listView and listView.dataTypes and listView.dataTypes[1] then
            SousChef.hookedFunctions = listView.dataTypes[1].setupCallback

			listView.dataTypes[1].setupCallback =
				function(rowControl, slot)
                    SousChef.hookedFunctions(rowControl, slot)
					SousChef.AddRankToSlot(rowControl)
				end
		end
    end

	ZO_ScrollList_RefreshVisible(BACKPACK)
	ZO_ScrollList_RefreshVisible(BANK)
	ZO_ScrollList_RefreshVisible(GUILD_BANK)
end

function SousChef.getIcon(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
		ZO_PreHookHandler(row, "OnMouseDown", SousChef.AddDetails)
		ZO_PreHookHandler(row, "OnMouseExit", function(self) rowClicked[self] = nil return false end )
	end
	return rankIcon
end

function SousChef.AddDetails(row)
	if not row.dataEntry or not row.dataEntry.data or rowClicked[row] then return false end
	local rowInfo = row.dataEntry.data
	local bagId = rowInfo.bagId
	local slotIndex = rowInfo.slotIndex

	if u.MatchesRecipe(rowInfo.name) then
        local gmatch = u.MatchInGlobalCookbook(rowInfo.name)
        if gmatch then
            ItemTooltip:AddLine("")
            ItemTooltip:AddLine("Known by ", "ZoFontWinH5", 1,1,1, BOTTOM, MODIFY_TEXT_TYPE_UPPERCASE)
            ItemTooltip:AddLine(u.TableKeyConcat(gmatch))
            rowClicked[row] = true
            return
        end
    end

	if ((GetItemCraftingInfo(bagId, slotIndex)) ~= CRAFTING_TYPE_PROVISIONING) then	return false end

	local usableIngredient = SousChef.ReverseCookbook[u.GetItemID(GetItemLink(bagId, slotIndex))]
	if SousChef.settings.showAltKnowledge then usableIngredient = SousChef.settings.ReverseCookbook[u.GetItemID(GetItemLink(bagId, slotIndex))] end
	if usableIngredient then
		ItemTooltip:AddLine("Used in:", "ZoFontWinH5", 1,1,1, BOTTOM, MODIFY_TEXT_TYPE_UPPERCASE)
	    for i,v in ipairs(usableIngredient) do
	    	ItemTooltip:AddLine(v)
	    end
		rowClicked[row] = true
	end
	return false
end

function SousChef.AddRecipeToIgnoreList(link)
	if GetItemLinkInfo(link) ~= "" then link = string.match(link, "([ %a]+)%]") end
	SousChef.settings.ignoredRecipes[link] = true
	d("Adding " .. link .. " to ignored recipes")

end

function SousChef.RemoveRecipeFromIgnoreList(link)
	if GetItemLinkInfo(link) ~= "" then link = string.match(link, "([ %a]+)%]") end
	if not SousChef.settings.ignoredRecipes[link] then d(link .. " not found in ignore list") return end
	SousChef.settings.ignoredRecipes[link] = nil
	d("Removed " .. link .. " from ignored recipes")
end

function SousChef.ListIgnoredRecipes()
	d("Ignoring:")
	 for recipe in pairs(SousChef.settings.ignoredRecipes) do
        d(recipe)
    end
end