-- @author DoomTero 2026
-- @copyright 2026 DoomTero - ZomboRut original work. ref:0cd830cd
-- @license Proprietary. Not licensed for reuse, redistribution or derivative works without written permission from DoomTero. Ask - permission is obtainable. See ZomboRut/LICENSE.txt

local DEFAULT_MAGAZINE_GAIN = 50

local MAGAZINE_TYPES = {
    ["Base.HottieZ"]     = true,
    ["Base.HottieZ_New"] = true,
}

local function _sandbox()
    return SandboxVars and SandboxVars.ZomboRut
end

local function _enabled()
    local sv = _sandbox()
    if not sv then return false end
    if sv.enableMod == false then return false end
    if sv.arousalEnabled == false then return false end
    return true
end

local function _magazineGain()
    local sv = _sandbox()
    local g = sv and sv.arousalMagazineGain
    if type(g) ~= "number" then g = DEFAULT_MAGAZINE_GAIN end
    if g < 0 then g = 0 end
    return g
end

local function magazineRead(character, item)
    if character == nil or item == nil then return end
    if not character.isLocalPlayer or not character:isLocalPlayer() then return end
    if not MAGAZINE_TYPES[item:getFullType()] then return end
    if not _enabled() then return end

    local gain = _magazineGain()
    if gain <= 0 then return end

    if ZR_Arousal and ZR_Arousal.gain then
        ZR_Arousal.gain(character, gain, "magazine")
    end
end

local function patchReadArousal()
    if ISReadABook == nil or ISReadABook.perform == nil then return end
    if ISReadABook._zrMagazinePatched then return end
    ISReadABook._zrMagazinePatched = true

    local origPerform = ISReadABook.perform
    function ISReadABook:perform(...)
        origPerform(self, ...)
        pcall(function()
            magazineRead(self.character, self.item)
        end)
    end
end

Events.OnGameStart.Add(patchReadArousal)
