---@diagnostic disable: duplicate-set-field, 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)
    if not item then return false end

    -- 42.13+: :hasTag expects an ItemTag, not a string
    if item.hasTag and ItemTag and (ResourceLocation and (ResourceLocation.of or ResourceLocation.new)) then
        local rl = (ResourceLocation.of and ResourceLocation.of("base:BlowTorch"))
                or (ResourceLocation.new and ResourceLocation.new("base","BlowTorch"))
        if rl then
            local ok, tag = pcall(function() return ItemTag.get(rl) end)
            if ok and tag and item:hasTag(tag) then
                local uses = (item.getCurrentUses and item:getCurrentUses()) or 0
                return uses >= 10
            end
        end
    end

    -- Fallback for pre-registry builds (or if registry lookup fails)
    local t  = item.getType     and item:getType()     or ""
    local ft = item.getFullType and item:getFullType() or ""
    if (t == "BlowTorch") or (ft == "Base.BlowTorch") then
        local uses = (item.getCurrentUses and item:getCurrentUses()) or 0
        return uses >= 10
    end

    return false
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")

    if self.sound ~= 0 and (self.actionAnim ~= "SewingCloth") then
        local radius = 20
        if self.character.getWeldingSoundMod then
            radius = 20 * self.character:getWeldingSoundMod()
        end
        addSound(self.character, self.character:getX(), self.character:getY(), self.character:getZ(), radius, radius)
    end
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("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;
        --Build 42 items
        if self:checkAddItem("CopperScrap", 12) then totalXp = totalXp + 2 end;
        if self:checkAddItem("AluminumScrap", 15) then totalXp = totalXp + 2 end;
        if self:checkAddItem("AluminumScrap", 10) then totalXp = totalXp + 2 end;
        if self:checkAddItem("SteelScrap", 12) then totalXp = totalXp + 2 end;
        if self:checkAddItem("SteelScrap", 15) then totalXp = totalXp + 2 end;
        if self:checkAddItem("SteelScrap", 20) then totalXp = totalXp + 2 end;
        if self:checkAddItem("IronScrap", 12) then totalXp = totalXp + 2 end;
        if self:checkAddItem("IronBand", 15) then totalXp = totalXp + 2 end;
        if self:checkAddItem("IronBandSmall", 12) then totalXp = totalXp + 2 end;
        if self:checkAddItem("SteelBar", 15) then totalXp = totalXp + 2 end;
        if self:checkAddItem("SteelBarHalf", 12) then totalXp = totalXp + 2 end;
        if self:checkAddItem("SteelBarQuarter", 12) then totalXp = totalXp + 2 end;
        if self:checkAddItem("NutsBolts", 12) then totalXp = totalXp + 2 end;
        if self:checkAddItem("NutsBolts", 12) then totalXp = totalXp + 2 end;
        if self:checkAddItem("NutsBolts", 12) then totalXp = totalXp + 2 end;
    end

    if isClient() then
        sendClientCommand(self.character, "vro_salvage", "drainTorch", { uses = 10 })
    else
        for i=1,10 do
            self.item:Use()
        end
        if self.item and self.item.syncItemFields then
            self.item:syncItemFields()
        end
    end

    sendClientCommand(self.character, "vro_salvage", "addXp", {
    perk   = "MetalWelding",
    amount = 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 = 2000 --[[ Might make this configurable in the future ]]- (character:getPerkLevel(Perks.MetalWelding) * 20);
    if character:isTimedActionInstant() then o.maxTime = 10 end
    return o
end