CannibalismParamEditor = CannibalismParamEditor or {}
local CannibalismUtils = {}

local function initializeReqs()
    CannibalismUtils = require("Cannibalism_Utils")
end

Events.OnInitGlobalModData.Add(initializeReqs)

local function getCandyItems()
    local items = SandboxVars.Cannibalism.CandyItems or ""
    local itemsToEdit = {}
    for item in string.gmatch(items, '([^;]+)') do
        table.insert(itemsToEdit, item)
    end
    return itemsToEdit
end

function CannibalismParamEditor.reducePoints(item, character, amount)
    if amount == 1 then
        local vars = SandboxVars.Cannibalism
        local currentPoints = CannibalismUtils.getPlayerInsanityPoints(character)

        local newPoints = currentPoints - vars.PointsDecrementCandy
        if newPoints < 0 then newPoints = 0 end
        character:getModData().Cannibalism_InsanityPoints = newPoints

        local insanityStage = CannibalismUtils.getPlayerInsanityStage(character)
        if MF then
            MF.getMoodle("insane", character:getPlayerNum()):setValue(insanityStage) --float
        end
    end
end

local function EditParams()
    local scriptManager = getScriptManager()
    local itemsToEdit = getCandyItems()

    if itemsToEdit then
        for _, item in ipairs(itemsToEdit) do
            scriptManager:getItem(item):DoParam("OnEat = CannibalismParamEditor.reducePoints")
        end
    end

    local isZombieMeatPoisonous = SandboxVars.Cannibalism.PoisonousZombieMeat
    local zombieMeat = scriptManager:getItem("ZombieMeat")

    if zombieMeat then
        if not isZombieMeatPoisonous then
            zombieMeat:DoParam("Poison = false")
            zombieMeat:DoParam("Tooltip = Tooltip_item_ZombieMeatNoPoison")
            zombieMeat:DoParam("PoisonPower = 0")
        else
            local poisonPower = SandboxVars.Cannibalism.ZombieMeatPoisonPower
            zombieMeat:DoParam("Poison = true")
            zombieMeat:DoParam("Tooltip = Tooltip_item_ZombieMeatPoison")
            zombieMeat:DoParam("PoisonPower = " .. tostring(poisonPower))
        end
    end
end
Events.OnInitGlobalModData.Add(EditParams)

--B42 stuff
local function EditBoneRecipes()
    -- Define a table of recipes and their scripts
    local recipeList = {
        ["MakeToothNecklace"] =
        "{ item 1 tags[base:drillwood;base:drillmetal;base:drillwoodpoor] mode:keep flags[MayDegradeLight;Prop1],item 2 [Base.HerbivoreTeeth;Base.HumanTeeth] flags[Prop2],item 1 [Base.Twine], }",
        ["MakeLongToothNecklace"] =
        "{ item 1 tags[base:drillwood;base:drillmetal;base:drillwoodpoor] mode:keep flags[MayDegradeLight;Prop1], item 3 [Base.HerbivoreTeeth;Base.HumanTeeth] flags[Prop2], item 2 [Base.Twine], }",
        ["MakeBoneClub"] =
        "{ item 1 [Base.AnimalBone;Base.LargeAnimalBone;Base.HumanBone;Base.LargeHumanBone]  mappers[ClubMapper] flags[Prop2;InheritCondition],item 2 [Base.LeatherStrips], }",
        ["SharpenBone"] =
        "{ item 1 tags[base:whetstone;base:file] mode:keep flags[MayDegradeLight],item 1 tags[base:saw;base:sharpknife;base:meatcleaver] mode:keep flags[MayDegradeLight],item 1 [Base.AnimalBone;Base.BoneBead_Large;Base.HatchetHead_Bone;Base.LargeAnimalBone;Base.SharpBone_Long;Base.SmallAnimalBone;Base.HumanBone;Base.LargeHumanBone] mappers[BoneMapper] flags[Prop2;AllowDestroyedItem], }",
        ["SmashBone"] =
        "{ item 1 tags[base:hammer;base:sledgehammer;base:clubhammer;base:hammerstone] mode:keep flags[Prop1;MayDegradeLight],item 1 [Base.AnimalBone;Base.LargeAnimalBone;Base.HumanBone;Base.LargeHumanBone]  flags[Prop2;AllowDestroyedItem], }",
        ["MakeLargeBoneBeads"] =
        "{ item 1 tags[base:sharpknife;base:meatcleaver] mode:keep flags[MayDegradeLight;IsNotDull],item 1 tags[base:whetstone;base:file] mode:keep flags[MayDegradeLight],item 1 [Base.AnimalBone;Base.LargeAnimalBone;Base.JawboneBovide;Base.HumanBone;Base.LargeHumanBone] flags[AllowDestroyedItem],item 1 tags[base:drillwood;base:drillmetal;base:drillwoodpoor] mode:keep flags[MayDegradeLight], }"
    }

    for recipeName, script in pairs(recipeList) do
        local recipe = getScriptManager():getCraftRecipe(recipeName)

        recipe:getInputs():clear()

        local wrappedScript = "{ inputs" .. script .. " }"

        recipe:Load(recipeName, wrappedScript)
    end
end

Events.OnInitWorld.Add(EditBoneRecipes)
if isServer() then Events.OnGameBoot.Add(EditBoneRecipes) end

--[[
local toothNecklaceRecipe = getScriptManager():getCraftRecipe("MakeToothNecklace")

local toothNecklaceInputs = toothNecklaceRecipe:getInputs()
for i = 0, toothNecklaceInputs:size() - 1 do
    local input = toothNecklaceInputs:get(i)

    -- Use getOriginalLine() to check the original input string
    print(input.originalLine)
end

]]
