local MER, F, E, I, V, P, G, L = unpack(ElvUI_MerathilisUI)
local module = MER:GetModule("MER_Skins")
local S = E:GetModule("Skins")

local _G = _G

local function moveNavButtons(self)
	local width = 0
	local collapsedWidth
	local maxWidth = self:GetWidth() - self.widthBuffer

	local lastShown
	local collapsed = false

	for i = #self.navList, 1, -1 do
		local currentWidth = width
		width = width + self.navList[i]:GetWidth()

		if width > maxWidth then
			collapsed = true
			if not collapsedWidth then
				collapsedWidth = currentWidth
			end
		else
			if lastShown then
				self.navList[lastShown]:SetPoint("LEFT", self.navList[i], "RIGHT", 1, 0)
			end
			lastShown = i
		end
	end

	if collapsed then
		if collapsedWidth + self.overflowButton:GetWidth() > maxWidth then
			lastShown = lastShown + 1
		end

		if lastShown then
			local lastButton = self.navList[lastShown]

			if lastButton then
				lastButton:SetPoint("LEFT", self.overflowButton, "RIGHT", 1, 0)
			end
		end
	end
end

local function handleNavButton(navButton)
	if navButton.restyled then
		return
	end

	S:HandleButton(navButton)
	navButton.arrowUp:SetAlpha(0)
	navButton.arrowDown:SetAlpha(0)
	navButton.selected:SetDrawLayer("BACKGROUND", 1)
	navButton.selected:SetColorTexture(F.r, F.g, F.b, 0.25)
	navButton.selected:SetInside()

	navButton:HookScript("OnClick", function()
		moveNavButtons(navButton:GetParent())
	end)

	-- arrow button
	local arrowButton = navButton.MenuArrowButton
	arrowButton:StripTextures()
	arrowButton.Art:Hide()
	arrowButton:SetHighlightTexture(0)

	local tex = arrowButton:CreateTexture(nil, "ARTWORK")
	S:SetupArrow(tex, "down")
	tex:SetSize(14, 14)
	tex:SetPoint("CENTER")
	arrowButton.__texture = tex

	navButton.restyled = true
end

function module:NavigationBar()
	-- hooksecurefunc("NavBar_Initialize", F.ReskinNavBar) -- monitor this

	hooksecurefunc("NavBar_AddButton", function(self)
		F:ReskinNavBar(self)
		handleNavButton(self.navList[#self.navList])
		moveNavButtons(self)
	end)

	-- Update navbar on WorldMap
	F:ReskinNavBar(WorldMapFrame.NavBar)
	local navList = WorldMapFrame.NavBar.navList
	for i = 2, #navList do
		handleNavButton(navList[i])
	end
	moveNavButtons(WorldMapFrame.NavBar)
end

module:AddCallback("NavigationBar")
