-- Transmoggin - Copyright (c) 2026 Rooster_Roost. All Rights Reserved.
-- Proprietary software. Personal non-commercial use only; redistribution/repackaging/forks/reuse require permission.
-- Full license: Core.lua and LICENSE.txt

local T=Transmoggin
local C=T.COLORS
local function rgba(t) return t[1],t[2],t[3],t[4] end
local function frame(type,name,parent,template)
    if BackdropTemplateMixin and not template then template="BackdropTemplate" end
    return CreateFrame(type,name,parent,template)
end
local function skin(f,bg,border)
    if not f.SetBackdrop then return end
    f:SetBackdrop({bgFile="Interface\\Buttons\\WHITE8X8",edgeFile="Interface\\Buttons\\WHITE8X8",edgeSize=1})
    f:SetBackdropColor(rgba(bg)); f:SetBackdropBorderColor(rgba(border))
end
local function text(p,size,color,font)
    local fs=p:CreateFontString(nil,"OVERLAY",font or "GameFontNormal"); fs:SetFont(STANDARD_TEXT_FONT,size,""); fs:SetTextColor(rgba(color)); return fs
end

function T:CreateTracker()
    local f=frame("Frame","TransmogginTracker",UIParent); self.tracker=f
    f:SetSize(430,460); f:SetPoint("RIGHT",-40,30); f:SetFrameStrata("DIALOG"); f:SetClampedToScreen(true); f:SetMovable(true); f:EnableMouse(true); f:RegisterForDrag("LeftButton")
    f:SetScript("OnDragStart",f.StartMoving); f:SetScript("OnDragStop",f.StopMovingOrSizing); skin(f,C.bg,C.gold2); f:Hide()
    local logo=f:CreateTexture(nil,"ARTWORK"); logo:SetTexture(self.ROOSTER); logo:SetSize(42,42); logo:SetPoint("TOPLEFT",8,-8)
    local title=text(f,16,C.gold); title:SetPoint("TOPLEFT",56,-13); title:SetText("TRANSMOGGIN TRACKER")
    local sub=text(f,11,C.muted); sub:SetPoint("TOPLEFT",56,-33); sub:SetText("Missing pieces from tracked looks")
    local x=CreateFrame("Button",nil,f,"UIPanelCloseButton"); x:SetPoint("TOPRIGHT",0,0)

    local scroll=CreateFrame("ScrollFrame",nil,f,"UIPanelScrollFrameTemplate"); f.scroll=scroll
    scroll:SetPoint("TOPLEFT",10,-58); scroll:SetPoint("BOTTOMRIGHT",-30,12)
    local content=CreateFrame("Frame",nil,scroll); f.content=content
    content:SetSize(382,1); scroll:SetScrollChild(content)
    f.rows={}
    for i=1,40 do
        local r=frame("Button",nil,content); r:SetSize(374,48); r:SetPoint("TOPLEFT",2,-(i-1)*50); skin(r,C.panel,C.gold2)
        r.icon=r:CreateTexture(nil,"ARTWORK"); r.icon:SetSize(40,40); r.icon:SetPoint("LEFT",4,0)
        r.name=text(r,11,C.text); r.name:SetPoint("TOPLEFT",50,-6); r.name:SetPoint("RIGHT",-6,0); r.name:SetHeight(14); r.name:SetJustifyH("LEFT"); r.name:SetWordWrap(false)
        r.where=text(r,10,C.gold); r.where:SetPoint("BOTTOMLEFT",50,6); r.where:SetPoint("RIGHT",-6,0); r.where:SetHeight(14); r.where:SetJustifyH("LEFT"); r.where:SetWordWrap(false)
        r:SetScript("OnEnter",function(self) if self.piece and T:TooltipsEnabled() then T:ShowAppearanceTooltip(self,self.piece) end end)
        r:SetScript("OnLeave",function() GameTooltip:Hide() end)
        f.rows[i]=r
    end
    f.empty=text(content,12,C.muted); f.empty:SetPoint("TOP",0,-120); f.empty:SetText("No missing tracked pieces.\nSave a look and click Track."); f.empty:SetJustifyH("CENTER")
    scroll:EnableMouseWheel(true)
    scroll:SetScript("OnMouseWheel",function(self,delta)
        local step=100; local max=math.max(0,(content:GetHeight() or 0)-self:GetHeight())
        local v=math.max(0,math.min(max,(self:GetVerticalScroll() or 0)-delta*step)); self:SetVerticalScroll(v)
    end)
end

function T:RefreshTracker()
    if not self.tracker then return end
    local list={}
    for _,look in ipairs(self.db.looks or {}) do
        if self.db.tracked[look.name] then
            for slot,p in pairs(look.pieces or {}) do
                if not self:IsPieceCollected(p) then list[#list+1]={look=look,piece=p,slot=slot} end
            end
        end
    end
    table.sort(list,function(a,b)
        local an=(a.look.name or "")..(a.piece.itemName or a.piece.name or "")
        local bn=(b.look.name or "")..(b.piece.itemName or b.piece.name or "")
        return an<bn
    end)
    local visible=math.min(#list,#self.tracker.rows)
    for i,r in ipairs(self.tracker.rows) do
        local e=list[i]
        if e then
            r.piece=e.piece; r.icon:SetTexture(e.piece.icon or 134400)
            r.name:SetText((e.look.name or "Look").."  -  "..((e.piece.itemName or e.piece.name) or "Piece"))
            local src=(self:EnsureSourceText(e.piece) or "Source unknown"):gsub("\n","  /  ")
            r.where:SetText(src)
            r:Show()
        else r.piece=nil; r:Hide() end
    end
    self.tracker.content:SetHeight(math.max(self.tracker.scroll:GetHeight(),visible*50+4))
    self.tracker.empty:SetShown(#list==0)
    if self.tracker.scroll.UpdateScrollChildRect then self.tracker.scroll:UpdateScrollChildRect() end
end
function T:ToggleTracker() if not self.tracker then return end; if self.tracker:IsShown() then self.tracker:Hide() else self:RefreshTracker(); self.tracker:Show() end end
