-----------------------------------
--This file modifies the Barracks UI for BtA
-----------------------------------

(function()

    local topics = require('lib_ui_topics')
    --local bta = require('btafunctions')
    local utils = require('lib_utils')

    local function makeBarracksSectionControls(message, section)
        local wrapper_el = ScpuiSystem:makeElement(message, "div", "barracks_top_panel_wrapper")
        local section_el = ScpuiSystem:makeElementPanel(message, "barracks_top_panel_b", "ops-panel-vert-tall-l.png")

        wrapper_el.style.width = "4%"
        wrapper_el.style.height = "auto"
        wrapper_el.style.overflow = "hidden"
        wrapper_el.style.position = "absolute"
        wrapper_el.style.top = "0"
        wrapper_el.style.left = "0"

        section_el.style.width = "100%"
        section_el.style.height = "auto"
        section_el.style.position = "relative"
        section_el.style.float = "left"

        wrapper_el:AppendChild(section_el)

        section_el.first_child.style["margin-top"] = "5%"
        section_el.first_child.style.width = "100%"
        section_el.first_child.style.height = "100%"

        local barracks_btn = ScpuiSystem:makeButton(message, "barracks_bullet_cont", "barracks_btn_1", {"barracks_bullet", "button_1"}, "bullet", "bullet-r.png", "barracks_btn_1_text", {"pos"}, ba.XSTR("Player Stats", 9285))
        local missions_btn = ScpuiSystem:makeButton(message, "barracks_bullet_cont", "barracks_btn_2", {"barracks_bullet", "button_1"}, "bullet", "bullet-r.png", "barracks_btn_2_text", {"pos"}, ba.XSTR("Mission Simulator", 9286))
        local cutscenes_btn = ScpuiSystem:makeButton(message, "barracks_bullet_cont", "barracks_btn_3", {"barracks_bullet", "button_1"}, "bullet", "bullet-r.png", "barracks_btn_3_text", {"pos"}, ba.XSTR("Movies", 9287))

        local btns = {barracks_btn, missions_btn, cutscenes_btn}

        for _, v in ipairs(btns) do
            v.style.width = "65%"
            v.style.height = "auto"
            v.style.margin = "auto"

            v.first_child.style.display = "block"
            v.first_child.style.width = "100%"
            v.first_child.style.height = "auto"

            v.first_child.first_child.style.display = "block"
            v.first_child.first_child.style.width = "100%"
            v.first_child.first_child.style.height = "auto"

            v.first_child.first_child.first_child.style.display = "block"
            v.first_child.first_child.first_child.style.width = "100%"
            v.first_child.first_child.first_child.style.height = "auto"

            v.first_child.last_child.style["text-align"] = "left"
            v.first_child.last_child.first_child.style.width = "250%"
            v.first_child.last_child.first_child.style["margin-left"] = "90%"
            v.first_child.last_child.first_child.style["margin-top"] = "-20%"
            v.first_child.last_child.first_child.style.position = "absolute"
        end

        if section ~= 1 then
            barracks_btn:AddEventListener("click", function(_, _, _)
                ba.postGameEvent(ba.GameEvents["GS_EVENT_BARRACKS_MENU"])
            end)
        else
            barracks_btn.first_child:SetPseudoClass("checked", true)
        end

        if section ~= 2 then
            missions_btn:AddEventListener("click", function(_, _, _)
                ba.postGameEvent(ba.GameEvents["GS_EVENT_SIMULATOR_ROOM"])
            end)
        else
            missions_btn.first_child:SetPseudoClass("checked", true)
        end

        if section ~= 3 then
            cutscenes_btn:AddEventListener("click", function(_, _, _)
                ba.postGameEvent(ba.GameEvents["GS_EVENT_GOTO_VIEW_CUTSCENES_SCREEN"])
            end)
        else
            cutscenes_btn.first_child:SetPseudoClass("checked", true)
        end

        section_el.first_child:AppendChild(barracks_btn)
        section_el.first_child:AppendChild(missions_btn)
        section_el.first_child:AppendChild(cutscenes_btn)

        return wrapper_el
    end

    --Hides the pilot head, the squad image, the logo, and the pilot select elements entirely
    topics.barracks.initialize:bind(9999, function(message, context)
        message.Document:GetElementById("pilot_select"):SetClass("hidden", true)
        message.Document:GetElementById("logo"):SetClass("hidden", true)
        message.Document:GetElementById("pilot_squad_select"):SetClass("hidden", true)
        message.Document:GetElementById("pilot_head_select"):SetClass("hidden", true)
    end)

    --Makes all our custom elements used in the Barracks
    topics.barracks.initialize:bind(1, function(message, context)
        local main_el = ScpuiSystem:getRootParent(message.Document)

        --First let's make a header element with the player name
        local player_el = ScpuiSystem:makeElementPanel(message, "player_panel", "tech-room-text-window.png")
        local name_el = ScpuiSystem:makeElement(message, "div", "player_name")

        name_el.inner_rml = ba.getCurrentPlayer():getName()
        name_el:SetClass("h2", true)
        name_el.style["text-align"] = "center"

        player_el.style.width = "30%"
        player_el.style.position = "absolute"
        player_el.style.top = "5%"
        player_el.style.left = "35%"

        local inner_el = player_el.first_child
        inner_el.style.top = "10%"
        inner_el:AppendChild(name_el)

        main_el:AppendChild(player_el)

        --Make the section controls
        local wrapper_el = makeBarracksSectionControls(message, 1)

        main_el:AppendChild(wrapper_el)

        --Make a wrapper container
        local panel_wrapper_el = ScpuiSystem:makeElement(message, "div", "lower_windows")
        panel_wrapper_el.style.position = "absolute"
        panel_wrapper_el.style.top = "25%"
		panel_wrapper_el.style.left = "10%"
		panel_wrapper_el.style.width = "80%"
		
		main_el:AppendChild(panel_wrapper_el)

        --Hide the medals button text
        local medals_text_el = message.Document:GetElementById("medals_btn").last_child
        medals_text_el:SetClass("hidden", true)

        local completion_el = ScpuiSystem:makeElementPanel(message, "completion_info", "pilot-info-window.png")
        completion_el.style.width = "46%"
        completion_el.style.height = "auto"
        completion_el.style.overflow = "hidden"
        completion_el.style.position = "relative"
        completion_el.style.float = "left"
		completion_el.style.top = "1%"
		completion_el.style.margin = "2%"

        completion_el.first_child.style.width = "100%"
        completion_el.first_child.style.height = "100%"

        --Make new title and text elements.. it's cheating a little to use the same IDs
        --as the existing ones but it saves us a lot of time manually adding styles here!
        local title_el = ScpuiSystem:makeElement(message, "div", "pilot_info_title")
        local category_el = ScpuiSystem:makeElement(message, "div", "pilot_stats_text")

        --A quick way to add completion lines
        local function add_value_element(parent, text, value)
            local text_container = message.Document:CreateElement("div")
            local text_el        = message.Document:CreateTextNode(text)
            text_container:AppendChild(text_el)
            text_container:SetClass("stats_value_label", true)

            local value_container = message.Document:CreateElement("div")
            local value_el        = message.Document:CreateTextNode(value)
            value_container:AppendChild(value_el)
            value_container:SetClass("stats_value_text", true)

            parent:AppendChild(text_container)
            parent:AppendChild(value_container)
        end

        --Create the Completion Title
        local title_p = ScpuiSystem:makeElement(message, "p")
        title_p:SetClass("h1", true)
        local completedPercentage = utils.compute_percentage(Completion:getTotalCompleted(), Completion:getTotal())
        local title_text = ba.XSTR("Total Completion: ", 9288) .. completedPercentage
        title_p.inner_rml = title_text
        title_el:AppendChild(title_p)

        --Create all the sub-completion bits
        local categories = Completion.categories

        for i, v in ipairs(categories) do
            add_value_element(category_el, ba.XSTR(v.name, v.xstr) .. ":", Completion:getNumInCategory(v.name) .. "/" .. Completion:getTotalInCategory(v.name))
        end

        completion_el.first_child:AppendChild(title_el)
        completion_el.first_child:AppendChild(category_el)
        panel_wrapper_el:AppendChild(completion_el)
		
		--Move the stats panel into the new container
		local stats_el = message.Document:GetElementById("pilot_info")
		panel_wrapper_el:AppendChild(stats_el)
		main_el:RemoveChild(stats_el)
		stats_el.style.width = "46%"
        stats_el.style.height = "auto"
        stats_el.style.position = "relative"
        stats_el.style.float = "right"
		stats_el.style.top = "1%"
		stats_el.style.margin = "2%"
    end)

    --Replaces the cutscene section selector with the custom barracks one
    topics.cutscenes.initialize:bind(1, function(message, context)
        local main_el = ScpuiSystem:getRootParent(message.Document)

        message.Document:GetElementById("top_panel_wrapper"):SetClass("hidden", true)

        --Cutscenes used the top panel for spacing, so we gotta recreate that now. Ugh.
        local spacer_el = message.Document:CreateElement("div")
        spacer_el.style.width = "100%"
        spacer_el.style.height = "15%"

        main_el:InsertBefore(spacer_el, main_el.first_child)

        local wrapper_el = makeBarracksSectionControls(message, 3)
        main_el:AppendChild(wrapper_el)
    end)

    --Replaces the simulator room section selector with the custom barracks one
    topics.simulator.initialize:bind(1, function(message, context)
        local main_el = ScpuiSystem:getRootParent(message.Document)

        message.Document:GetElementById("top_panel_wrapper"):SetClass("hidden", true)

        --Cutscenes used the top panel for spacing, so we gotta recreate that now. Ugh.
        local spacer_el = message.Document:CreateElement("div")
        spacer_el.style.width = "100%"
        spacer_el.style.height = "15%"

        main_el:InsertBefore(spacer_el, main_el.first_child)

        local wrapper_el = makeBarracksSectionControls(message, 2)
        main_el:AppendChild(wrapper_el)
    end)

end)()