local Helpers = require("BC/Helpers")

local MODULE_ID = Helpers.MODULE_ID
local OPTIONS_APPLIED_EVENT = "BetterCookingUI_OptionsApplied"

local DEBUG = getCore and getCore():getDebug() or false
local function dlog(msg)
    if not DEBUG then return end
    if DebugLog and DebugLog.log then
        DebugLog.log(DebugType.General, "[" .. MODULE_ID .. "] " .. tostring(msg))
    end
end

-- Public options state (defaults)
local Options = {
    enableIngredientSubMenu = true,
    enableSeasoningSubMenu = true,
    enableQuantitySuffix = true,

    --[[
    enableSpoilageSort = true,
    enableUsesSort = true,
    ]]

    forceFirstToRotIngredients = true,
    forceFirstToRotSeasonings = true,

    enableIngredientPartial = false,
    separateIngredientQualities = true,

    showRottenIngredientGroup = true,
    showRottenSeasoningGroup = true,
}

local config = {
    enableIngredientSubMenu = nil,
    enableSeasoningSubMenu = nil,
    enableQuantitySuffix = nil,

    --[[
    enableSpoilageSort = nil,
    enableUsesSort = nil,
    ]]

    forceFirstToRotIngredients = nil,
    forceFirstToRotSeasonings = nil,

    enableIngredientPartial = nil,
    separateIngredientQualities = nil,

    showRottenIngredientGroup = nil,
    showRottenSeasoningGroup = nil,
}

local function _getBool(opts, key, defaultValue)
    if not opts or not opts.getOption then return defaultValue end
    local opt = opts:getOption(key)
    if not opt or not opt.getValue then return defaultValue end
    local v = opt:getValue()
    return v == true
end

local function applyOptions()
    local opts = PZAPI and PZAPI.ModOptions and PZAPI.ModOptions:getOptions(MODULE_ID)
    if not opts then
        dlog("ModOptions not ready; keeping defaults.")
        return
    end

    Options.enableIngredientSubMenu = _getBool(opts, "enableIngredientSubMenu", Options.enableIngredientSubMenu)
    Options.enableSeasoningSubMenu  = _getBool(opts, "enableSeasoningSubMenu",  Options.enableSeasoningSubMenu)
    Options.enableQuantitySuffix    = _getBool(opts, "enableQuantitySuffix",    Options.enableQuantitySuffix)

    --[[
    Options.enableSpoilageSort      = _getBool(opts, "enableSpoilageSort",      Options.enableSpoilageSort)
    Options.enableUsesSort          = _getBool(opts, "enableUsesSort",          Options.enableUsesSort)
    ]]

    Options.forceFirstToRotIngredients = _getBool(opts, "forceFirstToRotIngredients", Options.forceFirstToRotIngredients)
    Options.forceFirstToRotSeasonings  = _getBool(opts, "forceFirstToRotSeasonings",  Options.forceFirstToRotSeasonings)

    Options.enableIngredientPartial     = _getBool(opts, "enableIngredientPartial",     Options.enableIngredientPartial)
    Options.separateIngredientQualities = _getBool(opts, "separateIngredientQualities", Options.separateIngredientQualities)

    Options.showRottenIngredientGroup = _getBool(opts, "showRottenIngredientGroup", Options.showRottenIngredientGroup)
    Options.showRottenSeasoningGroup  = _getBool(opts, "showRottenSeasoningGroup",  Options.showRottenSeasoningGroup)

    triggerEvent(OPTIONS_APPLIED_EVENT)
end

