local OFBCGuns93ModPackInjector = {
    installed = false,
    tickHookAdded = false,
}

local APPLY_MODS_MODULE = "BetterContainers/Categorize/Categories/_applyMods"
local GUNS93_PACK_PATH = "mods/guns93_items"

local BC_MOD_IDS = {
    "EURY_CONTAINERS",
    "REORDER_CONTAINERS",
    "\\EURY_CONTAINERS",
    "\\REORDER_CONTAINERS",
}

local GUNS93_MOD_IDS = {
    "guns93",
    "\\guns93",
}

local function isSortingToggleEnabled()
    local vars = SandboxVars and SandboxVars.OpinionatedFirearms
    if vars and vars.AddSorting ~= nil then
        return vars.AddSorting == true
    end

    return true
end

local function getActivatedModsList()
    if type(getActivatedMods) ~= "function" then
        return nil
    end

    local mods = getActivatedMods()
    if not mods or type(mods.contains) ~= "function" then
        return nil
    end

    return mods
end

local function isBetterContainersActive()
    local mods = getActivatedModsList()
    if not mods then
        return nil
    end

    for i = 1, #BC_MOD_IDS do
        if mods:contains(BC_MOD_IDS[i]) then
            return true
        end
    end

    return false
end

local function isGuns93Active()
    local mods = getActivatedModsList()
    if not mods then
        return false
    end

    for i = 1, #GUNS93_MOD_IDS do
        if mods:contains(GUNS93_MOD_IDS[i]) then
            return true
        end
    end

    return false
end

local function installApplyModsWrapper()
    if OFBCGuns93ModPackInjector.installed then
        return true
    end

    if not isSortingToggleEnabled() then
        OFBCGuns93ModPackInjector.installed = true
        return true
    end

    local bcState = isBetterContainersActive()
    if bcState == false then
        OFBCGuns93ModPackInjector.installed = true
        return true
    end
    if bcState == nil then
        return false
    end

    if _G.OF_GUNS93_BC_APPLYMODS_WRAPPED then
        OFBCGuns93ModPackInjector.installed = true
        return true
    end

    local ok, applyMods = pcall(require, APPLY_MODS_MODULE)
    if not ok or type(applyMods) ~= "function" then
        return false
    end

    if type(package) ~= "table" or type(package.loaded) ~= "table" then
        return false
    end

    local function wrappedApplyMods(loadCategoryFile, dlog)
        applyMods(loadCategoryFile, dlog)

        if not isGuns93Active() then
            return
        end

        if type(loadCategoryFile) == "function" then
            loadCategoryFile(GUNS93_PACK_PATH)
            if dlog then
                dlog("Loaded category pack guns93_items")
            end
        end
    end

    package.loaded[APPLY_MODS_MODULE] = wrappedApplyMods
    _G.OF_GUNS93_BC_APPLYMODS_WRAPPED = true
    OFBCGuns93ModPackInjector.installed = true
    return true
end

local function tryInstallOnTick()
    if installApplyModsWrapper() then
        OFBCGuns93ModPackInjector.tickHookAdded = false
        Events.OnTick.Remove(tryInstallOnTick)
    end
end

local function ensureTickHook()
    if OFBCGuns93ModPackInjector.tickHookAdded then
        return
    end

    OFBCGuns93ModPackInjector.tickHookAdded = true
    Events.OnTick.Add(tryInstallOnTick)
end

if not installApplyModsWrapper() then
    ensureTickHook()
end

if Events.OnMainMenuEnter and type(Events.OnMainMenuEnter.Add) == "function" then
    Events.OnMainMenuEnter.Add(installApplyModsWrapper)
end
