local _,RPAL=...

-- Authored platform renderer. Every style owns a different transparent mask;
-- there are no action-button borders, square caps, or shared procedural parts.
local TEX="Interface\\AddOns\\ODYN\\Textures\\Cosmetics\\Platforms\\"

local function Clamp(value,minimum,maximum)
    if value<minimum then return minimum end
    if value>maximum then return maximum end
    return value
end

local function Anchor(texture,holder,x,y,width,height)
    texture:ClearAllPoints();texture:SetPoint("CENTER",holder,"CENTER",x or 0,y or 0)
    texture:SetSize(width,height)
end

function RPAL.RenderProfilePlatform(parent,name,tint,width,height,animated)
    if not parent then return nil end
    name=name or "Default"
    local styles=RPAL.PROFILE_PLATFORM_STYLES or {}
    local definition=styles[name] or styles.Default or {asset="default",mode="classic"}
    tint=tint or {0.92,0.72,0.34,1}
    width=tonumber(width) or 330
    height=tonumber(height) or 74
    local primary=definition.color or tint
    local secondary=definition.secondary or {0.96,0.92,0.82,1}
    local asset=tostring(definition.asset or "default")
    local mode=definition.mode or "classic"

    local holder=CreateFrame("Frame",nil,parent)
    holder:SetSize(width,height);holder:EnableMouse(false);holder:SetClipsChildren(false)
    holder._rpalPlatformName=name;holder._rpalPlatformAsset=asset;holder._rpalPlatformVersion=2

    local function Texture(layer,sublevel,path,blend,color,alpha)
        local texture=holder:CreateTexture(nil,layer,nil,sublevel)
        texture:SetTexture(path);texture:SetPoint("CENTER");texture:SetSize(width,height)
        if blend then texture:SetBlendMode(blend) end
        color=color or primary
        texture:SetVertexColor(color[1] or 1,color[2] or 1,color[3] or 1,alpha or 1)
        if texture.SetSnapToPixelGrid then texture:SetSnapToPixelGrid(false) end
        if texture.SetTexelSnappingBias then texture:SetTexelSnappingBias(0) end
        return texture
    end

    local shadow=Texture("BACKGROUND",-6,TEX.."platform_shadow.tga",nil,{0.01,0.012,0.018,1},.88)
    Anchor(shadow,holder,0,-height*.10,width*1.06,height*.96)
    local underglow=Texture("BACKGROUND",-5,TEX.."platform_shadow.tga","ADD",primary,.20)
    Anchor(underglow,holder,0,-height*.055,width*1.03,height*.92)
    local primaryArt=Texture("ARTWORK",0,TEX.."platform_"..asset.."_primary.tga","ADD",primary,.86)
    local accentArt=Texture("OVERLAY",1,TEX.."platform_"..asset.."_accent.tga","ADD",secondary,.86)
    local bloom=Texture("BACKGROUND",-4,TEX.."platform_"..asset.."_accent.tga","ADD",primary,.13)
    Anchor(bloom,holder,0,0,width*1.055,height*1.055)

    holder._rpalPlatformPrimary=primaryArt
    holder._rpalPlatformAccent=accentArt
    holder._rpalPlatformBloom=bloom

    if animated~=false then
        local phase,pending=0,0
        holder:SetScript("OnUpdate",function(self,elapsed)
            if not self:IsShown() then return end
            pending=pending+(tonumber(elapsed) or 0)
            if pending<(1/30) then return end
            phase=phase+pending;pending=0
            local pulse=(math.sin(phase*1.15)+1)*.5
            local p2=(math.sin(phase*2.05)+1)*.5
            local x1,y1,sx1,sy1=0,0,1,1
            local x2,y2,sx2,sy2=0,0,1,1
            local primaryAlpha,accentAlpha=.76,.76

            if mode=="classic" then
                sy1=1+(pulse*.035);sx2=1+(pulse*.018)
                primaryAlpha=.70+(pulse*.16);accentAlpha=.62+(pulse*.24)
            elseif mode=="runes" then
                local rune=math.sin(phase*2.8);rune=rune*rune
                sx2=1+(rune*.025);sy2=1+(rune*.055)
                accentAlpha=.40+(rune*.58);primaryAlpha=.72+(pulse*.13)
            elseif mode=="arcane" then
                local converge=math.sin(phase*1.35)
                x1=converge*width*.010;x2=-converge*width*.014
                sx1=1+(pulse*.025);sx2=1-(pulse*.020)
                accentAlpha=.58+(p2*.34);primaryAlpha=.66+(pulse*.20)
            elseif mode=="ember" then
                local flicker=math.abs(math.sin(phase*4.4))*.65+math.abs(math.sin(phase*7.1))*.35
                y2=flicker*height*.035;sy2=1+(flicker*.09)
                accentAlpha=.42+(flicker*.56);primaryAlpha=.68+(pulse*.17)
            elseif mode=="frost" then
                local shimmer=(math.sin(phase*2.4)+1)*.5
                x2=(shimmer-.5)*width*.012;sy2=1+(shimmer*.04)
                accentAlpha=.46+(shimmer*.48);primaryAlpha=.70+(p2*.14)
            elseif mode=="void" then
                local eclipse=(math.cos(phase*1.08)+1)*.5
                sx1=.98+(eclipse*.035);sx2=1.025-(eclipse*.035)
                x1=(eclipse-.5)*width*.010;x2=-x1
                primaryAlpha=.48+(eclipse*.38);accentAlpha=.78-(eclipse*.32)
            elseif mode=="moon" then
                local drift=math.sin(phase*.86)
                y1=drift*height*.018;y2=-drift*height*.025
                sx2=1+(pulse*.045);sy2=1+(pulse*.070)
                accentAlpha=.54+(pulse*.38);primaryAlpha=.68+(p2*.16)
            end

            Anchor(primaryArt,holder,x1,y1,width*sx1,height*sy1)
            Anchor(accentArt,holder,x2,y2,width*sx2,height*sy2)
            primaryArt:SetAlpha(Clamp(primaryAlpha,0,1));accentArt:SetAlpha(Clamp(accentAlpha,0,1))
            underglow:SetAlpha(.12+(pulse*.14));bloom:SetAlpha(.07+(accentAlpha*.12))
        end)
    end
    return holder
end

