--Cannibalism = Cannibalism or {}
local CannibalismUtils = require("Cannibalism_Utils")

require "MF_ISMoodle"
MF.createMoodle("insane");

local function onChopCorpse(playerNum, corpse, knife)
    local character = getSpecificPlayer(playerNum)
    if luautils.walkAdj(character, corpse:getSquare()) then
        ISInventoryPaneContextMenu.transferIfNeeded(character, knife)
        ISTimedActionQueue.add(ISEquipWeaponAction:new(character, knife, 10, true, true))
        ISTimedActionQueue.add(Cannibalism_ChopCorpse:new(character, corpse, knife))
    end
end

local function onFillWorldObjectContextMenu(playerNum, context, worldObjects, test)
    local fetch = ISWorldObjectContextMenu.fetchVars
    local body = fetch.body
    if body and not body:isAnimal() then
        local knife = CannibalismUtils.getKnife(playerNum)
        local option = context:addOption(getText("ContextMenu_ChopUpCorpse"), playerNum, onChopCorpse, body,
            knife)
        local description = getText("ContextMenu_ChopUpCorpse_Tooltip")
        if not knife then
            option.notAvailable = true
            description = "<RED>" .. getText("ContextMenu_ChopUpCorpse_ToolRequired")
        end
        local tooltip = ISToolTip:new();
        tooltip:initialise();
        tooltip:setVisible(false);
        tooltip.description = description
        option.toolTip = tooltip
    end
end
Events.OnFillWorldObjectContextMenu.Add(onFillWorldObjectContextMenu)

local function onCreatePlayer()
    local character = getPlayer()
    local insanityStage = CannibalismUtils.getPlayerInsanityStage(character)
    MF.getMoodle("insane", character:getPlayerNum()):setValue(insanityStage) --float
end
Events.OnCreatePlayer.Add(onCreatePlayer)

local function everyOneMinute()
    local character = getPlayer()
    local insanityStage = CannibalismUtils.getPlayerInsanityStage(character)
    if insanityStage == 0.5 then return end

    local sandboxVars = SandboxVars.Cannibalism
    local presetVars = CannibalismUtils.getInsanitySettings()
    local emitter = character:getEmitter()

    CannibalismUtils.lastSoundAttemptTimestamp = CannibalismUtils.lastSoundAttemptTimestamp or getTimestamp()

    if insanityStage == 0.4 then --Stage one
        if (getTimestamp() - CannibalismUtils.lastSoundAttemptTimestamp) / 60 >= presetVars.StageOneSoundFrequency then
            local rng = ZombRand(0, 101)
            if rng <= presetVars.StageOneSoundChance then
                local sound = CannibalismUtils.getRandomSound(sandboxVars.StageOneSounds)
                if sound then
                    emitter:playSoundImpl(sound, nil)
                end
                CannibalismUtils.lastSoundAttemptTimestamp = getTimestamp()
            end
        end
    elseif insanityStage == 0.3 then --Stage two
        if (getTimestamp() - CannibalismUtils.lastSoundAttemptTimestamp) / 60 >= presetVars.StageTwoSoundFrequency then
            local rng = ZombRand(0, 101)
            if rng <= presetVars.StageTwoSoundChance then
                local sound = CannibalismUtils.getRandomSound(sandboxVars.StageTwoSounds)
                if sound then
                    emitter:playSoundImpl(sound, nil)
                end
                CannibalismUtils.lastSoundAttemptTimestamp = getTimestamp()
            end
        end
    elseif insanityStage == 0.2 then --Stage three
        if (getTimestamp() - CannibalismUtils.lastSoundAttemptTimestamp) / 60 >= presetVars.StageThreeSoundFrequency then
            local rng = ZombRand(0, 101)
            if rng <= presetVars.StageThreeSoundChance then
                local sound = CannibalismUtils.getRandomSound(sandboxVars.StageThreeSounds)
                if sound then
                    emitter:playSoundImpl(sound, nil)
                end
                CannibalismUtils.lastSoundAttemptTimestamp = getTimestamp()
            end
        end
    end
end
Events.EveryOneMinute.Add(everyOneMinute)

local function everyDays()
    local character = getPlayer()
    local hasCannibalisedToday = character:getModData().Cannibalism_hasCannibalisedToday or false
    local sandboxVars = SandboxVars.Cannibalism
    if not hasCannibalisedToday then
        local currentPoints = CannibalismUtils.getPlayerInsanityPoints(character)
        if currentPoints > 0 then
            local newPoints = currentPoints - sandboxVars.PointsDecrementDays
            if newPoints < 0 then newPoints = 0 end

            character:getModData().Cannibalism_InsanityPoints = newPoints

            local insanityStage = CannibalismUtils.getPlayerInsanityStage(character)
            MF.getMoodle("insane", character:getPlayerNum()):setValue(insanityStage) --float
        end
    end
    character:getModData().Cannibalism_hasCannibalisedToday = false
end
Events.EveryDays.Add(everyDays)

--[[
local CannibalismUtils = require("Cannibalism_Utils")
local character = getPlayer()
local stage = CannibalismUtils.getPlayerInsanityStage(character)
local currentInsanityPoints = CannibalismUtils.getPlayerInsanityPoints(character)
local presetVars = CannibalismUtils.getInsanitySettings()
local stageOnePoints = presetVars.StageOneInsanityPoints
print(currentInsanityPoints)
print(stageOnePoints)
print(currentInsanityPoints < stageOnePoints)
]]
