-- EllesmereUI_koKR_Plus / Inspect.lua
-- 살펴보기 창(InspectFrame) 배율/슬롯 등급. 지연 로드 프레임 — 로드 후 후크.
---@diagnostic disable: undefined-global
local NS = _G.EUIKorPlus
if not NS then return end
local GetDB = NS.GetDB
local GetFont, SolidTex, MakeFont, MakeDescFS = NS.GetFont, NS.SolidTex, NS.MakeFont, NS.MakeDescFS
local MakeBorder, SetBorderColor, MakeStepBtn, MakeSwitch = NS.MakeBorder, NS.SetBorderColor, NS.MakeStepBtn, NS.MakeSwitchlocal CS_SCALE_MIN, CS_SCALE_MAX, CS_SCALE_STEP = NS.RANGE.CS[1], NS.RANGE.CS[2], NS.RANGE.CS[3]

local function ApplyInspectScale()
    local f = _G.InspectFrame
    if not f or not f.SetScale then return end
    local s = GetDB().inspectScale or 1.0
    if s < CS_SCALE_MIN then s = CS_SCALE_MIN end
    if s > CS_SCALE_MAX then s = CS_SCALE_MAX end
    pcall(f.SetScale, f, s)
end

local _isShown
local function _ISUpdate()
    local f = _G.InspectFrame
    if not f or not f:IsShown() then return end
    local unit = f.unit
    if not unit then return end
    if not (C_TooltipInfo and C_TooltipInfo.GetInventoryItem) then return end
    for _, sd in ipairs(_GEAR_SLOTS) do
        local btn = _G["Inspect" .. sd[1] .. "Slot"]
        if btn then
            local fs = _tierFSRefs[btn]
            if not fs then
                fs = btn:CreateFontString(nil, "OVERLAY")
                fs:SetFont(GetFont(), 11, "OUTLINE")
                fs:SetWordWrap(false)
                -- 살펴보기 슬롯은 보더가 있어 글자가 겹침 → 2px 더 내림(-4)
                fs:SetPoint("TOP", btn, "TOP", 0, -4)
                fs:SetDrawLayer("OVERLAY", 7)
                _tierFSRefs[btn] = fs
            end
            local trk
            local link = GetInventoryItemLink(unit, sd[2])
            if link then
                local ok, info = pcall(C_TooltipInfo.GetInventoryItem,
                    unit, sd[2])
                if ok and info and info.lines then
                    trk = _slotTrack(sd[2], info)
                end
            end
            if trk then
                fs:SetText(trk)
                local c = _TRACK_COLOR[trk]
                if c then fs:SetTextColor(c[1], c[2], c[3], 1)
                else fs:SetTextColor(1, 0.82, 0, 1) end
                fs:Show()
            else
                fs:Hide()
            end
            -- 보석 유무: 채워진 보석 아이콘을 슬롯 좌하단에 소형 표시
            -- (캐릭터창처럼). 이름은 표시 안 함(이전 요청).
            local gems = _gemTexRefs[btn]
            if not gems then
                gems = {}
                for gi = 1, 4 do
                    local g = btn:CreateTexture(nil, "OVERLAY")
                    g:SetDrawLayer("OVERLAY", 7)
                    g:SetSize(11, 11)
                    if gi == 1 then
                        g:SetPoint("BOTTOMLEFT", btn, "BOTTOMLEFT", 2, 2)
                    else
                        g:SetPoint("LEFT", gems[gi - 1], "RIGHT", 1, 0)
                    end
                    g:Hide()
                    gems[gi] = g
                end
                _gemTexRefs[btn] = gems
            end
            local ng = 0
            if link and C_Item and C_Item.GetItemGem then
                for gi = 1, 4 do
                    local okG, _, gemLink = pcall(C_Item.GetItemGem, link, gi)
                    if okG and gemLink then
                        local icon = C_Item.GetItemIconByID
                            and C_Item.GetItemIconByID(gemLink)
                        if not icon and GetItemInfoInstant then
                            icon = select(5, GetItemInfoInstant(gemLink))
                        end
                        ng = ng + 1
                        gems[ng]:SetTexture(icon or 134400)
                        gems[ng]:Show()
                    end
                end
            end
            for gi = ng + 1, 4 do gems[gi]:Hide() end
        end
    end
end

local _isHooked
local function _HookInspectFrame()
    local f = _G.InspectFrame
    if not f or _isHooked or not f.HookScript then return end
    _isHooked = true
    ApplyInspectScale()
    f:HookScript("OnShow", function()
        _isShown = true
        ApplyInspectScale()
        _ISUpdate()
        if C_Timer and C_Timer.After then
            C_Timer.After(0.3, _ISUpdate)
            C_Timer.After(0.8, _ISUpdate)
            C_Timer.After(1.6, _ISUpdate)
        end
    end)
    f:HookScript("OnHide", function() _isShown = false end)
    if f:IsShown() then _isShown = true; ApplyInspectScale(); _ISUpdate() end
end

local function _InstallInspect()
    _HookInspectFrame()                       -- 이미 로드됐으면 즉시
    if _isHooked then return end
    local ev = CreateFrame("Frame")
    ev:RegisterEvent("ADDON_LOADED")
    ev:RegisterEvent("INSPECT_READY")
    ev:SetScript("OnEvent", function(self, e, arg1)
        if e == "ADDON_LOADED" then
            if arg1 == "Blizzard_InspectUI" or _G.InspectFrame then
                _HookInspectFrame()
                if _isHooked then self:UnregisterEvent("ADDON_LOADED") end
            end
        else                                  -- INSPECT_READY
            _HookInspectFrame()
            if _isShown and C_Timer and C_Timer.After then
                C_Timer.After(0.1, _ISUpdate)
                C_Timer.After(0.5, _ISUpdate)
            end
        end
    end)
end

if NS.RegisterSection then
    NS.RegisterSection({ order = 70, col = 0, row = 3, title = "살펴보기 창 배율",
        desc = "다른 플레이어 살펴보기 창 크기. 0.5~2.0배.",
        build = function(popup, descFS)
            NS.MakeStepperRow(popup, descFS, {
                get = function() return GetDB().inspectScale or 1.0 end,
                set = function(v) GetDB().inspectScale = v end,
                fmt = function(v) return string.format("배율 %.2fx", v) end,
                min = CS_SCALE_MIN, max = CS_SCALE_MAX, step = CS_SCALE_STEP,
                onChange = function() pcall(ApplyInspectScale) end,
            })
        end })
end
if NS.RegisterInstall then
    NS.RegisterInstall(function() _InstallInspect() end)
end
