
-------------------------------------
-- 包包顯示物品等級 Author: M
-------------------------------------

local _, LibItem = ...

local function SetContainerItemLevel(button, ItemLink)
    if (not button) then return end
    if (not button.levelString) then
        button.levelBorder = button:CreateTexture(nil, 'OVERLAY')
        button.levelBorder:SetSize(67, 67)
        button.levelBorder:SetPoint('CENTER')
        button.levelBorder:SetTexture([[Interface\Buttons\UI-ActionButton-Border]])
        button.levelBorder:SetBlendMode('ADD')
        button.levelBorder:Hide()
        button.levelString = button:CreateFontString(nil, "OVERLAY")
		button.levelString:SetFont(STANDARD_TEXT_FONT, 13, "OUTLINE")
		button.levelString:SetPoint("TOP")
        button.levelString:SetTextColor(1, 0.82, 0)
        button.slotString = button:CreateFontString(nil, "OVERLAY")
		button.slotString:SetFont(STANDARD_TEXT_FONT, 10, "OUTLINE")
		button.slotString:SetPoint("BOTTOMRIGHT")
        button.slotString:SetTextColor(1, 1, 1)
    end
    if (button.origItemLink ~= ItemLink) then
        button.origItemLink = ItemLink
    else
        return
    end
    if (ItemLink) then
        local count, level, _, _, quality, _, _, class, _, _, equipSlot = LibItem:GetItemInfoActually(ItemLink)
        if (count == 0 and level > 0) then
            button.levelString:SetText(level)
        else
            button.levelString:SetText("")
        end
        if (quality and quality > 1) then
            local r, g, b, hex = GetItemQualityColor(quality)
--            button.levelString:SetTextColor(r, g, b)
            button.levelBorder:SetVertexColor(r, g, b, 0.4)
            button.levelBorder:Show()
        else
            button.levelBorder:Hide()
        end
        if (equipSlot and string.find(equipSlot, "INVTYPE_")) then
            button.slotString:SetText(_G[equipSlot] or "")
        elseif (class == ARMOR or class == WEAPON or class == AUCTION_CATEGORY_GEMS) then
            button.slotString:SetText(class)
        else
            button.slotString:SetText("")
        end
    else
        button.levelString:SetText("")
        button.levelBorder:Hide()
        button.slotString:SetText("")
    end
end

-- 背包
hooksecurefunc("ContainerFrame_Update", function(self)
    local id = self:GetID()
    local name = self:GetName()
    local button
    for i = 1, self.size do
        button = _G[name.."Item"..i]
        SetContainerItemLevel(button, GetContainerItemLink(id, button:GetID()))
    end
end)

-- 银行
hooksecurefunc("BankFrameItemButton_Update", function(self)
    SetContainerItemLevel(self, GetContainerItemLink(self:GetParent():GetID(), self:GetID()))
end)


-- For Bagnon
if (Bagnon and Bagnon.ItemSlot) then
    local origFunc = Bagnon.ItemSlot.DelayedUpdates
    function Bagnon.ItemSlot:DelayedUpdates()
        origFunc(self)
        SetContainerItemLevel(self, self:GetItem())
    end
end

-- For Combuctor
if (Combuctor and Combuctor.ItemSlot) then
    local origFunc = Combuctor.ItemSlot.Update
    function Combuctor.ItemSlot:Update()
        origFunc(self)
        SetContainerItemLevel(self, self:GetItem())
    end
end

-- For LiteBag
if (LiteBagItemButton_UpdateItem) then
    hooksecurefunc("LiteBagItemButton_UpdateItem", function(self)
        SetContainerItemLevel(self, GetContainerItemLink(self:GetParent():GetID(), self:GetID()))
    end)
end
