local _,ns = ...
ns.tips("按alt一次买一组")
local savedMerchantItemButton_OnModifiedClick = MerchantItemButton_OnModifiedClick 
function MerchantItemButton_OnModifiedClick(self, ...) 
   if ( IsAltKeyDown() ) then 
      local itemLink = GetMerchantItemLink(self:GetID()) 
      if not itemLink then return end 
      local maxStack = select(8, C_Item.GetItemInfo(itemLink)) 
      if ( maxStack and maxStack > 1 ) then 
         BuyMerchantItem(self:GetID(), GetMerchantItemMaxStack(self:GetID())) 
      end 
   end 
   savedMerchantItemButton_OnModifiedClick(self, ...) 
end

--拍卖行保存过滤AHFilterRestoreDB
local AHDB = CreateFrame("frame")
AHDB:RegisterEvent('AUCTION_HOUSE_SHOW')
AHDB:SetScript("OnEvent", function(self, event)
	AddUIDB.AHFilterRestoreDB = AddUIDB.AHFilterRestoreDB or {}
	--设置拍卖行
	AuctionHouseFrame.SearchBar.FilterButton.filters = next(AddUIDB.AHFilterRestoreDB) and AddUIDB.AHFilterRestoreDB or
	CopyTable(AUCTION_HOUSE_DEFAULT_FILTERS)
	AuctionHouseFrame.SearchBar:UpdateClearFiltersButton()
	--设置hook
	if self.hooked then return end
	hooksecurefunc(AuctionHouseFrame.SearchBar, "OnFilterToggled", function()
		local newFilter = AuctionHouseFrame.SearchBar.FilterButton:GetFilters()
		AddUIDB.AHFilterRestoreDB = newFilter
	end)
	
	AuctionHouseFrame.SearchBar.FilterButton.ClearFiltersButton:SetScript("OnClick", function()
		AuctionHouseFrame.SearchBar.FilterButton.filters = CopyTable(AUCTION_HOUSE_DEFAULT_FILTERS)
		AuctionHouseFrame.SearchBar.FilterButton.minLevel = 0
		AuctionHouseFrame.SearchBar.FilterButton.maxLevel = 0
		AuctionHouseFrame.SearchBar.FilterButton.ClearFiltersButton:Hide()
		wipe(AddUIDB.AHFilterRestoreDB)
	end)
	
	self.hooked = true
end)


----改名
StaticPopupDialogs.SC_NAME = { 
        text = "输入你要改的名字,不输入为恢复原名", 
        button1 = "确定并重载界面", 
        button2 = CANCEL, 
		--功能调用
        --OnAccept =  function() end, 
		OnAccept = function (self, data, data2)
		AddUIDB.Gname = self.EditBox:GetText()
		ReloadUI()
		end,
        timeout = 0, 
        whileDead = 1, 
        hideOnEscape = true, 
		hasEditBox = true,
        preferredIndex = 5, 
}
SLASH_SCNAME1 = "/gn" 
SLASH_SCNAME2 = "/name" 
SLASH_SCNAME3 = "/改名" 
SlashCmdList["SCNAME"] = function() 
        StaticPopup_Show("SC_NAME") 
end


local Gname = CreateFrame("Frame")
Gname:RegisterEvent("ADDON_LOADED")
Gname:SetScript("OnEvent", function(self, event)
if AddUIDB.Gname == nil then return end
if AddUIDB.Gname == ""  then return end
local changedName = AddUIDB.Gname


local playerName = UnitName("player")
local UnitNameStore = UnitName
local function GetUnitName(unit)
	local realname,Server = UnitNameStore(unit)
	if realname == playerName then
		return changedName,Server
	else
		return realname,Server
	end
end
UnitName = GetUnitName

local playercnClass, playerusClass ,playerClassid = UnitClass("player")
local UnitClassStore = UnitClass
local function GetUnitClass(unit)
	local cnclass,usclass,classid= UnitClassStore(unit)
	if usclass == nil then
		return playercnClass, playerusClass ,playerClassid
	else
		return cnclass,usclass,classid
	end
end
UnitClass = GetUnitClass
--[[
--头像相关
PlayerName:SetText(changedName)
local function FrameName(self)
	if UnitIsUnit("player",self.unit) then
		if self.TargetFrameContent then
			self.TargetFrameContent.TargetFrameContentMain.Name:SetText(changedName)
		elseif self.HealthBar then
			if not self.Name then return end
			self.Name:SetText(changedName)
		end
	end
end
hooksecurefunc(TargetFrame, "CheckClassification", FrameName)
hooksecurefunc(FocusFrame, "CheckClassification", FrameName)
hooksecurefunc(TargetFrameToT, "Update", FrameName)
hooksecurefunc(FocusFrameToT, "Update", FrameName)
--团队框架
hooksecurefunc("CompactUnitFrame_UpdateName", function(frame)
	if frame:IsForbidden() then return end
	if not frame.name then return end
	if not frame.unit then return end
	--if UnitIsUnit("player",frame.unit) then
	--	frame.name:SetText(changedName)
	--end
	frame.name:SetText(UnitClass(frame.unit))
end)]]
--角色面板
CharacterFrame:HookScript("OnShow", function(self)
	C_Timer.After(0,function()
		CharacterFrameTitleText:SetText(changedName)
	end)
	if C_AddOns.IsAddOnLoaded("TinyInspect") then
		PaperDollFrame.inspectFrame.title:SetText(changedName)
	end
end)
--鼠标提示
local function mynamechange()
	local _, unit = GameTooltip:GetUnit()
	if unit and UnitIsUnit(unit,"player") then
		GameTooltipTextLeft1:SetText(changedName)
	end
end
TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Unit, mynamechange)
--details
local NicknameDetails = changedName
if C_AddOns.IsAddOnLoaded("Details") then
	local base = Details.container_combatentes
	local old = base.GetOrCreateActor
	base.GetOrCreateActor = function (self, actorSerial, actorName, actorFlags, bShouldCreateActor)
		local newActor, arg2, arg3 = old(self, actorSerial, actorName, actorFlags, bShouldCreateActor)
		if NicknameDetails then newActor.displayName = NicknameDetails end
		return newActor, arg2, arg3
	end
end

end)
--BlizzardInterfaceCode\Interface\FrameXML\UIParent
ns.tips("让拍卖行和专业界面可以同时打开")
function CanShowRightUIPanel(frame)
	local width = frame and GetUIPanelWidth(frame) or UIParent:GetAttribute("DEFAULT_FRAME_WIDTH");
	local rightSide = UIParent:GetAttribute("RIGHT_OFFSET") + width;
	if frame == AuctionHouseFrame or frame == ProfessionsFrame then
		return true
	else
		return rightSide <= GetMaxUIPanelsWidth();
	end
end