local _, ns = ...
local SlickUI = ns.SlickUI

local Theme = {}
SlickUI.Theme = Theme

Theme.Font = "Interface\\AddOns\\SlickUI\\Media\\Fonts\\Roboto.ttf"
Theme.ChatFont = "Interface\\AddOns\\SlickUI\\Media\\Fonts\\PTSans.ttf"
Theme.Texture = "Interface\\Buttons\\WHITE8X8"
Theme.ManagedFonts = setmetatable({}, { __mode = "k" })

Theme.Colors = {
	panel = { 0.035, 0.04, 0.055, 0.92 },
	panelSoft = { 0.09, 0.10, 0.13, 0.94 },
	border = { 1.00, 0.67, 0.12, 0.92 },
	borderMuted = { 0.34, 0.36, 0.42, 0.90 },
	health = { 0.10, 0.78, 0.60, 1.00 },
	hostile = { 0.92, 0.07, 0.045, 1.00 },
	neutral = { 1.00, 0.82, 0.10, 1.00 },
	power = { 0.12, 0.48, 0.92, 1.00 },
	cast = { 1.00, 0.62, 0.12, 1.00 },
	castInterruptible = { 0.16, 0.82, 0.38, 1.00 },
	castProtected = { 0.85, 0.16, 0.12, 1.00 },
	text = { 0.96, 0.97, 1.00, 1.00 },
	mutedText = { 0.68, 0.71, 0.78, 1.00 },
	goldText = { 1.00, 0.77, 0.24, 1.00 },
}

function Theme:GetFontScale()
	local settings = SlickUI.DB and SlickUI.DB.systemFonts
	return math.max(0.75, math.min(2.50, tonumber(settings and settings.slickUIScale) or 1))
end

function Theme:ApplyManagedFont(fontString, definition)
	if not fontString or not definition then return end
	local scaledSize = math.max(6, math.floor(definition.size * self:GetFontScale() + 0.5))
	fontString:SetFont(self.Font, scaledSize, definition.flags)
	fontString:SetShadowOffset(1, -1)
	fontString:SetShadowColor(0, 0, 0, 0.85)
end

function Theme:SetFont(fontString, size, flags)
	local definition = {
		size = tonumber(size) or 12,
		flags = flags or "OUTLINE",
	}
	self.ManagedFonts[fontString] = definition
	self:ApplyManagedFont(fontString, definition)
end

function Theme:RefreshFonts()
	for fontString, definition in pairs(self.ManagedFonts) do
		self:ApplyManagedFont(fontString, definition)
	end
end

local function CreateEdge(parent, pointA, pointB, width, height)
	local edge = parent:CreateTexture(nil, "BORDER", nil, 7)
	edge:SetColorTexture(unpack(Theme.Colors.border))
	edge:SetPoint(pointA)
	edge:SetPoint(pointB)
	if width then edge:SetWidth(width) end
	if height then edge:SetHeight(height) end
	return edge
end

function Theme:CreatePanel(frame)
	local background = frame:CreateTexture(nil, "BACKGROUND", nil, -8)
	background:SetAllPoints(frame)
	background:SetColorTexture(unpack(self.Colors.panel))

	frame.SlickBackground = background
	frame.SlickEdges = {
		CreateEdge(frame, "TOPLEFT", "TOPRIGHT", nil, 1),
		CreateEdge(frame, "BOTTOMLEFT", "BOTTOMRIGHT", nil, 1),
		CreateEdge(frame, "TOPLEFT", "BOTTOMLEFT", 1, nil),
		CreateEdge(frame, "TOPRIGHT", "BOTTOMRIGHT", 1, nil),
	}
end

function Theme:CreateBarBackground(bar)
	local background = bar:CreateTexture(nil, "BACKGROUND", nil, -7)
	background:SetAllPoints(bar)
	background:SetColorTexture(0.015, 0.018, 0.025, 0.92)
	bar.SlickBackground = background
end

function Theme:CreateText(parent, size, layer)
	local text = parent:CreateFontString(nil, layer or "OVERLAY")
	self:SetFont(text, size or 12)
	text:SetTextColor(unpack(self.Colors.text))
	return text
end
