if not PZAPI or not PZAPI.ModOptions then return end

local BOOL, INT, ENUM = "bool", "int", "enum"

local OPTS = {
    { id = "enableMod",              kind = BOOL, def = true  },
    { id = "zombieDefeatEnabled",    kind = BOOL, def = true  },
    { id = "surrenderDuration",      kind = INT,  min = 1, max = 60,  def = 5  },
    { id = "escapeDuration",         kind = INT,  min = 1, max = 60,  def = 10 },
    { id = "cloakEnabled",           kind = BOOL, def = true  },
    { id = "cloakDurationGameHours", kind = INT,  min = 0, max = 12,  def = 2  },
    { id = "surrenderHotkey",        kind = ENUM, n = 15, def = 14 },
    { id = "cancelSceneOnHotkey",    kind = BOOL, def = false },
    { id = "defeatMode",             kind = ENUM, n = 3,  def = 3  },
    { id = "sprinterTakedownEnabled",kind = BOOL, def = true  },
    { id = "overwhelmZombieCount",   kind = INT,  min = 3, max = 8,   def = 5  },
    { id = "autoGiveUpSensitivity",  kind = INT,  min = 1, max = 10,  def = 2  },
    { id = "clothingGrabPerHit",     kind = INT,  min = 1, max = 5,   def = 1  },
    { id = "clothingGrabDrop",       kind = BOOL, def = true  },
    { id = "ripSceneChance",         kind = INT,  min = 0, max = 100, def = 25 },
    { id = "mainDurationMultiplier", kind = INT,  min = 1, max = 4,   def = 2  },
    { id = "zombieClothingMode",     kind = ENUM, n = 3,  def = 1  },
    { id = "sceneSoundMode",         kind = ENUM, n = 2,  def = 2  },
    { id = "banditLimit",            kind = ENUM, n = 4,  def = 2  },
    { id = "banditRewardNew",        kind = ENUM, n = 4,  def = 2  },
    { id = "slimitMoodleHours",      kind = INT,  min = 1, max = 48,  def = 12 },
    { id = "tLength",                kind = ENUM, n = 6,  def = 3  },
    { id = "clothingStripMode",      kind = ENUM, n = 2,  def = 2  },
    { id = "weaponDropMode",         kind = ENUM, n = 2,  def = 2  },
    { id = "playerDefeatStripMode",  kind = ENUM, n = 2,  def = 1  },
    { id = "zombieSexSickness",      kind = ENUM, n = 5,  def = 3  },
    { id = "preserveShoes",          kind = BOOL, def = true  },
    { id = "sceneBagDrop",           kind = ENUM, n = 2,  def = 1  },
    { id = "maleSubmissiveDefeat",   kind = BOOL, def = false },
    { id = "allowMaleMale",          kind = BOOL, def = false },
    { id = "oppositeGenderOnly",     kind = BOOL, def = false },
    { id = "hideSceneZombies",       kind = BOOL, def = true  },
    { id = "arousalEnabled",         kind = BOOL, def = true  },
    { id = "arousalBuildHours",      kind = INT,  min = 6, max = 240, def = 96 },
    { id = "arousalPenalties",       kind = BOOL, def = true  },
    { id = "arousalMagazineGain",    kind = INT,  min = 0, max = 100, def = 50 },
    { id = "arousalNoiseSleep",      kind = BOOL, def = false },
    { id = "struggleEnabled",        kind = BOOL, def = true  },
    { id = "struggleMode",           kind = ENUM, n = 4,  def = 1  },
    { id = "struggleDifficulty",     kind = INT,  min = 1, max = 10,  def = 3  },
}

local function nameKey(id) return "Sandbox_ZomboRut_" .. id end
local function tipKey(id)  return "Sandbox_ZomboRut_" .. id .. "_tooltip" end

local function commit(id, value)
    if SandboxVars and SandboxVars.ZomboRut then
        SandboxVars.ZomboRut[id] = value
    end
    pcall(function()
        if getSandboxOptions then
            getSandboxOptions():set("ZomboRut." .. id, value)
        end
    end)
end

local syncFromSandbox

local function openSandboxEditor()
    if ISServerSandboxOptionsUI.instance then
        ISServerSandboxOptionsUI.instance:close()
    end
    local ui = ISServerSandboxOptionsUI:new(150, 150, 800, 600)

    local baseApply = ui.onButtonApply
    ui.onButtonApply = function(self, ...)
        local ret = baseApply(self)
        if not isClient() then
            pcall(function() getSandboxOptions():toLua() end)
        end
        syncFromSandbox()
        return ret
    end

    ui:initialise()
    ui:addToUIManager()
end

local options = PZAPI.ModOptions:create("ZomboRutFramework", "ZomboRut")
options:addDescription("IGUI_ZomboRut_ModOptionsHint")

for _, o in ipairs(OPTS) do
    local nk, tk = nameKey(o.id), tipKey(o.id)
    if o.kind == BOOL then
        local c = options:addTickBox(o.id, nk, o.def, tk)
        c.onChangeApply = function(_, v) commit(o.id, v == true) end
    elseif o.kind == INT then
        local c = options:addSlider(o.id, nk, o.min, o.max, 1, o.def, tk)
        c.onChangeApply = function(_, v) commit(o.id, math.floor((v or o.def) + 0.5)) end
    elseif o.kind == ENUM then
        local c = options:addComboBox(o.id, nk, tk)
        for k = 1, o.n do c:addItem(nk .. "_option" .. k, k == o.def) end
        c.onChangeApply = function(_, v) commit(o.id, v) end
    end
end

options:addButton("openSandbox", "IGUI_ZomboRut_OpenSandbox", "IGUI_ZomboRut_OpenSandbox_TT", openSandboxEditor)

function syncFromSandbox()
    if not (SandboxVars and SandboxVars.ZomboRut) then return end
    for _, o in ipairs(OPTS) do
        local cur = SandboxVars.ZomboRut[o.id]
        local opt = options:getOption(o.id)
        if cur ~= nil and opt and opt.setValue then
            opt:setValue(cur)
        end
    end
end
Events.OnGameStart.Add(syncFromSandbox)
