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

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
    local vars = SandboxVars.Cannibalism
    self.corpse:getSquare():removeCorpse(self.corpse, false);
    local isHuman = CannibalismUtils.isBodyHuman(self.corpse)

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

    self.character:getEmitter():stopSound(self.sound)
    ISBaseTimedAction.perform(self);
end

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
