-- Portal Pro :: UI/CompactBar.lua
-- The slim always-on strip. This is what most people will actually run day to day, so
-- it's a first-class screen rather than a shrunken main window.

local ADDON_NAME, PP = ...
local UI, T, U = PP.UI, PP.Theme, PP.U
local L = PP.L

local Bar = PP:NewModule("CompactBar")
PP.CompactBar = Bar

local frame

local function chip(parent, labelText, width)
	local c = CreateFrame("Frame", nil, parent)
	c:SetSize(width or 76, 30)

	local label = UI:Label(c, labelText)
	label:SetPoint("TOPLEFT")

	local value = UI:Text(c, "-", T.fontSize.base, "", "textPrimary")
	value:SetPoint("BOTTOMLEFT")

	c.valueText = value
	function c:Set(text, colorName)
		self.valueText:SetText(text)
		self.valueText:SetTextColor(T:C(colorName or "textPrimary"))
	end
	return c
end

local function build()
	if frame then return frame end

	frame = UI:Panel(UIParent, "raised")
	-- Widened to fit a full g/s/c net figure without collapsing it to one rounded unit.
	frame:SetSize(474, 40)
	frame:SetFrameStrata("MEDIUM")
	frame:Hide()
	UI:Border(frame, "borderStrong")
	UI:Shadow(frame, 4)
	frame:SetScale(PP.profile.ui.scale or 1)

	UI:RestorePosition(frame, PP.profile.ui, "compactPoint",
		{ "TOP", "UIParent", "TOP", 0, -80 })
	UI:MakeDraggable(frame, frame, function(f)
		UI:SavePosition(f, PP.profile.ui, "compactPoint")
	end)

	local logo = frame:CreateTexture(nil, "ARTWORK")
	logo:SetSize(18, 18)
	logo:SetPoint("LEFT", T.space.sm, 0)
	logo:SetTexture("Interface\\ICONS\\Spell_Arcane_PortalStormWind")
	logo:SetTexCoord(0.08, 0.92, 0.08, 0.92)

	frame.leads = chip(frame, L["Leads"], 46)
	frame.leads:SetPoint("LEFT", logo, "RIGHT", T.space.md, 0)

	frame.net = chip(frame, L["Net"], 120)
	frame.net:SetPoint("LEFT", frame.leads, "RIGHT", T.space.md, 0)

	frame.portals = chip(frame, L["Portals"], 46)
	frame.portals:SetPoint("LEFT", frame.net, "RIGHT", T.space.md, 0)

	frame.runes = chip(frame, L["Runes"], 40)
	frame.runes:SetPoint("LEFT", frame.portals, "RIGHT", T.space.md, 0)

	local expand = UI:Button(frame, "[ ]", function()
		PP.profile.ui.compact = false
		Bar:Hide()
		UI:Show()
	end, "ghost", 26, 26)
	expand:SetPoint("RIGHT", -T.space.xs, 0)
	UI:Tooltip(expand, "Open full window")

	local broadcast = UI:Button(frame, L["Broadcast"], function(self, button)
		if button == "RightButton" then
			PP.Broadcaster:ToggleArm()
		else
			PP.Broadcaster:Broadcast()
		end
	end, "primary", 96, 26)
	broadcast:RegisterForClicks("LeftButtonUp", "RightButtonUp")
	broadcast:SetPoint("RIGHT", expand, "LEFT", -T.space.sm, 0)
	frame.broadcastButton = broadcast

	local cdFill = frame:CreateTexture(nil, "OVERLAY")
	cdFill:SetHeight(2)
	cdFill:SetPoint("BOTTOMLEFT", broadcast, "BOTTOMLEFT", 0, -2)
	cdFill:SetColorTexture(T:C("accent"))
	frame.cdFill = cdFill
	frame.cdWidth = 96

	return frame
end

function Bar:Show()
	build()
	frame:Show()
	self:Refresh()
end

function Bar:Hide()
	if frame then frame:Hide() end
end

function Bar:IsShown()
	return frame and frame:IsShown()
end

function Bar:Refresh()
	if not frame or not frame:IsShown() then return end

	local count = PP.Leads:Count()
	frame.leads:Set(tostring(count), count > 0 and "accent" or "textMuted")

	local totals = PP.Stats:Totals("session")
	frame.net:Set(U.MoneyFull(totals.net, true), totals.net >= 0 and "positive" or "negative")

	local portals, teleports = PP.CastTracker:CountSince(PP.Stats:SessionStart())
	frame.portals:Set(tostring(portals + teleports))

	local runes = PP.Reagents:PortalCount()
	frame.runes:Set(tostring(runes),
		runes <= (PP.profile.reagents.lowWarning or 5) and "warning" or "textPrimary")

	local remaining, throttle = PP.Broadcaster:Cooldown()
	if PP.Broadcaster:IsArmed() then
		frame.broadcastButton:SetTextValue(L["ARMED"])
		frame.cdFill:SetWidth(frame.cdWidth)
		frame.cdFill:SetColorTexture(T:C("warning"))
	elseif remaining > 0 then
		frame.broadcastButton:SetTextValue(("%ds"):format(math.ceil(remaining)))
		frame.cdFill:SetWidth(math.max(1, frame.cdWidth * (1 - remaining / math.max(1, throttle))))
		frame.cdFill:SetColorTexture(T:C("accent"))
	else
		frame.broadcastButton:SetTextValue(L["Broadcast"])
		frame.cdFill:SetWidth(frame.cdWidth)
		frame.cdFill:SetColorTexture(T:C("positive"))
	end
end

function Bar:OnEnable()
	C_Timer.NewTicker(0.25, function() Bar:Refresh() end)
	PP:On("LEADS_CHANGED", function() Bar:Refresh() end)
	PP:On("LEDGER_CHANGED", function() Bar:Refresh() end)
end
