if ETPS == nil then
    ETPS = {}
end

function Debug_Swap(message)
    ba.print("Swap "..message.."\n")
end

--The actual core SEXP function, assigned down at the bottom.
function ETPS:SwapWeapon_SEXP(remove,install,lock, ...)
    local weaponInfo = tb.WeaponClasses
    local weaponTypeCount = #weaponInfo
    local found = 0
    local removeHandle
    local installHandle
    local rname = string.lower(remove)
    local iname = string.lower(install)
    local wepname
    for weaponIndex =1,weaponTypeCount do
        wepname = string.lower(weaponInfo[weaponIndex].Name)
        if rname==wepname then
            removeHandle = weaponInfo[weaponIndex]
            found = found + 1
        end
        if iname==wepname then
            installHandle = weaponInfo[weaponIndex]
            found = found + 1
        end
    end
    if found < 2 then
        ba.error("Swap SEXP attempted to reference a weapon that it can't find. Weapons: "..remove..","..install)
    end
    Debug_Swap("starting swap attempt")
    Debug_Swap("changing "..removeHandle.Name.." to "..installHandle.Name.."")
    if lock == 1 then Debug_Swap("and locking") end
    if lock == 2 then Debug_Swap("and unlocking") end
    local targets = ...
    local name
    local handle
    if targets == nil then
        Debug_Swap("no targets provided, checking everything")
        local numShips = #mn.Ships
        local ships = mn.Ships
        if numShips > 0 then
            for shipsIndex = 1, numShips do
                name, handle = nil, nil
                handle = ships[shipsIndex]
                name = handle.Name
                Debug_Swap("Checking "..name)
                ETPS:SwapWeapon_Ship(name,handle,lock,removeHandle,installHandle)
            end
        end
    else

    local name
    for i,handle in pairs(targets) do
            name = handle.Name

            Debug_Swap("Checking "..name)
            ETPS:SwapWeapon_Ship(name,handle,lock,removeHandle,installHandle)
        end
    end
    Debug_Swap("Swap finished")
end

--So I want to do a number of different things to weapons
--    Lock or unlock turrets
--    damage or repair turrets
--    swap with other weapons
--        preserve or refill ammo
--        act on ammo in percentage or raw numbers


function ETPS:SwapWeapon_Ship(name,handle,lock,remove,install)
    local subs = handle
    if subs == nil then
        return
    end
    local subcount = #subs
    local subhandle
    for subindex = 1,subcount do
        Debug_Swap("\tsubsystem "..subindex)
        subhandle = subs[subindex]
        if subhandle:isTurret() then
            Debug_Swap("\tis turret ")
            ETPS:SwapWeapon_Turret(subhandle,lock,remove,install,name)
        end
    end
end

function ETPS:SwapWeapon_Turret(handle,lock,remove,install,shipname)
    local primaries = handle.PrimaryBanks
    local secondaries = handle.SecondaryBanks
    local either = false
    if primaries then
        Debug_Swap("\t\twith primary banks")
        either = either or ETPS:SwapWeapon_Banks(primaries,lock,remove,install)
    end
    if secondaries then
        Debug_Swap("\t\twith secondary banks")
        either = either or ETPS:SwapWeapon_Banks(secondaries,lock,remove,install)
    end
    if either == true then
        if lock == 1 then
            handle.TurretLocked = true
            Debug_Swap("\t\tand locking")
        elseif lock == 2 then
            handle.TurretLocked = false
            local strsexp = ("( beam-free \""..shipname.."\" \""..handle:getModelName().."\" )" )
            mn.evaluateSEXP(strsexp)
            Debug_Swap("\t\tand unlocking ".. strsexp)
        end
    end
end

function ETPS:SwapWeapon_Banks(handle,lock,remove,install)
    local weapons = tb.WeaponClasses
    local bankCount = #handle
    local bank
    local handled = 0
    if bankCount <1 then
        return false
    end
    for bankindex = 1,bankCount do
        bank = handle[bankindex]
        Debug_Swap("\t\t\tbank "..bankindex)
        if bank and bank:isValid() then
            if bank.WeaponClass then
                Debug_Swap("\t\t\t\tis "..bank.WeaponClass.Name)
                if bank.WeaponClass == remove then
                    Debug_Swap("match, swaping for "..install.Name)
                    handle[bankindex].WeaponClass = install
                    handled = handled+1
                end
            end
        end
    end
    if handled > 0 then return true end
    return false
end




--mn.LuaSEXPs["weapon-swap-mass"].Action = ETPS:SwapWeapon_SEXP --
mn.LuaSEXPs["weapon-swap-mass"].Action = function(remove,install,lock, ...) ETPS:SwapWeapon_SEXP (remove,install,lock, ...)  end