InsurgentWeaponUtil = {}

function InsurgentWeaponUtil.addBasicPistol(player)
    InsurgentWeaponUtil.addWeapon(player, "Base.Pistol", {"Base.RedDot", "Base.Laser"})
    InsurgentWeaponUtil.addMags(player, "Base.9mmClip", 2)
end

function InsurgentWeaponUtil.getAmmoMult(player)
    if player:HasTrait("LoadoutExtraAmmo") then
        return 2 
    elseif player:HasTrait("LoadoutLessAmmo") then
        return 0.5
	end
    return 1
end

function InsurgentWeaponUtil.addWeapon(player, weaponName, attachments)
    local inventory = player:getInventory()
    local weapon = inventory:AddItem(weaponName) 
    if weapon and attachments then
        for _, attachment in ipairs(attachments) do
            weapon:attachWeaponPart(InventoryItemFactory.CreateItem(attachment))
        end
    end
    return weapon
end

function InsurgentWeaponUtil.addMags(player, magName, count)
    local ammoMulti = InsurgentWeaponUtil.getAmmoMult(player)
    for i=1, count * ammoMulti do
        InsurgentWeaponUtil.addMag(player, magName)
    end
end

function InsurgentWeaponUtil.addMag(player, magName)
    local mag = player:getInventory():AddItem(magName)
    mag:setCurrentAmmoCount(mag:getMaxAmmo())
    return mag
end

function InsurgentWeaponUtil.equipAndFullyLoadWeapon(player, weapon, magName)
    local mag = InsurgentWeaponUtil.addMag(player, magName)
    mag:setCurrentAmmoCount(mag:getMaxAmmo() + 1)

    InsurgentWeaponUtil.equipWeapon(player, weapon)
    ISTimedActionQueue.add(ISInsertMagazine:new(player, weapon, mag))
end

function InsurgentWeaponUtil.equipWeapon(player, weapon)
    ISTimedActionQueue.add(ISEquipWeaponAction:new(player, weapon, 30, true, weapon:isTwoHandWeapon()))
end