---@diagnostic disable: undefined-field, param-type-mismatch
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by steph. (I can conquer, if not, this code will last longer)
--- DateTime: 9/21/2022 9:05 PM
--- Taken from original PZcode removeburntvehicle in Timed Actions vehicle lua


require "TimedActions/ISBaseTimedAction"

ISVehicleSalvage = ISBaseTimedAction:derive("ISVehicleSalvage")

local function predicateBlowTorch(item)
    return (item ~= nil) and
            (item:hasTag("BlowTorch") or item:getType() == "BlowTorch") and
            (item:getDrainableUsesInt() >= 10)
end

function ISVehicleSalvage:isValid()
    if not predicateBlowTorch(self.character:getPrimaryHandItem()) then
        return false
    end
    return self.vehicle and not self.vehicle:isRemovedFromWorld();
end

function ISVehicleSalvage:update()
    self.character:faceThisObject(self.vehicle)
    self.item:setJobDelta(self:getJobDelta())
    self.item:setJobType(getText("ContextMenu_SalvageVehicle"))

    if self.sound ~= 0 and not self.character:getEmitter():isPlaying(self.sound) then
        self.sound = self.character:playSound("BlowTorch")
    end

    self.character:setMetabolicTarget(Metabolics.HeavyWork);
end

function ISVehicleSalvage:start()
    self.item = self.character:getPrimaryHandItem()
    self:setActionAnim("BlowTorch")
    self:setOverrideHandModels(self.item, nil)
    self.sound = self.character:playSound("BlowTorch")
end

function ISVehicleSalvage:stop()
    if self.item then
        self.item:setJobDelta(0)
    end
    if self.sound ~= 0 then
        self.character:getEmitter():stopSound(self.sound)
    end
    ISBaseTimedAction.stop(self)
end

function ISVehicleSalvage:perform()
    if self.sound ~= 0 then
        self.character:getEmitter():stopSound(self.sound)
    end
    local totalXp = 10;
    for i=1,math.max(6,self.character:getPerkLevel(Perks.MetalWelding)) do
        if self:checkAddItem("MetalBar", 15) then totalXp = totalXp + 2 end;
        if self:checkAddItem("MetalBar", 20) then totalXp = totalXp + 2 end;
        if self:checkAddItem("Radio.ElectricWire", 15) then totalXp = totalXp + 2 end;
        if self:checkAddItem("Wire", 20) then totalXp = totalXp + 2 end;
        if self:checkAddItem("MetalPipe", 15) then totalXp = totalXp + 2 end;
        if self:checkAddItem("MetalPipe", 20) then totalXp = totalXp + 2 end;
        if self:checkAddItem("ElectronicsScrap", 15) then totalXp = totalXp + 2 end;
        if self:checkAddItem("SheetMetal", 12) then totalXp = totalXp + 2 end;
        if self:checkAddItem("SheetMetal", 15) then totalXp = totalXp + 2 end;
        if self:checkAddItem("SheetMetal", 20) then totalXp = totalXp + 2 end;
        if self:checkAddItem("SmallSheetMetal", 12) then totalXp = totalXp + 2 end;
        if self:checkAddItem("SmallSheetMetal", 15) then totalXp = totalXp + 2 end;
        if self:checkAddItem("SmallSheetMetal", 20) then totalXp = totalXp + 2 end;
        if self:checkAddItem("ScrapMetal", 12) then totalXp = totalXp + 2 end;
        if self:checkAddItem("ScrapMetal", 12) then totalXp = totalXp + 2 end;
        if self:checkAddItem("ScrapMetal", 12) then totalXp = totalXp + 2 end;
        if self:checkAddItem("Screws", 12) then totalXp = totalXp + 2 end;
        if self:checkAddItem("Screws", 12) then totalXp = totalXp + 2 end;
        if self:checkAddItem("ElectronicsScrap", 15) then totalXp = totalXp + 2 end;
        if self:checkAddItem("LeatherStripsDirty", 15) then totalXp = totalXp + 2 end;
        if self:checkAddItem("RippedSheetsDirty", 15) then totalXp = totalXp + 2 end;
        if self:checkAddItem("LightBulb", 20) then totalXp = totalXp + 2 end;
        if self:checkAddItem("Amplifier", 20) then totalXp = totalXp + 2 end;
        if self:checkAddItem("EngineParts", 28) then totalXp = totalXp + 3 end;
        if self:checkAddItem("UnusableMetal", 12) then totalXp = totalXp + 2 end;
    end
    for i=1,10 do   --Propane uses--
        self.item:Use();
    end
    self.character:getXp():AddXP(Perks.MetalWelding, totalXp);
    sendClientCommand(self.character, "vehicle", "remove", { vehicle = self.vehicle:getId() })
    self.item:setJobDelta(0);
    -- needed to remove from queue / start next.
    ISBaseTimedAction.perform(self)
end

function ISVehicleSalvage:checkAddItem(item, baseChance)
    if ZombRand(baseChance-self.character:getPerkLevel(Perks.MetalWelding)) == 0 then
        --		self.character:getInventory():AddItem(item);
        self.vehicle:getSquare():AddWorldInventoryItem(item, ZombRandFloat(0,0.9), ZombRandFloat(0,0.9), 0);
        return true;
    end
    return false;
end

function ISVehicleSalvage:new(character, vehicle)
    local o = {}
    setmetatable(o, self)
    self.__index = self
    o.character = character
    o.vehicle = vehicle
    o.maxTime = 3000 - (character:getPerkLevel(Perks.MetalWelding) * 20);
    if character:isTimedActionInstant() then o.maxTime = 10 end
    return o
end