--11.0调整,修改自ReTradeTabs
local TradeTabs = CreateFrame("Frame","TradeTabs")

function TradeTabs:OnEvent(event,arg1,...)
  self:UnregisterEvent(event)
  if not IsLoggedIn() then
    self:RegisterEvent(event)
  elseif InCombatLockdown() then
    self:RegisterEvent("PLAYER_REGEN_ENABLED")
  else
    self:Initialize()
  end
end

TradeTabs:RegisterEvent("TRADE_SKILL_SHOW")
--TradeTabs:RegisterEvent("ADDON_LOADED")
TradeTabs:SetScript("OnEvent",TradeTabs.OnEvent)

local whitelist = {
	[129] = true, -- 急救 First Aid
	[164] = true, -- 煅造 Blacksmithing 
	[165] = true, -- 制皮 Leatherworking	
	
    [182] = true, -- 草药学 herbalism	
	[186] = true, -- 采矿 Mining	
	[202] = true, -- 工程 Engineering
	[171] = true, -- 炼金 Alchemy 	
	[333] = true, -- 附魔 Enchanting 
	[755] = true, -- 珠宝 Jewelcrafting
	[773] = true, -- 铭文 Inscription
	--[794] = true, -- 考古 Archaeology
	[356] = true, -- 钓鱼 Fishing
	[185] = true, -- 烹饪 Cooking 
	[197] = true, -- 裁缝 Tailoring
    [393] = true, -- 剥皮 skinning
}

local onlyPrimary = {
	[171] = true, --炼金 Alchemy
	[202] = true, --工程 Engineering
	[356] = true, -- 钓鱼 Fishing
}

local items = 67556       --大厨的帽子 134020
local RUNEFORGING = 53428 --DK的符文附魔 Runeforging spellid

local function buildSpellList()
  local p1, p2, arch, fishing, cooking = GetProfessions()
  local profs = {p1, p2, cooking, fishing}
  local tradeSpells = {}
  local extras = 0
  for _,prof in pairs(profs) do
    local name, icon, _, _, abilities, offset, skillLine = GetProfessionInfo(prof)
    if whitelist[skillLine] then
      if onlyPrimary[skillLine] then
        abilities = 1
      end
      for i = 1, abilities do
        if not C_Spell.IsSpellPassive(i + offset) then
          if i > 1 then
            tinsert(tradeSpells, i + offset)
            extras = extras + 1
          else
            tinsert(tradeSpells, #tradeSpells + 1 - extras, i + offset)
          end
        end
      end
    end
  end
  return tradeSpells
end

function TradeTabs:Initialize()
  local parent = ProfessionsFrame
  local tradeSpells = buildSpellList()
  local i = 1
  local prev


    for i, slot in ipairs(tradeSpells) do
      local spellID = C_SpellBook.GetSpellBookItemInfo(slot, Enum.SpellBookSpellBank.Player).spellID
      local tab = self:CreateTab(i, parent, spellID)
      i = i + 1
      local point,relPoint,x,y = "TOPLEFT", "BOTTOMLEFT", 0, -17

      if not prev then
        prev, relPoint, x, y = parent, "TOPRIGHT", 0, -44
      end

      tab:SetPoint(point, prev, relPoint, x, y)
      prev = tab
    end

  self.initialized = true
end

local function onEnter(self)
  GameTooltip:SetOwner(self,"ANCHOR_RIGHT")
  GameTooltip:SetText(self.tooltip)
  self:GetParent():LockHighlight()
end

local function onLeave(self)
  GameTooltip:Hide()
  self:GetParent():UnlockHighlight()
end

local function updateSelection(self)
  if C_Spell.IsCurrentSpell(self.spell) then
    self:SetChecked(true)
    self.clickStopper:Show()
  else
    self:SetChecked(false)
    self.clickStopper:Hide()
  end
end

local function createClickStopper(button)
  local f = CreateFrame("Frame",nil,button)
  f:SetAllPoints(button)
  f:EnableMouse(true)
  f:SetScript("OnEnter",onEnter)
  f:SetScript("OnLeave",onLeave)
  button.clickStopper = f
  f.tooltip = button.tooltip
  f:Hide()
end

local function RotateTexture(texture, angle)
  local s2 = math.sqrt(2)
  local cos, sin, rad = math.cos, math.sin, math.rad
  local function CalculateCorner(angle)
    local r = rad(angle)
    return 0.5 + cos(r) / s2, 0.5 + sin(r) / s2
  end
  local LRx, LRy = CalculateCorner(angle + 45)
  local LLx, LLy = CalculateCorner(angle + 135)
  local ULx, ULy = CalculateCorner(angle + 225)
  local URx, URy = CalculateCorner(angle - 45)
  texture:SetTexCoord(ULx, ULy, LLx, LLy, URx, URy, LRx, LRy)
end

function TradeTabs:CreateTab(i, parent, spellID)
  local spellInfo = C_Spell.GetSpellInfo(spellID)
  local button = CreateFrame("CheckButton","TradeTabsTab"..i,parent,"ProfessionsItemFlyoutButtonTemplate,SecureActionButtonTemplate")
  button:DisableDrawLayer("BACKGROUND")
  button.tooltip = spellInfo.name
  button.spellID = spellID
  button.spell = spellInfo.name
  local background = button:CreateTexture(nil, "BORDER")
  background:SetTexture("Interface\\SpellBook\\SpellBook-SkillLineTab")
  background:SetSize(70, 67)

  background:SetPoint("CENTER", 14, -5)
  RotateTexture(background, 0)

  button:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square", "ADD")
  button:SetCheckedTexture("Interface\\Buttons\\UI-Quickslot-Depress")
  button:Show()
  button:RegisterForClicks("AnyUp", "AnyDown")
  
  if spellInfo.name == "烹饪用火" then
	button:SetAttribute("type", "macro")
    button:SetAttribute("macrotext", "/use 大厨的帽子\n/cast [@player]烹饪用火")
  else
	button:SetAttribute("type1","spell")
	button:SetAttribute("spell1",spellInfo.name)
  end
  button:SetAttribute("spell1",spellInfo.name)
  button:SetNormalTexture(spellInfo.iconID)
  button:SetScript("OnEvent",updateSelection)
  button:RegisterEvent("TRADE_SKILL_SHOW")
  button:RegisterEvent("TRADE_SKILL_CLOSE")
  button:RegisterEvent("CURRENT_SPELL_CAST_CHANGED")
  createClickStopper(button)
  updateSelection(button)
  return button
end

--TradeTabs:Initialize()
