local addonName = "Altoholic"
local addon = _G[addonName]
local L = LibStub("AceLocale-3.0"):GetLocale(addonName)

addon:Controller("AltoholicUI.RecipeRow", {
	Update = function(frame, itemID, color)
		
		-- ** set the crafted item **
		-- local craftedItemID, maxMade = DataStore:GetCraftResultItem(itemID)
		local maxMade = 1
		local itemName, itemLink, itemRarity
		
		if itemID then
			frame.CraftedItem:SetIcon(GetItemIcon(itemID))
			frame.CraftedItem.itemID = itemID
			
			itemName, itemLink, itemRarity = GetItemInfo(itemID)
			
			frame.CraftedItem.Icon:SetVertexColor(1, 1, 1)
			if itemRarity then
				frame.CraftedItem:SetRarity(itemRarity)
			end
			
			if maxMade > 1 then
				frame.CraftedItem.Count:SetText(maxMade)
				frame.CraftedItem.Count:Show()
			else
				frame.CraftedItem.Count:Hide()
			end
			frame.CraftedItem:Show()
		else
			frame.CraftedItem:Hide()
		end
		
		-- ** set the recipe text **
		local recipeText
		
		if itemName then
			local _, _, _, hexColor = GetItemQualityColor(itemRarity)
			recipeText = format("|c%s%s", hexColor, itemName)
		end
	
		frame.RecipeLink.Text:SetText(recipeText)
		
		-- ** set the reagents **
		local reagents = DataStore:GetCraftReagents(itemID)		-- reagents = "2996,2|2318,1|2320,1"
		local index = 1
		
		if reagents then
			for reagent in reagents:gmatch("([^|]+)") do
				local reagentIcon = frame["Reagent" .. index]
				local reagentID, reagentCount = strsplit(",", reagent)
				reagentID = tonumber(reagentID)
				
				if reagentID then
					reagentCount = tonumber(reagentCount)
					
					reagentIcon.itemID = reagentID
					reagentIcon:SetIcon(GetItemIcon(reagentID))
					reagentIcon.Count:SetText(reagentCount)
					reagentIcon.Count:Show()
				
					reagentIcon:Show()
					index = index + 1
				else
					reagentIcon:Hide()
				end				
			end
		end
		
		-- hide unused reagent icons
		while index <= 8 do
			frame["Reagent" .. index]:Hide()
			index = index + 1
		end

		frame:Show()
	end,
})
