-----------------------------------
--This file modifies the Nodemap progression function and overall layout
-----------------------------------

(function()

    local topics = require('lib_ui_topics')

    --Forward the node map's progression function to our own
	topics.nodemap.progressionFunction:bind(1, function(message, context)
		local BtA = require("btafunctions")
		context.value = function(mission)
			return BtA:getGameProgress(mission)
		end
	end)

	--Redesign the UI before it initializes so the map gets properly scaled
	topics.nodemap.initialize:bind(1, function(message, context)
		---@type Document
		local doc = message.Document
		local bg_el = ScpuiSystem:getRootParent(message.Document)
		
		--Set the background
		bg_el:SetClass("node_map_bg", true)

		--Clean out the built-in map elements
		local og_map_el = doc:GetElementById("node_map_panel_wrapper")
		ScpuiSystem:clearEntries(og_map_el)
		bg_el:RemoveChild(og_map_el)

		--Create a background element that's not faded like the main background is
		local new_bg_el = ScpuiSystem:makeImg(message, "mapgrid.png", false)
		new_bg_el.style.width = "100%"
		new_bg_el.style.display = "block"
		new_bg_el.style.height = "100%"
		new_bg_el.style.ultrawide = "false"

		--Create a new map element
		local map_el = ScpuiSystem:makeElement(message, "div", "map_view")
		map_el.style["border-width"] = "0px"
		map_el.style["border-color"] = "rgba(0, 0, 0, 0)"
		map_el.style.top = "0"
		map_el.style.display = "block"
		map_el.style.position = nil
		map_el.style["margin"] = "auto"
		map_el.style.width = (gr.getCenterWidth() * 0.92) .. "px"

		--- Creates a styled toggle-style button panel
		--- @param message table The SCPUI context message
		--- @param id_suffix string Unique suffix for element IDs (e.g., "toggle" or "other")
		--- @param button_text string The label to show on the button
		--- @param width string The width of the button panel
		--- @param image_src string The oval image source
		--- @param on_click fun() The click callback function
		--- @return Element The top-level panel element to insert in the UI
		local function createButtonPanel(message, id_suffix, button_text, width, image_src, on_click)
			local panel_el = ScpuiSystem:makeElement(message, "div", id_suffix .. "_panel")
			panel_el.style.position = "absolute"
			panel_el.style.width = width

			local cont_el, button_el = ScpuiSystem:makeButton(
				message,
				id_suffix .. "_cont",
				"oval_btn",
				{"togglebutton", "button_1"},
				"oval",
				image_src,
				id_suffix .. "_text",
				{"pos", "button_text_right"},
				button_text
			)

			-- Oval image styling
			local img_span = button_el.first_child
			local img = img_span.first_child
			img_span:SetClass("togglebutton", true)
			img_span.style.width = "25%"
			img_span.style.height = "100%"
			img_span.style["margin-left"] = "5%"
			img.style.width = "100%"
			img.style.height = "100%"

			-- Text positioning
			local text_span = button_el.first_child.next_sibling
			local p = text_span.first_child
			p.style.width = "100%"
			p.style["margin-left"] = "35%"
			p.style["margin-top"] = "-75%"

			-- Position the container in top-left of its panel
			cont_el.style.position = "absolute"
			cont_el.style.top = "0"
			cont_el.style.left = "0"
			cont_el:AddEventListener("click", function(_, _, _) on_click() end)

			local panel_bg = ScpuiSystem:makeImg(message, "ops-panel-horz.png", false)
			panel_bg.style.width = "100%"
			panel_bg.style.height = "auto"

			panel_el:AppendChild(cont_el)
			panel_el:AppendChild(panel_bg)
			return panel_el
		end

		local toggle_panel = createButtonPanel(
			message,
			"toggle",
			ba.XSTR("Disable Node Lines", 11632),
			"18%",
			"oval.png",
			function() message:toggle_node_lines() end
		)
		toggle_panel.style.top = "0"
		toggle_panel.style.right = "0"
		bg_el:AppendChild(toggle_panel)

		local system_map_btn_el = createButtonPanel(
			message,
			"toggle",
			ba.XSTR("Zoom to Current Location", 11542),
			"24%",
			"oval.png",
			function() ScpuiSystem:beginSubstate("SystemMap") end
		)
		system_map_btn_el.style.bottom = "3%"
		system_map_btn_el.style.left = "37.5%"

		--Create a title element
		local title_el = ScpuiSystem:makeElement(message, "div", "map_title")
		title_el.style.position = "absolute"
		title_el.style.top = "1%"
		title_el.style.left = "1%"
		title_el.style.color = "white"
		title_el.style.width = "18%"
		title_el.style["white-space"] = "nowrap"
		title_el.style["overflow"] = "visible"
		title_el.inner_rml = ba.XSTR("Galaxy Node Map", 9299)
		title_el:SetClass("h1", true)

		--Append the new elements to the background element
		bg_el:InsertBefore(new_bg_el, doc:GetElementById("map_desc_wrapper"))
		bg_el:InsertBefore(map_el, doc:GetElementById("map_desc_wrapper"))
		bg_el:InsertBefore(toggle_panel, doc:GetElementById("map_desc_wrapper"))
		bg_el:InsertBefore(system_map_btn_el, doc:GetElementById("map_desc_wrapper"))
		bg_el:AppendChild(title_el)

		--Node map expects a few elements to exist so we can create them and just hide them
		local select_text_el = ScpuiSystem:makeElement(message, "div", "make_a_selection")
		select_text_el:SetClass("hidden", true)
		bg_el:AppendChild(select_text_el)

		--Hide everything else but the Exit button for now
		local description_el = doc:GetElementById("map_desc_wrapper")
		description_el:SetClass("hidden", true)
		local misc_buttons_el = doc:GetElementById("bottom_panel_b")
		misc_buttons_el:SetClass("hidden", true)

		--Increase the size of some rendered elements
		message.LineWidth = 1.8
		message.NodeDiameter = 5
		
		--Shrink the exit button a bit to match system map
		doc:GetElementById("bottom_panel_wrapper").style.width = "7%"
		doc:GetElementById("bottom_panel_a").style.width = "100%"
		doc:GetElementById("accept_cont").style.width = "85%"
		doc:GetElementById("accept_cont").first_child.style["margin-left"] = "4%"

		context.done = true

	end)

	-- When a system is selected, show the text in the breakout reader automatically
	topics.nodemap.selectEntry:bind(1, function(message, context)
		message:breakout_reader()
		message:deselectAll()
	end)

	-- Skip rendering the default map background and instead let it be transparent
	topics.nodemap.renderBackground:bind(100, function(self, context)
		gr.clearScreen(0,0,0,0)
		context.value = false -- Tell it not to render the default
	end)

	topics.nodemap.exit:bind(1, function(message, context)
		ba.postGameEvent(ba.GameEvents["GS_EVENT_TECH_MENU"])
	end)

	-- Cancel upstream keybinding
	topics.nodemap.keydown:bind(1, function(message, context)
		context.done = true
	end)

end)()