require "TimedActions/ISBaseTimedAction"
Cannibalism_ChopCorpse = ISBaseTimedAction:derive("Cannibalism_ChopCorpse")
local CannibalismUtils = require("Cannibalism_Utils")


local humanTextures =
{
    ["MaleBody01"] = true,
    ["MaleBody01a"] = true,
    ["MaleBody02"] = true,
    ["MaleBody02a"] = true,
    ["MaleBody03"] = true,
    ["MaleBody03a"] = true,
    ["MaleBody04"] = true,
    ["MaleBody04a"] = true,
    ["MaleBody05"] = true,
    ["MaleBody05a"] = true,
    ["FemaleBody01"] = true,
    ["FemaleBody02"] = true,
    ["FemaleBody03"] = true,
    ["FemaleBody04"] = true,
    ["FemaleBody05"] = true,
}
---Checks if a dead body is human
---@param body IsoDeadBody
---@return boolean
local function isBodyHuman(body)
    if not body then return false end
    local humanVisual = body:getHumanVisual()
    if not humanVisual then return false end

    local skinTexture = humanVisual:getSkinTexture()
    if not skinTexture then return false end

    if humanTextures[skinTexture] then return true else return false end
end

function Cannibalism_ChopCorpse:isValid() -- Check if the action can be done
    if not self.corpse then return false end
    if not self.corpse:getSquare() then return false end

    return true
end

function Cannibalism_ChopCorpse:update() -- Trigger every game update when the action is beind performed
    self.character:faceThisObject(self.corpse)
end

function Cannibalism_ChopCorpse:waitToStart() -- Wait until false is returned
    self.character:faceThisObject(self.corpse)
    return self.character:shouldBeTurning()
end

function Cannibalism_ChopCorpse:start() -- Trigger when the action starts
    self:setActionAnim("SawLog")
    self:setOverrideHandModels(self.character:getPrimaryHandItem(), nil)
    self.sound = self.character:getEmitter():playSound("Chopup")
end

function Cannibalism_ChopCorpse:stop() -- Trigger if the action is cancelled
    self.character:getEmitter():stopSound(self.sound)
    ISBaseTimedAction.stop(self)
end

function Cannibalism_ChopCorpse:perform() -- Trigger when the action is complete
    self.character:getEmitter():stopSound(self.sound)
    ISBaseTimedAction.perform(self)
end

function Cannibalism_ChopCorpse:complete()
    local vars = SandboxVars.Cannibalism
    self.corpse:getSquare():removeCorpse(self.corpse, false)
    local isHuman = isBodyHuman(self.corpse)

    if not isHuman then
        for i = 1, vars.ZombieMeatProduce do
            local meat = instanceItem("ZombieMeat")
            self.character:getInventory():AddItem(meat)
            sendAddItemToContainer(self.character:getInventory(), meat)
        end
    else
        for i = 1, vars.HumanMeatProduce do
            local meat = instanceItem("HumanMeat")
            self.character:getInventory():AddItem(meat)
            sendAddItemToContainer(self.character:getInventory(), meat)
        end
    end

    -- Give a human skull
    if vars.ObtainSkull == true then
        local skull = instanceItem("Human_Skull")
        self.character:getInventory():AddItem(skull)
        sendAddItemToContainer(self.character:getInventory(), skull)
    end

    -- Give normal human bones
    if vars.ObtainBones == true then
        local humanBoneRng = ZombRand(1, 6)
        for i = 1, humanBoneRng do
            local bone = instanceItem("HumanBone")
            self.character:getInventory():AddItem(bone)
            sendAddItemToContainer(self.character:getInventory(), bone)
        end


        -- Give large human bones
        local largeHumanBoneRng = ZombRand(1, 4)
        for i = 1, largeHumanBoneRng do
            local bone = instanceItem("LargeHumanBone")
            self.character:getInventory():AddItem(bone)
            sendAddItemToContainer(self.character:getInventory(), bone)
        end
    end

    addXp(self.character, Perks.Butchering, SandboxVars.Cannibalism.ButcheringXP)
    return true
end

--Give XP

function Cannibalism_ChopCorpse:new(character, corpse, knife) -- What to call in the code
    local o = {}
    setmetatable(o, self)
    self.__index = self
    o.character = character
    o.corpse = corpse
    o.knife = knife
    o.maxTime = 48 * 10 -- Time taken by the action (48 per 1 second)
    if o.character:isTimedActionInstant() then o.maxTime = 1 end
    return o
end