local function initConfig()
    local title = getTextOrNull("UI_BetterCooking_Options_Title") or "Better Cooking"
    local panel = PZAPI and PZAPI.ModOptions and PZAPI.ModOptions:create(MODULE_ID, title)
    if not panel then
        dlog("PZAPI.ModOptions missing; no options UI (defaults apply).")
        return
    end

    config.enableIngredientSubMenu = panel:addTickBox(
        "enableIngredientSubMenu",
        getTextOrNull("UI_BetterCooking_Ingredients_SubMenu") or "Ingredients Sub-Menu",
        Options.enableIngredientSubMenu,
        getTextOrNull("UI_BetterCooking_Ingredients_SubMenu_tooltips") or "Puts all Ingredients into an Ingredients Sub-menu."
    )

    config.enableSeasoningSubMenu = panel:addTickBox(
        "enableSeasoningSubMenu",
        getTextOrNull("UI_BetterCooking_Seasonings_SubMenu") or "Seasonings Sub-Menu",
        Options.enableSeasoningSubMenu,
        getTextOrNull("UI_BetterCooking_Seasonings_SubMenu_tooltip") or "Puts all Seasonings into a Seasonings Sub-menu."
    )

    config.enableQuantitySuffix = panel:addTickBox(
        "enableQuantitySuffix",
        getTextOrNull("UI_BetterCooking_QuantitySuffix") or "Quantity Suffix",
        Options.enableQuantitySuffix,
        getTextOrNull("UI_BetterCooking_QuantitySuffix_tooltip") or "Displays the Quantity of items available for a seasoning/ingredient as a suffix."
    )

    --[[
    config.enableSpoilageSort = options:addTickBox(
        "enableSpoilageSort",
        getTextOrNull("UI_BetterCooking_SortBySpoilage") or "Sort by Spoilage",
        Options.enableSpoilageSort,
        getTextOrNull("UI_BetterCooking_SortBySpoilage_tooltip") or "This will ensure the seasoning/ingredient shown is the first that will spoil. (Sorted by spoilage THEN uses)"
    )
    config.enableUsesSort = options:addTickBox(
        "enableUsesSort",
        getTextOrNull("UI_BetterCooking_SortByUsesRemaining") or "Sort by Uses Remaining",
        Options.enableUsesSort,
        getTextOrNull("UI_BetterCooking_SortByUsesRemaining") or "This will sort seasonings by the lowest remaining uses. (Sorted by spoilage THEN uses)."
    )
    ]]

    config.forceFirstToRotIngredients = panel:addTickBox(
        "forceFirstToRotIngredients",
        getTextOrNull("UI_BetterCooking_ForceFirstToRotIngredients") or "Force First to Rot (Ingredients)",
        Options.forceFirstToRotIngredients,
        getTextOrNull("UI_BetterCooking_ForceFirstToRotIngredients_tooltip") or "Overrides alphabetical ordering for the Ingredients list and sorts by spoilage first (then uses remaining)."
    )

    config.forceFirstToRotSeasonings = panel:addTickBox(
        "forceFirstToRotSeasonings",
        getTextOrNull("UI_BetterCooking_ForceFirstToRotSeasonings") or "Force First to Rot (Seasonings)",
        Options.forceFirstToRotSeasonings,
        getTextOrNull("UI_BetterCooking_ForceFirstToRotSeasonings_tooltip") or "Overrides alphabetical ordering for the Seasonings list and sorts by spoilage first (then uses remaining)."
    )

    panel:addDescription(
        getTextOrNull("UI_BetterCooking_Ingredients_Info")
            or "Ingredients will always show the largest available +hunger and first to spoil, sorted alphabetically."
    )

    config.enableIngredientPartial = panel:addTickBox(
        "enableIngredientPartial",
        getTextOrNull("UI_BetterCooking_Ingredients_ShowPartial") or "Show Partial Ingredients",
        Options.enableIngredientPartial,
        getTextOrNull("UI_BetterCooking_Ingredients_ShowPartial_tooltip") or "Adds an entry to the ingredients list that will show partial hunger values, largest partial and first to spoil. (Depending on prior options)"
    )

    config.separateIngredientQualities = panel:addTickBox(
        "separateIngredientQualities",
        getTextOrNull("UI_BetterCooking_Ingredients_SeparateQualities") or "Separate Ingredient Qualities",
        Options.separateIngredientQualities,
        getTextOrNull("UI_BetterCooking_Ingredients_SeparateQualities_tooltip") or "When enabled, ingredients with a Quality value (e.g. meat) will be split into separate entries per quality tier. Counts are per tier."
    )

    panel:addDescription(
        getTextOrNull("UI_BetterCooking_Rotten_Info") or "Choose how to handle Rotten entries in the menu"
    )

    config.showRottenIngredientGroup = panel:addTickBox(
        "showRottenIngredientGroup",
        getTextOrNull("UI_BetterCooking_Ingredients_ShowRotten") or "Show Rotten Ingredients",
        Options.showRottenIngredientGroup,
        getTextOrNull("UI_BetterCooking_Ingredients_ShowRotten_tooltip") or "Adds an entry to the ingredients list that will show rotten items."
    )

    config.showRottenSeasoningGroup = panel:addTickBox(
        "showRottenSeasoningGroup",
        getTextOrNull("UI_BetterCooking_Seasonings_ShowRotten") or "Show Rotten Seasonings",
        Options.showRottenSeasoningGroup,
        getTextOrNull("UI_BetterCooking_Seasonings_ShowRotten_tooltip") or "Adds an entry to the seasonings list that will show rotten items."
    )

    -- Apply handler (_Options.lua pattern)
    panel.apply = function()
        applyOptions()
    end
end

-- Init immediately and re-apply on menu enter (same cadence as _Options.lua)
initConfig()
Events.OnMainMenuEnter.Add(function() applyOptions() end)

-- Back-compat helper API (other files can keep calling OPS.shouldX())
function Options.shouldUseSeasoningsSubMenu()
    return Options.enableSeasoningSubMenu == true
end

function Options.shouldUseIngredientsSubMenu()
    return Options.enableIngredientSubMenu == true
end

function Options.shouldShowQuantity()
    return Options.enableQuantitySuffix == true
end

--[[
function OPS.shouldSortBySpoilage()
    local result = OPS.enableSpoilageSort and OPS.enableSpoilageSort:getValue() == true
    return result
end

function OPS.shouldSortByUsesRemaining()
    local result = OPS.enableUsesSort and OPS.enableUsesSort:getValue() == true
    return result
end
]]

function Options.shouldForceFirstToRotIngredients()
    return Options.forceFirstToRotIngredients == true
end

function Options.shouldForceFirstToRotSeasonings()
    return Options.forceFirstToRotSeasonings == true
end

function Options.shouldShowPartialIngredients()
    return Options.enableIngredientPartial == true
end

function Options.shouldSeparateIngredientQualities()
    return Options.separateIngredientQualities == true
end

function Options.shouldShowRottenIngredientGroup()
    return Options.showRottenIngredientGroup == true
end

function Options.shouldShowRottenSeasoningGroup()
    return Options.showRottenSeasoningGroup == true
end

return Options