-----------------------------------
--This file modifies the UI backgrounds based on the current campaign
-----------------------------------

(function()

    local topics = require('lib_ui_topics')
    --local bta = require('btafunctions')
    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 = message.Document:GetElementById("main_background")
        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)]]--

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

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

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

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

    --[[topics.loopbrief.initialize:bind(1, function(message, context)
        local campaignfilename = ba.getCurrentPlayer():getCampaignFilename()
        local main = message.Document:GetElementById("main_background")
        if useSubspaceBG(main) then return end
        if utils.isOneOf(campaignfilename, "bta1_m", "bta2_m", "bta-tem_m") then
            main:SetClass("loop_brief_bg", true)
        end
    end)]]--

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

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

    topics.options.initialize:bind(1, function(message, context)
        local campaignfilename = ba.getCurrentPlayer():getCampaignFilename()
        local main = message.Document:GetElementById("main_background")
        if useSubspaceBG(main) then return end
        if utils.isOneOf(campaignfilename, "bta1_m", "bta2_m", "bta-tem_m") then
            if mn.isInMission() then
                main:SetClass("cockpit_bg", true)
            else
                main:SetClass("options_bg", true)
            end
        end
    end)

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

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

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

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

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

    --For the gamepaused screen background selection
    topics.gamepaused.initialize:bind(1, function(message, context)
        local mission = string.sub(mn.getMissionFilename(), 1, 4)
        local main = message.Document:GetElementById("main_background")
        if mission == "bta1" then
            main:SetClass("paused_bg_bta1", true)
        elseif mission == "bta2" then
            main:SetClass("paused_bg_bta2", true)
        elseif mission == "bta3" then
            main:SetClass("paused_bg_bta3", true)
        else
            main:SetClass("paused_general_bg", true)
        end
    end)

    topics.redalert.initialize:bind(1, function(message, context)
        local campaignfilename = ba.getCurrentPlayer():getCampaignFilename()
        local main = message.Document:GetElementById("main_background")
        --if useSubspaceBG(main) then return end --Red alert is specially handled already
        if utils.isOneOf(campaignfilename, "bta1_m", "bta2_m", "bta-tem_m") then
            main:SetClass("red_alert_bg", true)
        end
    end)

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

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

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

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

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

  end)()