---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by Robotex140.
--- DateTime: 14-Mar-22 15:22
---

HCOntest_references = {
    ['HCNearCarpybench'] = 'Requires Nearby Built Carpentry Bench',
    ['HCNearHerbatable'] = 'Requires Nearby Built Herbalist table',
    ['HCNearTarkiln'] = 'Requires Nearby Built Tar Kiln',
    ['HCNearKiln'] = 'Requires Nearby Built Kiln',
    ['HCNearGrindstone'] = 'Requires Nearby Built Grindstone'}


HCOntest_furni_references = {
    ['HCNearCarpybench'] = 'Hydrocraft.HCCarpenterbench',
    ['HCNearHerbatable'] = 'Hydrocraft.HCHerbtable',
    ['HCNearTarkiln'] = 'Hydrocraft.HCTarkiln',
    ['HCNearKiln'] = 'Hydrocraft.HCKiln',
    ['HCNearGrindstone'] = 'Hydrocraft.HCGrindstone'}

if getActivatedMods():contains("CraftHelper41") then
    require "UI/craftHelper41RecipePanel"
    local old_set_recipe = craftHelper41RecipePanel.setRecipe;
    function craftHelper41RecipePanel:setRecipe(recipe)
        -- Do all of original set recipe stuff, then add in the extra HC stuff 
        old_set_recipe(self, recipe)
        newItem = self.newItem
        local lua_test = recipe:getLuaTest()
        if lua_test ~= nil and HCOntest_references[lua_test] ~= nil then
    
            local sourceInList = {};
            sourceInList.items = {}
            local itemInList = {};
            itemInList.count = 1;
            local furniitem = InventoryItemFactory.CreateItem(HCOntest_furni_references[lua_test]);
            itemInList.texture = furniitem:getTex();
            itemInList.name = HCOntest_references[lua_test];
            table.insert(sourceInList.items, itemInList);
            table.insert(newItem.sources, sourceInList);
        end
        self.recipe = recipe;
        self.newItem = newItem;
        self:refreshIngredientPanel();
    end
end

require "ISUI/ISCraftingUI"

-- unfortunately there is no way to cleanly inject the stuff we need without rewriting the entirety of the function
-- this will probably cause problems the next time is indiestore modifies this code
-- for future, all changes changes made are in lines 168- 180 of this file. Remember to update this, if changes are made

