local _,ns = ...
ns.tips("专业界面解锁所有材料按钮")

--来源https://wago.io/0FXluFV22/8
local BP,BPT = false,false
local _,ns = ...
ns.ADDUIEVENT("ADDON_LOADED", function(addons)
if addons == "Blizzard_Professions" then BP = true end
if addons == "Blizzard_ProfessionsTemplates" then BPT = true end
if not BP or not BPT then return end

--解锁材料
--function ItemUtil.GetCraftingReagentCount(itemID)--内存飙升,改用AceHook-3.0
if not LibStub("AceAddon-3.0") or not LibStub("AceAddon-3.0",true) then return end
local AddUIHookCount = LibStub("AceAddon-3.0"):GetAddon("AddUIHookCount", true)
if not AddUIHookCount then
	AddUIHookCount = AddUIHookCount or LibStub("AceAddon-3.0"):NewAddon("AddUIHookCount", "AceHook-3.0")
end
function AddUIHookCount:GetCraftingReagentCountOverride()
	return 9999
end

local function showCheckBox(self)         
    local arb = _G["UnlockAllReagentsButton"]
    arb:SetParent(self)
    arb:ClearAllPoints()
    arb:SetPoint("BOTTOMLEFT", 30, 0)
    arb:Show()
end

if not InCombatLockdown() then
    local arb = _G["UnlockAllReagentsButton"] 
    or CreateFrame("CheckButton", "UnlockAllReagentsButton", nil, "UICheckButtonTemplate")
    
    arb:ClearAllPoints()
    arb:SetSize(26,26)
    arb.text:SetText(LIGHTGRAY_FONT_COLOR:WrapTextInColorCode("解锁所有材料以方便查看星级"))
    arb:Hide()
    
    arb:SetScript("OnClick", function(self)   
            self.count = (self:GetChecked() and (self.countValue or 9999)) or nil
			
			if self:GetChecked() then
				AddUIHookCount:RawHook(ItemUtil, "GetCraftingReagentCount", "GetCraftingReagentCountOverride",true)            
			else
				AddUIHookCount:Unhook(ItemUtil, "GetCraftingReagentCount")
			end
            
            local sf = self:GetParent()
            
            if sf then 
                if ProfessionsFrame.OrdersPage.OrderView:IsVisible() then
                    ProfessionsFrame.OrdersPage.OrderView:OnEvent("BAG_UPDATE")
                    
                else
                    sf:Refresh() 
                end
                
                local qd = sf.QualityDialog
                if qd and qd.recipeID then 
                    qd:Setup() 
                end    
            end            
        end
    )
    
    arb.countValue = 9999
    
    ProfessionsFrame.CraftingPage.SchematicForm:HookScript("OnShow", showCheckBox)
    ProfessionsFrame.OrdersPage.OrderView.OrderDetails.SchematicForm:HookScript("OnShow", showCheckBox)
    
end


end)