local ADDON_NAME, ns = ...
ns = ns or {}

local Util = ns.Util
local Const = ns.Const
local Panel = ns.Panel

local Remix = {}
ns.Remix = Remix
Remix._autoApplied = false
local function isDefaultRemixExpSelection()
    local selected = 0
    for exp, val in pairs(ACAU_ExpFilterDB or {}) do
        if val == true then
            selected = selected + 1
            if exp ~= Const.REMIX_EXPANSION then
                return false
            end
        end
    end
    return selected == 1 and (ACAU_ExpFilterDB[Const.REMIX_EXPANSION] == true)
end

local function safeSeasonID()
    local fn = _G.PlayerGetTimerunningSeasonID
    if type(fn) ~= "function" then return nil end
    local ok, season = pcall(fn)
    if not ok then return nil end
    return season
end

function Remix:IsRemixCharacter()
    if not Util or not Util.IsRetail or not Util:IsRetail() then return false end
    return safeSeasonID() == Const.REMIX_SEASON_ID
end

local function rememberCurrentState()
    ACAU_Config.remixRestore = {
        categories = Util:DeepCopy(ACAU_CategoryDB),
        expansions = Util:DeepCopy(ACAU_ExpFilterDB),
        theme = ACAU_Config.theme,
        profMode = ACAU_CategoryDB["ProfessionsMode"],
        worldMode = ACAU_CategoryDB["WorldEventsMode"],
    }
end

local function applyRemixFilters()
    ACAU_CategoryDB = ACAU_CategoryDB or {}
    ACAU_ExpFilterDB = ACAU_ExpFilterDB or {}
    for category, val in pairs(ACAU_CategoryDB) do
        if type(val) == "boolean" and category ~= Const.REMIX_CATEGORY then
            ACAU_CategoryDB[category] = false
        end
    end
    for category in pairs(Const.CATEGORY_DEFAULTS) do
        if category ~= "ProfessionsMode" and category ~= "WorldEventsMode" then
            ACAU_CategoryDB[category] = (category == Const.REMIX_CATEGORY)
        end
    end
    ACAU_CategoryDB["ProfessionsMode"] = "None"
    ACAU_CategoryDB["WorldEventsMode"] = "None"

    for exp in pairs(ACAU_ExpFilterDB) do
        if type(ACAU_ExpFilterDB[exp]) == "boolean" then
            ACAU_ExpFilterDB[exp] = (exp == Const.REMIX_EXPANSION)
        end
    end
    for exp in pairs(Const.EXPANSION_DEFAULTS) do
        ACAU_ExpFilterDB[exp] = (exp == Const.REMIX_EXPANSION)
    end
end

local function restoreState()
    local state = ACAU_Config.remixRestore
    if not state then return end
    ACAU_CategoryDB = Util:MergeDefaults(Util:DeepCopy(state.categories or {}), Const.CATEGORY_DEFAULTS)
    ACAU_ExpFilterDB = Util:MergeDefaults(Util:DeepCopy(state.expansions or {}), Const.EXPANSION_DEFAULTS)
    if state.profMode then ACAU_CategoryDB["ProfessionsMode"] = state.profMode end
    if state.worldMode then ACAU_CategoryDB["WorldEventsMode"] = state.worldMode end
    if state.theme then ACAU_Config.theme = state.theme end
    ACAU_Config.remixRestore = nil
end

function Remix:Enable()
    if not ACAU_Config then return end
    if not ACAU_Config.remixModeEnabled then
        rememberCurrentState()
    end
    ACAU_Config.remixModeEnabled = true
    ACAU_Config.remixRelaxed = false
    applyRemixFilters()
    ACAU_Config.theme = Const.REMIX_THEME
end

function Remix:Disable()
    if not ACAU_Config then return end
    ACAU_Config.remixModeEnabled = false
    ACAU_Config.remixRelaxed = nil
    restoreState()
end

function Remix:MaybeAutoApply()
    if not ACAU_Config then return end
    if self._autoApplied then return end
    if ACAU_Config.remixAutoApply == false then return end
    if not self:IsRemixCharacter() then return end
    self._autoApplied = true
    self:Enable()
end

function Remix:OnExpansionChanged()
    if not (ACAU_Config and ACAU_Config.remixModeEnabled) then return end
    if ACAU_Config.remixRelaxed then return end
    if isDefaultRemixExpSelection() then return end
    local state = ACAU_Config.remixRestore or {}
    local restored = false
    if state.categories then
        ACAU_CategoryDB = Util:MergeDefaults(Util:DeepCopy(state.categories), Const.CATEGORY_DEFAULTS)
        restored = true
    end
    if state.profMode then
        ACAU_CategoryDB["ProfessionsMode"] = state.profMode
    else
        ACAU_CategoryDB["ProfessionsMode"] = ACAU_CategoryDB["ProfessionsMode"] or "All"
    end
    if state.worldMode then
        ACAU_CategoryDB["WorldEventsMode"] = state.worldMode
    else
        ACAU_CategoryDB["WorldEventsMode"] = ACAU_CategoryDB["WorldEventsMode"] or "All"
    end
    if not restored then
        for cat, _ in pairs(Const.CATEGORY_DEFAULTS) do
            if cat ~= "ProfessionsMode" and cat ~= "WorldEventsMode" then
                ACAU_CategoryDB[cat] = true
            end
        end
    end
    ACAU_Config.remixRelaxed = true
    if Panel and Panel.RefreshFiltersUI then
        Panel:RefreshFiltersUI()
    end
end
