-- shapes.lua - Minimap shape masks for SexyMap OctoWoW Edition
-- Provides CApplyShape() which clips the minimap to a given shape
--
-- IMPORTANT: We use Minimap:SetMaskTexture() instead of creating overlay textures.
-- Creating an overlay texture on top of the minimap blocks the map content and causes
-- the minimap to appear as pure white. SetMaskTexture clips the minimap to the shape
-- without covering it.
--
-- In WoW 1.12, SetMaskTexture(nil) does NOT remove the mask — it falls back to
-- the default round mask. This means there is no way to show a truly square
-- minimap via mask alone. For square shapes, we use the circle mask and rely
-- on the border/backdrop visuals to give a square appearance.

local _G = getfenv(0)

-- The default round minimap mask.
-- IMPORTANT: "Interface\Minimap\UI-Minimap-Background" is NOT a mask — it's a
-- decorative border texture. Using it with SetMaskTexture causes green rendering.
-- The correct default mask is the circle.blp we ship with the addon.
local DEFAULT_MASK = "Interface\\AddOns\\SexyMap\\shapes\\circle.blp"

-- Shape texture paths (addon-local BLP files in shapes/ directory)
-- All paths include .blp because SetMaskTexture does NOT auto-append .blp
-- (unlike SetBackdrop which does auto-append it).
local shapeTextures = {
	["Round"]            = "Interface\\AddOns\\SexyMap\\shapes\\circle.blp",
	["Square"]           = "Square",  -- special: no mask change, keep default round
	["Diamond"]          = "Interface\\AddOns\\SexyMap\\shapes\\diamond.blp",
	["Hexagon"]          = "Interface\\AddOns\\SexyMap\\shapes\\hexagon.blp",
	["Octagon"]          = "Interface\\AddOns\\SexyMap\\shapes\\octagon.blp",
	["Heart"]            = "Interface\\AddOns\\SexyMap\\shapes\\heart.blp",
	["Snowflake"]        = "Interface\\AddOns\\SexyMap\\shapes\\circle.blp",
	["LL Corner Rounded"] = "Interface\\AddOns\\SexyMap\\shapes\\bottomleft.blp",
	["LR Corner Rounded"] = "Interface\\AddOns\\SexyMap\\shapes\\bottomright.blp",
	["UL Corner Rounded"] = "Interface\\AddOns\\SexyMap\\shapes\\circle.blp", -- fallback, no topleft BLP
	["UR Corner Rounded"] = "Interface\\AddOns\\SexyMap\\shapes\\circle.blp", -- fallback, no topright BLP
	["Large Circle"]     = "Interface\\AddOns\\SexyMap\\shapes\\largecircle.blp",
	["Route66"]          = "Interface\\AddOns\\SexyMap\\shapes\\circle.blp",
	["Deathsky"]         = "Interface\\AddOns\\SexyMap\\shapes\\deathsky_mask.blp",
	["VFX Border"]       = "Interface\\AddOns\\SexyMap\\shapes\\circle.blp",
	["Left"]             = "Interface\\AddOns\\SexyMap\\shapes\\left.blp",
	["Bottom"]           = "Interface\\AddOns\\SexyMap\\shapes\\bottom.blp",
}

function CApplyShape(shapeName)
	if not shapeName then return end

	local texPath = shapeTextures[shapeName]

	if not texPath then
		-- Unknown shape: apply default round mask
		Minimap:SetMaskTexture(DEFAULT_MASK)
		return
	end

	-- Square shape: keep the default round mask.
	-- WoW 1.12 does not support removing the mask entirely (SetMaskTexture(nil)
	-- just resets to the default round mask). The square appearance for presets
	-- like "Ruins" and "Simple Square" comes from their border textures and
	-- backdrop frame, not from the minimap mask shape.
	if texPath == "Square" then
		Minimap:SetMaskTexture(DEFAULT_MASK)
		return
	end

	-- Use SetMaskTexture to clip the minimap to the shape.
	-- pcall protects against invalid texture paths
	local ok, err = pcall(function()
		Minimap:SetMaskTexture(texPath)
	end)
	if not ok then
		-- Fallback to default round mask if the texture failed to load
		Minimap:SetMaskTexture(DEFAULT_MASK)
	end
end