function ISCraftingUI:populateRecipesList()
    local allRecipes = getAllRecipes();
    self.allRecipesList = {};
    self.recipesList = {};
    self.recipesListH = {};
    self.recipesList[getText("IGUI_CraftCategory_Favorite")] = {}; -- set these 2 to have a good order
    self.recipesListH[#self.recipesListH+1] = getText("IGUI_CraftCategory_Favorite")
    self.recipesList[getText("IGUI_CraftCategory_General")] = {};
    self.recipesListH[#self.recipesListH+1] = getText("IGUI_CraftCategory_General")
    self:getContainers();

    for i=0,allRecipes:size()-1 do
        local newItem = {};
        local recipe = allRecipes:get(i);
        if not recipe:isHidden() and (not recipe:needToBeLearn() or (self.character and self.character:isRecipeKnown(recipe))) then
            if recipe:getCategory() then
                newItem.category = recipe:getCategory();
            else
                newItem.category = getText("IGUI_CraftCategory_General");
            end
            if not self.recipesList[newItem.category] then
                self.recipesList[newItem.category] = {};
                self.recipesListH[#self.recipesListH+1] = newItem.category
            end
            newItem.recipe = recipe;
            if self.character then
                newItem.available = RecipeManager.IsRecipeValid(recipe, self.character, nil, self.containerList);

                local modData = self.character:getModData();
                if modData[self:getFavoriteModDataLocalString(recipe)] or false then  -- Update the favorite list and save backward compatibility
                    --table.remove(modData, self:getFavoriteModDataLocalString(recipe));
                    modData[self:getFavoriteModDataString(recipe)] = true;
                end
                newItem.favorite = modData[self:getFavoriteModDataString(recipe)] or false;
            end
            if newItem.favorite then
                table.insert(self.recipesList[getText("IGUI_CraftCategory_Favorite")], newItem);
            end
            local resultItem = self:GetItemInstance(recipe:getResult():getFullType());
            if resultItem then
                newItem.texture = resultItem:getTex();
                newItem.itemName = resultItem:getDisplayName();
                if recipe:getResult():getCount() > 1 then
                    newItem.itemName = (recipe:getResult():getCount() * resultItem:getCount()) .. " " .. newItem.itemName;
                end
            end
            newItem.sources = {};
            for x=0,recipe:getSource():size()-1 do
                local source = recipe:getSource():get(x);
                local sourceInList = {};
                sourceInList.items = {}
                for k=1,source:getItems():size() do
                    local sourceFullType = source:getItems():get(k-1)
                    local item = nil
                    local itemName = nil
                    if sourceFullType == "Water" then
                        item = self:GetItemInstance("Base.WaterDrop");
                    elseif luautils.stringStarts(sourceFullType, "[") then
                        -- a Lua test function
                        item = self:GetItemInstance("Base.WristWatch_Right_DigitalBlack");
                    else
                        item = self:GetItemInstance(sourceFullType);
                    end
                    if item then
                        local itemInList = {};
                        itemInList.count = source:getCount();
                        itemInList.texture = item:getTex();
                        if sourceFullType == "Water" then
                            if itemInList.count == 1 then
                                itemInList.name = getText("IGUI_CraftUI_CountOneUnit", getText("ContextMenu_WaterName"))
                            else
                                itemInList.name = getText("IGUI_CraftUI_CountUnits", getText("ContextMenu_WaterName"), itemInList.count)
                            end
                            if recipe:getHeat() < 0 then
                                itemInList.name = getText("IGUI_FoodTemperatureNaming", getText("IGUI_Temp_Hot"), itemInList.name);
                            elseif recipe:getHeat() > 0 then
                                itemInList.name = getText("IGUI_FoodTemperatureNaming", getText("IGUI_Temp_Cold"), itemInList.name);
                            end;
                        elseif source:getItems():size() > 1 then -- no units
                            itemInList.name = item:getDisplayName()
                        elseif not source:isDestroy() and item:IsDrainable() then
                            if itemInList.count == 1 then
                                itemInList.name = getText("IGUI_CraftUI_CountOneUnit", item:getDisplayName())
                            else
                                itemInList.name = getText("IGUI_CraftUI_CountUnits", item:getDisplayName(), itemInList.count)
                            end
                            if recipe:getHeat() < 0 then
                                itemInList.name = getText("IGUI_FoodTemperatureNaming", getText("IGUI_Temp_Hot"), itemInList.name);
                            elseif recipe:getHeat() > 0 then
                                itemInList.name = getText("IGUI_FoodTemperatureNaming", getText("IGUI_Temp_Cold"), itemInList.name);
                            end;
                        elseif not source:isDestroy() and source:getUse() > 0 then -- food
                            itemInList.count = source:getUse()
                            if itemInList.count == 1 then
                                itemInList.name = getText("IGUI_CraftUI_CountOneUnit", item:getDisplayName())
                            else
                                itemInList.name = getText("IGUI_CraftUI_CountUnits", item:getDisplayName(), itemInList.count)
                            end
                        elseif itemInList.count > 1 then
                            itemInList.name = getText("IGUI_CraftUI_CountNumber", item:getDisplayName(), itemInList.count)
                        else
                            itemInList.name = item:getDisplayName()
                        end
                        itemInList.fullType = item:getFullType()
                        if sourceFullType == "Water" then
                            itemInList.fullType = "Water"
                        end
                        table.insert(sourceInList.items, itemInList);
                    end
                end
                table.insert(newItem.sources, sourceInList)
            end

            local lua_test = recipe:getLuaTest()
            if lua_test ~= nil and HCOntest_references[lua_test] ~= nil then

                local sourceInList = {};
                sourceInList.items = {}
                local itemInList = {};
                itemInList.count = 1;
                local furniitem = InventoryItemFactory.CreateItem(HCOntest_furni_references[lua_test]);
                itemInList.texture = furniitem:getTex();
                itemInList.name = HCOntest_references[lua_test];
                table.insert(sourceInList.items, itemInList);
                table.insert(newItem.sources, sourceInList);
            end       
            
            table.insert(self.recipesList[newItem.category], newItem);
            table.insert(self.allRecipesList, newItem);
        end
    end

    -- now do the evolved recipe
    local newRecipe = {};
    local itemInList = {};
    local doneRecipes = {};
    local doneItems = {};
    -- first we get all our available evolvedRecipe
    for i=0,self.containerList:size()-1 do
        local container = self.containerList:get(i);
        for x=0,container:getItems():size() - 1 do
            local baseItem = container:getItems():get(x);
            local evorecipe = RecipeManager.getEvolvedRecipe(baseItem, self.character, self.containerList, false);
            if evorecipe and evorecipe:size() > 0 then
                for y=0,evorecipe:size() - 1 do
                    local evo = evorecipe:get(y);
                    --if recipe is hidden skip it - unless we already have a result item
                    if (not evo:isHidden() or baseItem:getType() ~= evo:getBaseItem()) then
                        newRecipe = {};
                        if not doneRecipes[evo:getName() .. baseItem:getFullType()] then
                            doneRecipes[evo:getName() .. baseItem:getFullType()] = true;
                            doneItems = {};
                            newRecipe.baseItem = baseItem;
                            local resultItem = self:GetItemInstance(evo:getFullResultItem());
                            newRecipe.texture = resultItem:getTex();
                            newRecipe.resultName = resultItem:getDisplayName();
                            newRecipe.items = {};
                            newRecipe.available = false;
                            newRecipe.itemName = evo:getName();
                            if baseItem:getType() ~= evo:getBaseItem() then
                                newRecipe.customRecipeName = getText("IGUI_CraftUI_FromBaseItem", baseItem:getDisplayName());
                                -- add the textures of our extra items to display them
                                newRecipe.extraItems = {};
                                if baseItem:getExtraItems() then
                                    for u=0,baseItem:getExtraItems():size()-1 do
                                        local extraItem = self:GetItemInstance(baseItem:getExtraItems():get(u));
                                        if extraItem then
                                            table.insert(newRecipe.extraItems, extraItem:getTex());
                                        end
                                    end
                                end
                                if instanceof(baseItem, "Food") and baseItem:getSpices() then
                                    for u=0,baseItem:getSpices():size()-1 do
                                        local extraItem = self:GetItemInstance(baseItem:getSpices():get(u));
                                        if extraItem then
                                            table.insert(newRecipe.extraItems, extraItem:getTex());
                                        end
                                    end
                                end
                            end
                            newRecipe.recipe = evo;
                            newRecipe.evolved = true;
                            local itemCanBeUse = evo:getItemsCanBeUse(self.character, baseItem, self.containerList);
                            for l=0, itemCanBeUse:size()-1 do
                                local newItem = itemCanBeUse:get(l);
                                if not doneItems[newItem] then
                                    doneItems[newItem] = true;
                                    newRecipe.available = true;
                                    itemInList.texture = newItem:getTex();
                                    itemInList.name = newItem:getName();
                                    itemInList.fullType = newItem:getFullType();
                                    itemInList.itemToAdd = newItem;
                                    itemInList.available = true;
                                    itemInList.poison = self.character:isKnownPoison(newItem)
                                    table.insert(newRecipe.items, itemInList);
                                    itemInList = {};
                                end
                            end
                            if self.character then
                                local modData = self.character:getModData();
                                newRecipe.favorite = modData[self:getFavoriteModDataString(evo)] or false;
                            end
                            table.insert(self.recipesList["Cooking"], newRecipe);
                            table.insert(self.allRecipesList, newRecipe);
                            if newRecipe.favorite then
                                table.insert(self.recipesList[getText("IGUI_CraftCategory_Favorite")], newRecipe);
                            end
                        end
                    end
                end
            end
        end
    end

    -- then we look for missing recipes
    local allRecipes = RecipeManager.getAllEvolvedRecipes();
    for i=0, allRecipes:size()-1 do
        local evolvedRecipe = allRecipes:get(i);
        local found = false;
        if not evolvedRecipe:isHidden() then
            for x,v in ipairs(self.recipesList["Cooking"]) do
                if v.evolved and v.recipe == evolvedRecipe then -- check possible missing items
                    local possibleItems = evolvedRecipe:getPossibleItems();
                    for k=0, possibleItems:size() -1 do
                        local possibleItem = possibleItems:get(k);
                        local found2 = false;
                        for g,h in ipairs(v.items) do
                            if h.fullType == possibleItem:getFullType() then
                                found2 = true;
                                break;
                            end
                        end
                        if not found2 then
                            local newItem = self:GetItemInstance(possibleItem:getFullType());
                            itemInList.texture = newItem:getTex();
                            itemInList.name = newItem:getDisplayName();
                            itemInList.available = false;
                            table.insert(v.items, itemInList);
                            itemInList = {};
                        end
                    end
                    found = true;
                end
            end
            if not found then -- recipe not in list, we add it with all the missing items
                newRecipe = {};
                local resultItem = self:GetItemInstance(evolvedRecipe:getFullResultItem());
                if resultItem then
                    newRecipe.texture = resultItem:getTex();
                    newRecipe.resultName = resultItem:getDisplayName();
                    newRecipe.items = {};
                    newRecipe.available = false;
                    newRecipe.itemName = evolvedRecipe:getName();
                    newRecipe.recipe = evolvedRecipe;
                    newRecipe.evolved = true;
                    newRecipe.baseItem = self:GetItemInstance(evolvedRecipe:getModule():getName() .. "." .. evolvedRecipe:getBaseItem());
                    local possibleItems = evolvedRecipe:getPossibleItems();
                    for k=0, possibleItems:size() -1 do
                        local possibleItem = possibleItems:get(k);
                        local newItem = self:GetItemInstance(possibleItem:getFullType());
                        itemInList.texture = newItem:getTex();
                        itemInList.name = newItem:getDisplayName();
                        itemInList.available = false;
                        table.insert(newRecipe.items, itemInList);
                        itemInList = {};
                    end
                    if self.character then
                        local modData = self.character:getModData();
                        newRecipe.favorite = modData[self:getFavoriteModDataString(evolvedRecipe)] or false;
                    end
                    table.insert(self.recipesList["Cooking"], newRecipe);
                    if newRecipe.favorite then
                        table.insert(self.recipesList[getText("IGUI_CraftCategory_Favorite")], newRecipe);
                    end
                else
                    print('ISCraftingUI: no such result item '..tostring(evolvedRecipe:getFullResultItem()))
                end
            end
        end
    end
    --    if #self.recipesList["Favorite"] == 0 then self.recipesList["Favorite"] = nil; end
end