-----------------------------------
--This file modifies the Journal background based on the current campaign
-----------------------------------

(function()

    local topics = require('lib_ui_topics')
    local utils = require('lib_utils')

    local function useSubspaceBG(el)
        local oldState = nil
        if hv.OldState ~= nil then
            oldState = hv.OldState.Name
        end

        local missionname = nil
        if mn.getMissionFilename() ~= "" and oldState ~= "GS_STATE_MAIN_MENU" then
            missionname = mn.getMissionFilename() .. ".fs2"
        else
            missionname = ca.getNextMissionFilename()
        end

        if missionname ~= nil then
            missionname = missionname:match("(.+)%..+")

            if utils.isOneOf(missionname, "bta1_m2_08", "bta2_m1_02") then
                if ba.getCurrentGameState().Name == "GS_STATE_DEBRIEF" or mn.isInMission() then
                    return false
                end
                el:SetClass("subspace_red_alert_bg", true)
                return true
            elseif missionname == "bta2_m3_07" then
                el:SetClass("taweret_red_alert_bg", true)
                return true
            elseif missionname == "bta2_m3_08" then
                if ba.getCurrentGameState().Name == "GS_STATE_DEBRIEF" then
                    return false
                end
                el:SetClass("annwn_red_alert_bg", true)
                return true
            end
        end

        return false
    end

    topics.journal.initialize:bind(1, function(message, context)
        local campaignfilename = ba.getCurrentPlayer():getCampaignFilename()
        local main = ScpuiSystem:getRootParent(message.Document)
        if useSubspaceBG(main) then return end
        if utils.isOneOf(campaignfilename, "bta1_m", "bta2_m", "bta-tem_m") then
            main:SetClass("journal_bg", true)
        end
    end)

end)()