local addonName, ZLAUTO = ...
local ZL = ZLAUTO.mainFrame
local ZLCreateFrame = ZL.ZLCreateFrame
local CreateButton = ZL.CreateButton
local CreateEditBox = ZL.CreateEditBox
local CreateDynamicTable = ZL.CreateDynamicTable
local GameTooltipShow = ZL.GameTooltipShow
local cGetItemInfo = C_Item and C_Item.GetItemInfo or GetItemInfo
local cGetItemIcon = C_Item and C_Item.GetItemIconByID or GetItemIcon

local LibSerialize = LibStub("LibSerialize")
local LibDeflate = LibStub("LibDeflate")

local INVTYPE_LIST = {"头", "项链", "肩膀", "披风", "胸部", "手腕", "手", "腰带", "腿", "脚", "戒指", "饰品", "主手", "副手", "远程", "其他"}

local InventoryType = {
    INVTYPE_HEAD = "头", INVTYPE_NECK = "项链", INVTYPE_SHOULDER = "肩膀", INVTYPE_CLOAK = "披风", INVTYPE_CHEST = "胸部", INVTYPE_ROBE = "胸部",
    INVTYPE_WRIST = "手腕", INVTYPE_HAND = "手", INVTYPE_WAIST = "腰带", INVTYPE_LEGS = "腿", INVTYPE_FEET = "脚",
    INVTYPE_FINGER = "戒指", INVTYPE_TRINKET = "饰品", INVTYPE_WEAPON = "主手", INVTYPE_SHIELD = "副手", INVTYPE_RANGED = "远程",
    INVTYPE_2HWEAPON = "主手", INVTYPE_WEAPONMAINHAND = "主手", INVTYPE_WEAPONOFFHAND = "副手", INVTYPE_HOLDABLE = "副手",
    INVTYPE_THROWN = "主手", INVTYPE_RANGEDRIGHT = "远程", INVTYPE_RELIC = "远程",
}
local INVTYPE_BISTOOLTIP = {
    ["头"] = "Head",
    ["项链"] = "Neck",
    ["肩膀"] = "Shoulder",
    ["披风"] = "Back",
    ["胸部"] = "Chest",
    ["手腕"] = "Wrist",
    ["手"] = "Hands",
    ["腰带"] = "Waist",
    ["腿"] = "Legs",
    ["脚"] = "Feet",
    ["戒指"] = "Finger",
    ["饰品"] = "Trinket",
    ["主手"] = "Weapon",
    ["副手"] = "Off hand",
    ["远程"] = "Ranged",
}
local INVTYPETEXTURE_LIST = {
    ["头"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-Head.blp",
    ["项链"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-Neck.blp",
    ["肩膀"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-Shoulder.blp",
    ["披风"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-Chest.blp",
    ["胸部"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-Chest.blp",
    ["手腕"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-Wrists.blp",
    ["手"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-Hands.blp",
    ["腰带"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-Waist.blp",
    ["腿"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-Legs.blp",
    ["脚"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-Feet.blp",
    ["戒指"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-Finger.blp",
    ["饰品"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-Trinket.blp",
    ["主手"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-MainHand.blp",
    ["副手"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-SecondaryHand.blp",
    ["远程"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-Ranged.blp",
    ["其他"] = "Interface\\Paperdoll\\UI-PaperDoll-Slot-Ranged.blp",
}

local CLASSNAME_LIST = {
    ["战士"] = "Warrior",
    ["圣骑士"] = "Paladin",
    ["猎人"] = "Hunter",
    ["潜行者"] = "Rogue",
    ["牧师"] = "Priest",
    ["死亡骑士"] = "Death knight",
    ["萨满祭司"] = "Shaman",
    ["法师"] = "Mage",
    ["术士"] = "Warlock",
    ["德鲁伊"] = "Druid",
}
local CLASSICON_LIST = {
    ["战士"] = 626008,
    ["圣骑士"] = 626003,
    ["猎人"] = 626000,
    ["潜行者"] = 626005,
    ["牧师"] = 626004,
    ["死亡骑士"] = 135771,
    ["萨满祭司"] = 626006,
    ["法师"] = 626001,
    ["术士"] = 626007,
    ["德鲁伊"] = 625999,
}
local SPECS_LIST = {
    ["Blood tank"] = "鲜血坦克", ["Frost"] = "冰霜", ["Unholy"] = "邪恶", ["Blood dps"] = "鲜血输出",
    ["Balance"] = "平衡", ["Feral tank"] = "野性坦克", ["Feral dps"] = "野性输出", ["Restoration"] = "恢复",
    ["Beast mastery"] = "野兽控制", ["Marksmanship"] = "射击", ["Survival"] = "生存",
    ["Arcane"] = "奥术", ["Fire"] = "火焰", ["Fire FFB"] = "霜火",
    ["Holy"] = "神圣", ["Protection"] = "防护", ["Retribution"] = "惩戒",
    ["Discipline"] = "戒律", ["Shadow"] = "暗影",
    ["Assassination"] = "刺杀", ["Subtlety"] = "敏锐", ["Combat"] = "战斗",
    ["Elemental"] = "元素", ["Enhancement"] = "增强",
    ["Affliction"] = "痛苦", ["Demonology"] = "恶魔学识", ["Destruction"] = "毁灭",
    ["Arms"] = "武器", ["Fury"] = "狂怒",
}
local SPECSICON_LIST = {
    ["Death knight"] = {
        ["classIcon"] = 135771,
        ["Blood tank"] = 135770,
        ["Frost"] = 135773,
        ["Unholy"] = 135775,
        ["Blood dps"] = 135770,
    },
    ["Druid"] = {
        ["classIcon"] = 625999,
        ["Balance"] = 136096,
        ["Feral tank"] = 132276,
        ["Feral dps"] = 132115,
        ["Restoration"] = 136041,
    },
    ["Hunter"] = {
        ["classIcon"] = 626000,
        ["Beast mastery"] = 132164,
        ["Marksmanship"] = 132222,
        ["Survival"] = 132215,
    },
    ["Mage"] = {
        ["classIcon"] = 626001,
        ["Arcane"] = 135932,
        ["Fire"] = 135810,
        ["Fire FFB"] = 236217,
        ["Frost"] = 135846,
    },
    ["Paladin"] = {
        ["classIcon"] = 626003,
        ["Holy"] = 135920,
        ["Protection"] = 135893,
        ["Retribution"] = 135873,
    },
    ["Priest"] = {
        ["classIcon"] = 626004,
        ["Discipline"] = 135987,
        ["Holy"] = 135953,
        ["Shadow"] = 136207,
    },
    ["Rogue"] = {
        ["classIcon"] = 626005,
        ["Assassination"] = 132292,
        ["Combat"] = 132090,
        ["Subtlety"] = 132320,
    },
    ["Shaman"] = {
        ["classIcon"] = 626006,
        ["Elemental"] = 136048,
        ["Enhancement"] = 136051,
        ["Restoration"] = 136052,
    },
    ["Warlock"] = {
        ["classIcon"] = 626007,
        ["Affliction"] = 136145,
        ["Demonology"] = 136172,
        ["Destruction"] = 136186,
        ["Destruction fire"] = 136186,
    },
    ["Warrior"] = {
        ["classIcon"] = 626008,
        ["Arms"] = 132292,
        ["Fury"] = 132347,
        ["Protection"] = 134952,
    },
}


function ZL:CreateConcernFrame()
    if not ZL.opt.concernList then ZL.opt.concernList = {} end
    local concernList = ZL.opt.concernList
    local realmKey = GetRealmName()
    local charKey = ZL.GetClassColorName(UnitName("player")) .. " - " .. realmKey
    local className = UnitClass("player")
    local classIcon = CLASSICON_LIST[className]
    if (not concernList[charKey]) and ((not ZL.opt.concernType) or (not ZL.opt.concernType.deleteList[charKey])) then
        concernList[charKey] = {
            ["classInfo"] = {
                icon = classIcon,
                class = className,
                hasRelic = UnitHasRelicSlot("player"),
            },
            ["slotInfo"] = {
                ["头"] = {},
                ["项链"] = {},
                ["肩膀"] = {},
                ["披风"] = {},
                ["胸部"] = {},
                ["手腕"] = {},
                ["手"] = {},
                ["腰带"] = {},
                ["腿"] = {},
                ["脚"] = {},
                ["戒指"] = {},
                ["饰品"] = {},
                ["主手"] = {},
                ["副手"] = {},
                ["远程"] = {},
                ["其他"] = {},
            },
            ["itemList"] = {},
        }
    end
    local function profileDropdownList()
        local tempList = {}
        local tempDownList = {}
        if concernList then
            for _charKey in pairs(concernList) do
                tinsert(tempList, _charKey)
            end
            table.sort(tempList)
            for i, _charKey in ipairs(tempList) do
                if (not ZL.opt.concernType) or (not ZL.opt.concernType.hideList[_charKey]) then
                    tinsert(tempDownList, {
                        text = "|T" .. concernList[_charKey]["classInfo"]["icon"] .. ":0:0:2:0|t " .. _charKey,
                        value = i,
                    })
                end
            end
        end
        wipe(tempList)
        tempList = nil
        return tempDownList
    end

    ZL.ConcernFrame = ZLCreateFrame("ZLConcernFrame", ZL, nil, nil, nil, true, true, "关注物品")
    local ConcernFrame = ZL.ConcernFrame
    tinsert(UISpecialFrames, ConcernFrame:GetName())
    ConcernFrame:SetScript("OnMouseUp", function (self)
        self:StopMovingOrSizing()
        if not ZL.db.global.ZLConcernFrame then ZL.db.global.ZLConcernFrame = {} end
        ZL.db.global.ZLConcernFrame.point, _, ZL.db.global.ZLConcernFrame.relative, ZL.db.global.ZLConcernFrame.x, ZL.db.global.ZLConcernFrame.y = self:GetPoint()
    end)
    ConcernFrame:SetScript("OnMouseDown", function (self)
        self:StartMoving()
    end)
    ConcernFrame:SetScript("OnShow", function (self)
        ConcernFrame:Update()
    end)
    ConcernFrame:ClearAllPoints()
    if ZL.opt.ZLConcernFrame and ZL.opt.ZLConcernFrame.point then
        ConcernFrame:SetPoint(ZL.opt.ZLConcernFrame.point, UIParent, ZL.opt.ZLConcernFrame.relative, ZL.opt.ZLConcernFrame.x, ZL.opt.ZLConcernFrame.y)
    else
        ConcernFrame:SetPoint("CENTER")
    end

    ConcernFrame.infoBtn = CreateFrame("Button", nil, ConcernFrame)
    ConcernFrame.infoBtn:SetSize(25, 25)
    ConcernFrame.infoBtn:SetPoint("TOPLEFT", 3, 0)
    ConcernFrame.infoBtn:SetNormalTexture(616343)
    ConcernFrame.infoBtn:SetHighlightTexture(616343)
    ConcernFrame.infoBtn:SetScript("OnEnter", function (self)
        GameTooltipShow(self, "BOTTOM", "TOP")
        GameTooltip:AddLine("添加关注物品5种方式：")
        GameTooltip:AddLine("1、直接输入物品ID添加", 1, 1, 1)
        GameTooltip:AddLine("2、聊天窗口|cff00ff00右键|r点击物品链接，点|cff00ff00关注|r", 1, 1, 1)
        GameTooltip:AddLine("3、打开AtlasLoot插件|cffff0000(需安装)|r，|cff00ff00右键|r点击物品图标", 1, 1, 1)
        GameTooltip:AddLine("4、点击部位按钮，弹出BisTooltip插件|cffff0000(需安装)|r，选择对应物品关注", 1, 1, 1)
        GameTooltip:AddLine("5、拍卖清单、账单总览、拾取记录、对账单窗口|cff00ff00双击|r物品图标关注", 1, 1, 1)
        GameTooltip:AddLine(" ", 1, 1, 1)
        GameTooltip:AddLine("删除物品：|cff00ff00双击|r对应物品图标", 1, 1, 1)
        GameTooltip:Show()
    end)
    ConcernFrame.infoBtn:SetScript("OnLeave", function ()
        GameTooltip:Hide()
    end)

    ConcernFrame.profileDropdown = CreateFrame("Button", nil, ConcernFrame)
    local profileDropdown = ConcernFrame.profileDropdown
    profileDropdown:SetPoint("TOPLEFT", ConcernFrame.TitleBottom, "BOTTOMLEFT", 5, -5)
    profileDropdown:SetSize(211, 24)
    ZL.GUI:GetClass('Dropdown'):Bind(profileDropdown)
    if (not ZL.opt.concernType) or (not ZL.opt.concernType.deleteList[charKey]) then
        profileDropdown:SetDefaultText("|T" .. classIcon .. ":0:0:2:0|t " .. charKey)
    else
        profileDropdown:SetDefaultText(" ")
    end
    profileDropdown:SetMenuTable(profileDropdownList)
    profileDropdown:SetCallback('OnSelectChanged', function ()
        local selectCharKey = profileDropdown:GetText()
        selectCharKey = selectCharKey:match("^|T%d+:0:0:2:0|t (.+)")
        if selectCharKey ~= charKey then
            local msgStr = "注意：已切换为非当前账号配置\n添加和删除关注物品将保存到：" .. selectCharKey
            ZL.Print(nil, nil, 1, 1, 0, msgStr)
        end
        ConcernFrame:Update()
    end)

    ConcernFrame.exportBtn = CreateButton(nil, ConcernFrame, "导出", "LEFT", profileDropdown, "RIGHT", 5, 0, 40, 24, 14)
    local exportBtn= ConcernFrame.exportBtn
    exportBtn:SetScript("OnClick", function ()
        local selectCharKey = profileDropdown:GetText()
        selectCharKey = selectCharKey:match("^|T%d+:0:0:2:0|t (.+)")
        if not selectCharKey then return end
        if concernList and concernList[selectCharKey] and concernList[selectCharKey]["itemList"]
        and next(concernList[selectCharKey]["itemList"]) then
            local data = concernList[selectCharKey]
            local serialized = LibSerialize:Serialize(data)
            local compressed = LibDeflate:CompressDeflate(serialized)
            local encoded = "!AutoAuction:1!" .. LibDeflate:EncodeForPrint(compressed)
            if not ZL.WaStringFrame then ZL:CreateWAStringFrame() end
            local editBox = ZL.WaStringFrame.scrollFrame.editBox
            ZL.WaStringFrame.Title:SetText("【" .. selectCharKey .. "】关注物品数据|cff87CEEB导出|r")
            editBox:SetScript("OnTextChanged", function (self) self:HighlightText() end)
            editBox:SetScript("OnMouseUp", function(self) self:HighlightText() end)
            editBox:SetScript("OnEscapePressed", function() ZL.WaStringFrame:Hide() end)
            editBox:SetText(encoded)
            editBox:HighlightText()
            editBox:SetFocus()
            ZL.WaStringFrame:Show()
            encoded = nil
            compressed = nil
            serialized = nil
        else
            ZL.Print(nil, nil, 0, 1, 0, "【" .. selectCharKey .. "】没有关注物品数据！")
        end
    end)

    ConcernFrame.hideBtn = CreateButton(nil, ConcernFrame, "隐藏", "LEFT", exportBtn, "RIGHT", 5, 0, 40, 24, 14)
    local hideBtn= ConcernFrame.hideBtn
    hideBtn:SetScript("OnEnter", function (self)
        GameTooltipShow(self, "LEFT", "RIGHT")
        GameTooltip:AddLine("隐藏角色")
        GameTooltip:AddLine("不删除数据，只有登录该角色\n时才可以看到关注数据", 1, 1, 1)
        GameTooltip:Show()
    end)
    hideBtn:SetScript("OnLeave", function ()
        GameTooltip:Hide()
    end)
    hideBtn:SetScript("OnClick", function (self)
        local function tempfunc()
            local menuList = {{text="隐藏角色", isTitle=true}}
            local charList = {}
            for _charKey in pairs(concernList) do
                tinsert(charList, _charKey)
            end
            table.sort(charList)
            if not ZL.opt.concernType then ZL.opt.concernType = {hideList={}, deleteList={}} end
            local concernType = ZL.opt.concernType
            for _, _charKey in ipairs(charList) do
                tinsert(menuList, {
                    text = "|T" .. concernList[_charKey]["classInfo"]["icon"] .. ":0:0:2:0|t " .. _charKey,
                    checkable = 1,
                    checked = concernType.hideList[_charKey],
                    isNotRadio = 1,
                    keepShownOnClick = 1,
                    func = function (_, data)
                        if not data.checked then
                            data.checked = 1
                            concernType.hideList[_charKey] = true
                            local selectCharKey = ConcernFrame.profileDropdown:GetText()
                            selectCharKey = selectCharKey:match("^|T%d+:0:0:2:0|t (.+)")
                            if _charKey == selectCharKey then
                                local tempDownList = profileDropdownList()
                                if #tempDownList > 0 then
                                    ConcernFrame.profileDropdown:SetDefaultText(tempDownList[1].text)
                                else
                                    ConcernFrame.profileDropdown:SetDefaultText(" ")
                                end
                                ConcernFrame:Update()
                            end
                        else
                            data.checked = nil
                            concernType.hideList[_charKey] = nil
                        end
                        ConcernFrame.profileDropdown:SetMenuTable(profileDropdownList)
                    end
                })
            end
            tinsert(menuList, {isSeparator = true})
            tinsert(menuList, {text="取消", func = function ()
                ZL.GUI:CloseMenu()
            end})
            return menuList
        end
        ZL.GUI:ToggleMenu(self, tempfunc(), 'TOPLEFT', self, 'TOPRIGHT')
    end)

    ConcernFrame.importBtn = CreateButton(nil, ConcernFrame, "导入", "TOPLEFT", exportBtn, "BOTTOMLEFT", 0, -5, 40, 24, 14)
    local importBtn= ConcernFrame.importBtn
    importBtn:SetScript("OnClick", function ()
        local selectCharKey = profileDropdown:GetText()
        selectCharKey = selectCharKey:match("^|T%d+:0:0:2:0|t (.+)")
        if not selectCharKey then return end
        if not ZL.WaStringFrame then ZL:CreateWAStringFrame() end
        local editBox = ZL.WaStringFrame.scrollFrame.editBox
        ZL.WaStringFrame.Title:SetText("在下方粘贴字符串文本，关注物品数据将|cffffffff导入|r到【" .. selectCharKey .. "】")
        editBox:SetScript("OnMouseUp", nil)
        editBox:SetScript("OnEscapePressed", function() ZL.WaStringFrame:Hide() end)
        editBox:SetScript("OnTextChanged", function (self)
            local text = self:GetText()
            if text:match("^!AutoAuction:%d+!.+") then
                text = text:gsub("!AutoAuction:%d+!", "", 1)
                local decodedData = LibDeflate:DecodeForPrint(text)
                local decompressedData = LibDeflate:DecompressDeflate(decodedData)
                local success, myReceivedData = LibSerialize:Deserialize(decompressedData)
                if success then
                    if ((not self.time) or (GetTime() - self.time) > 1) then
                        self.time = GetTime()
                        local _selectCharKey = profileDropdown:GetText()
                        _selectCharKey = _selectCharKey:match("^|T%d+:0:0:2:0|t (.+)")
                        if not _selectCharKey then return end
                        local receivedClass = myReceivedData.classInfo.class
                        local selectClass = concernList[_selectCharKey].classInfo.class
                        local msgStr
                        if selectClass ~= receivedClass then
                            msgStr = "职业不符，无法导入！当前选择角色职业：|T" .. CLASSICON_LIST[selectClass] .. ":0:0:2:0|t |cffffffff" .. selectClass .. "|r，导入数据职业：|T" .. CLASSICON_LIST[receivedClass] .. ":0:0:2:0|t |cffffffff" .. receivedClass .. "|r"
                            ZL.Print(nil, nil, 1, 0, 0, msgStr)
                        else
                            local btnText
                            if next(concernList[_selectCharKey].itemList) then
                                msgStr = "当前选择角色【" .. _selectCharKey .. "】存在关注物品数据，导入后将覆盖原有数据，请选择"
                                btnText = "覆盖导入"
                            else
                                msgStr = "确定将关注物品数据导入到【" .. _selectCharKey .. "】中？"
                                btnText = "导入"
                            end
                            ZL_CURRENT.linshiTest = myReceivedData
                            local statisticsData = {
                                data = myReceivedData,
                                selectCharKey = _selectCharKey,
                                btnText = btnText,
                            }
                            StaticPopup_Show("ZL_IMPORTCORCERNSTRING", msgStr, nil, statisticsData)
                        end
                    end
                else
                    ZL.Print(nil, nil, 1, 0, 0, "解析失败！")
                end
            end
        end)
        editBox:SetText("")
        editBox:HighlightText()
        editBox:SetFocus()

        ZL.WaStringFrame:Show()
    end)

    ConcernFrame.delBtn = CreateButton(nil, ConcernFrame, "删除", "LEFT", importBtn, "RIGHT", 5, 0, 40, 24, 14)
    local delBtn= ConcernFrame.delBtn
    delBtn:SetScript("OnEnter", function (self)
        GameTooltipShow(self, "LEFT", "RIGHT")
        GameTooltip:AddLine("删除角色")
        GameTooltip:AddLine("删除角色关注数据", 1, 1, 1)
        GameTooltip:Show()
    end)
    delBtn:SetScript("OnLeave", function ()
        GameTooltip:Hide()
    end)
    delBtn:SetScript("OnClick", function (self)
        local function tempfunc()
            local menuList = {{text="删除角色", isTitle=true}}
            local charList = {}
            for _charKey in pairs(concernList) do
                tinsert(charList, _charKey)
            end
            table.sort(charList)
            if not ZL.opt.concernType then ZL.opt.concernType = {hideList={}, deleteList={}} end
            local concernType = ZL.opt.concernType
            local tempList = {}
            if next(concernType.deleteList) then
                for key in pairs(concernType.deleteList) do
                    tinsert(tempList, key)
                end
            end
            table.sort(tempList)
            for _, key in ipairs(tempList) do
                tinsert(charList, key)
            end
            for _, _charKey in ipairs(charList) do
                local texture = concernList[_charKey] and concernList[_charKey]["classInfo"]["icon"] or 134400
                tinsert(menuList, {
                    text = "|T" .. texture .. ":0:0:2:0|t " .. _charKey,
                    checkable = 1,
                    checked = concernType.deleteList[_charKey],
                    isNotRadio = 1,
                    keepShownOnClick = 1,
                    func = function (_, data)
                        if not data.checked then
                            data.checked = 1
                            concernType.deleteList[_charKey] = true
                            concernType.hideList[_charKey] = nil
                            concernList[_charKey] = nil
                            local selectCharKey = ConcernFrame.profileDropdown:GetText()
                            selectCharKey = selectCharKey:match("^|T%d+:0:0:2:0|t (.+)")
                            if _charKey == selectCharKey then
                                local tempDownList = profileDropdownList()
                                if #tempDownList > 0 then
                                    ConcernFrame.profileDropdown:SetDefaultText(tempDownList[1].text)
                                else
                                    ConcernFrame.profileDropdown:SetDefaultText(" ")
                                end
                                ConcernFrame:Update()
                            end
                        else
                            data.checked = nil
                            concernType.deleteList[_charKey] = nil
                            if _charKey == charKey then
                                concernList[charKey] = {
                                    ["classInfo"] = {
                                        icon = classIcon,
                                        class = className,
                                        hasRelic = UnitHasRelicSlot("player"),
                                    },
                                    ["slotInfo"] = {
                                        ["头"] = {},
                                        ["项链"] = {},
                                        ["肩膀"] = {},
                                        ["披风"] = {},
                                        ["胸部"] = {},
                                        ["手腕"] = {},
                                        ["手"] = {},
                                        ["腰带"] = {},
                                        ["腿"] = {},
                                        ["脚"] = {},
                                        ["戒指"] = {},
                                        ["饰品"] = {},
                                        ["主手"] = {},
                                        ["副手"] = {},
                                        ["远程"] = {},
                                        ["其他"] = {},
                                    },
                                    ["itemList"] = {},
                                }
                            end
                        end
                        ConcernFrame.profileDropdown:SetMenuTable(profileDropdownList)
                    end
                })
            end
            tinsert(menuList, {isSeparator = true})
            tinsert(menuList, {text="取消", func = function ()
                ZL.GUI:CloseMenu()
            end})
            return menuList
        end
        ZL.GUI:ToggleMenu(self, tempfunc(), 'TOPLEFT', self, 'TOPRIGHT')
    end)

    ConcernFrame.itemIdFontString = ConcernFrame:CreateFontString(nil, "OVERLAY")
    ConcernFrame.itemIdFontString:SetPoint("TOPLEFT", profileDropdown, "BOTTOMLEFT", 5, -5)
    ConcernFrame.itemIdFontString:SetHeight(24)
    ConcernFrame.itemIdFontString:SetFont(GameFontNormal:GetFont(), 14)
    ConcernFrame.itemIdFontString:SetWordWrap(false)
    ConcernFrame.itemIdFontString:SetJustifyH("CENTER")
    ConcernFrame.itemIdFontString:SetTextColor(1, 0.82, 0, 1)
    ConcernFrame.itemIdFontString:SetText("物品ID：")

    ConcernFrame.itemIdBox = CreateEditBox(ConcernFrame, 105, 24, 14, "RIGHT", true)
    local itemIdBox = ConcernFrame.itemIdBox
    itemIdBox:SetPoint("LEFT", ConcernFrame.itemIdFontString, "RIGHT", 5, 0)
    itemIdBox.Background:SetColorTexture(0, 0, 0, 1)
    --添加关注
    function ZL.addItem(itemId)
        itemId = tonumber(itemId) or (itemIdBox:GetNumber() or 0)
        if itemId > 0 then
            local selectCharKey = profileDropdown:GetText()
            selectCharKey = selectCharKey:match("^|T%d+:0:0:2:0|t (.+)")
            if not selectCharKey then return end
            if not concernList[selectCharKey]["itemList"][itemId] then
                local item = Item:CreateFromItemID(itemId)
                if item:GetItemID() then
                    item:ContinueOnItemLoad(function()
                        local _, itemLink, _, _, _, _, _, _, itemEquipLoc = cGetItemInfo(itemId)
                        if itemEquipLoc and InventoryType[itemEquipLoc] then
                            tinsert(concernList[selectCharKey]["slotInfo"][InventoryType[itemEquipLoc]], itemId)
                            ZL.Print(nil, nil, 0, 1, 0, itemLink .. "已添加到" .. selectCharKey .. "关注清单！")
                        else
                            tinsert(concernList[selectCharKey]["slotInfo"]["其他"], itemId)
                            ZL.Print(nil, nil, 0, 1, 0, itemLink .. "已添加到" .. selectCharKey .. "|cffff0000其他|r部位！")
                        end
                        concernList[selectCharKey]["itemList"][itemId] = true
                        ConcernFrame:Update()
                        ZL.AuctionFrame2:Update()
                    end)
                else
                    ZL.Print(nil, nil, 1 , 0, 0, "物品ID不存在！")
                end
            else
                ZL.Print(nil, nil, 0 , 1, 0, "物品ID已存在！")
            end
        else
            -- ZL.Print(nil, nil, 1 , 0, 0, "物品ID错误！")
        end
        itemIdBox:ClearFocus()
    end
    local addItem = ZL.addItem
    --删除关注
    function ZL.deleteItem(itemId)
        itemId = tonumber(itemId)
        if type(itemId) ~= "number" or itemId <= 0 then return end
        local selectCharKey = profileDropdown:GetText()
        selectCharKey = selectCharKey:match("^|T%d+:0:0:2:0|t (.+)")
        if not selectCharKey then return end
        concernList[selectCharKey]["itemList"][itemId] = nil
        local item = Item:CreateFromItemID(itemId)
        if item:GetItemID() then
            item:ContinueOnItemLoad(function()
                local _, itemLink, _, _, _, _, _, _, itemEquipLoc = cGetItemInfo(itemId)
                local invtype
                if itemEquipLoc and InventoryType[itemEquipLoc] then
                    invtype = InventoryType[itemEquipLoc]
                else
                    invtype = "其他"
                end
                for _index, _itemId in ipairs(concernList[selectCharKey]["slotInfo"][invtype]) do
                    if _itemId == itemId then
                        tremove(concernList[selectCharKey]["slotInfo"][invtype], _index)
                        break
                    end
                end
                ConcernFrame:Update()
                ZL.Print(nil, nil, 0, 1, 0, itemLink .. "已取消关注")
                ZL.AuctionFrame2:Update()
            end)
        else
            ZL.Print(nil, nil, 1 , 0, 0, "物品ID不存在！")
        end

    end
    local function bisTooltipActivityMenu(self)
        local menuList = {}
        if Bistooltip_bislists then
            local selectCharKey = profileDropdown:GetText()
            selectCharKey = selectCharKey:match("^|T%d+:0:0:2:0|t (.+)")
            if not selectCharKey then return end
            local class = concernList[selectCharKey]["classInfo"]["class"]
            local bisClass = CLASSNAME_LIST[class]
            if bisClass then
                tinsert(menuList, {text="数据来源：BisTooltip", isTitle = true})
                for spec, specData in pairs(Bistooltip_bislists[bisClass]) do
                    local specList = {}
                    tinsert(specList, {text="天赋：" .. SPECS_LIST[spec], isTitle = true})
                    for _, phase in ipairs(Bistooltip_phases) do
                        if specData[phase] then
                            local phaseList = {}
                            tinsert(phaseList, {text="阶段：" .. phase, isTitle = true})
                            local invtypeTempList = {}
                            if self.row then
                                invtypeTempList[1] = INVTYPE_LIST[self.row]
                            else
                                invtypeTempList = INVTYPE_LIST
                            end
                            for i, invtype in ipairs(invtypeTempList) do
                                local bisinvtype = (invtype == "远程" and concernList[selectCharKey]["classInfo"]["hasRelic"]) and "Relic" or INVTYPE_BISTOOLTIP[invtype]
                                for bisIndex, bisData in ipairs(specData[phase]) do
                                    if bisData.slot_name == bisinvtype then
                                        local invtypeList = {}
                                        local text = (invtype == "远程" and concernList[selectCharKey]["classInfo"]["hasRelic"]) and RELICSLOT or invtype
                                        invtypeList[1] = {text="部位：" .. text, isTitle = true}
                                        for index, itemId in ipairs(bisData) do
                                            local item = Item:CreateFromItemID(itemId)
                                            if item:GetItemID() then
                                                item:ContinueOnItemLoad(function()
                                                    local isExist = concernList[selectCharKey]["itemList"][itemId] and true or nil
                                                    local _, itemLink, _, _, _, _, _, _, itemEquipLoc, itemTexture = cGetItemInfo(itemId)
                                                    if itemEquipLoc and InventoryType[itemEquipLoc] then
                                                        invtypeList[index + 1] = {
                                                            text = "Top" .. index .. " |T" .. itemTexture .. ":0:0:2:0|t " .. itemLink,
                                                            checkable = 1,
                                                            checked = isExist,
                                                            isNotRadio = 1,
                                                            tooltipWhileDisabled = 1,
                                                            -- disabled = isExist,
                                                            keepShownOnClick = 1,
                                                            tooltipMore = function(tip, data, button)
                                                                tip:SetHyperlink(itemLink)
                                                                tip:Show()
                                                                if _G["ShoppingTooltip1"] and _G["ShoppingTooltip1"]:IsShown() then
                                                                    _G["ShoppingTooltip1"]:Hide()
                                                                end
                                                                if _G["ShoppingTooltip2"] and _G["ShoppingTooltip2"]:IsShown() then
                                                                    _G["ShoppingTooltip2"]:Hide()
                                                                end
                                                            end,
                                                            func = function (owner, data, dropdown, _, button)
                                                                if not data.checked then
                                                                    addItem(itemId)
                                                                    data.checked = 1
                                                                else
                                                                    --删除关注
                                                                    ZL.deleteItem(itemId)
                                                                    data.checked = nil
                                                                end
                                                            end
                                                        }
                                                    end
                                                end)
                                            end
                                        end
                                        local texture = (invtype == "远程" and concernList[selectCharKey]["classInfo"]["hasRelic"]) and "Interface\\Paperdoll\\UI-PaperDoll-Slot-Relic.blp" or INVTYPETEXTURE_LIST[invtype]
                                        tinsert(phaseList, {
                                            text = "|T" .. texture .. ":0:0:2:0|t " .. text,
                                            func = function ()
                                                self:ClearFocus()
                                                ZL.GUI:CloseMenu()
                                            end,
                                            hasArrow = true,
                                            menuTable = invtypeList,
                                        })
                                        break
                                    end
                                end
                            end
                            tinsert(specList, {
                                text = phase,
                                func = function ()
                                    self:ClearFocus()
                                    ZL.GUI:CloseMenu()
                                end,
                                hasArrow = true,
                                menuTable = phaseList,
                            })
                        end
                    end
                    tinsert(menuList, {
                        text = "|T" .. SPECSICON_LIST[bisClass][spec] .. ":0:0:2:0|t " .. SPECS_LIST[spec],
                        func = function ()
                            ZL.GUI:CloseMenu()
                            itemIdBox:ClearFocus()
                        end,
                        hasArrow = true,
                        menuTable = specList,
                    })
                end
            end
        end
        return menuList
    end
    itemIdBox:SetScript("OnEnterPressed", function (self)
        addItem()
    end)
    itemIdBox:SetScript("OnEditFocusGained", function (self)
        self:HighlightText()
        if Bistooltip_bislists then
            ZL.GUI:ToggleMenu(self, bisTooltipActivityMenu(self), 'TOPLEFT', self, 'BOTTOMRIGHT')
        end
    end)
    itemIdBox:SetScript("OnEditFocusLost", function (self)
        self:HighlightText(0, 0)
    end)
    ConcernFrame.addBtn = CreateButton(nil, ConcernFrame, "添加", "LEFT", itemIdBox, "RIGHT", 5, 0, 40, 24, 14)
    local addBtn = ConcernFrame.addBtn
    addBtn:SetScript("OnClick", function ()
        addItem()
    end)
    addBtn:SetScript("OnEnter", function (self)
        GameTooltipShow(self, "LEFT", "RIGHT")
        GameTooltip:AddLine("如需关注套装兑换物品")
        GameTooltip:AddLine("输入兑换物品ID，关注即可", 1, 1, 1)
        GameTooltip:Show()
    end)
    addBtn:SetScript("OnLeave", function ()
        GameTooltip:Hide()
    end)
    --创建滚动条
    ConcernFrame.scrollFrame = CreateFrame("ScrollFrame", nil, ConcernFrame, "UIPanelScrollFrameTemplate")
    ConcernFrame.scrollFrame:SetFrameLevel(ConcernFrame:GetFrameLevel() + 1) -- 设置为比父框架高1级
    ConcernFrame.scrollFrame:SetPoint("TOPLEFT", ConcernFrame.TitleBottom, "BOTTOMLEFT", 0, -20-profileDropdown:GetHeight()-ConcernFrame.itemIdFontString:GetHeight())
    ConcernFrame.scrollFrame:SetPoint("BOTTOMRIGHT", ConcernFrame, "BOTTOMRIGHT", 0, 3)
    ConcernFrame.scrollFrame:SetClipsChildren(true) --必须设置为true，scrollFrame所有的子元素才会被剪切
    ConcernFrame.scrollFrame:SetScript("OnMouseWheel", ZL.ScrollFrame_OnMouseWheel) --重设滑动条鼠标滚轮事件
    --重设ScrollBar位置，放置到scrollFrame内，防止被剪切掉
    ConcernFrame.scrollFrame.ScrollBar:ClearAllPoints()
    ConcernFrame.scrollFrame.ScrollBar:SetPoint("TOPLEFT", ConcernFrame.scrollFrame, "TOPRIGHT", -20, -17)
    ConcernFrame.scrollFrame.ScrollBar:SetPoint("BOTTOMRIGHT", ConcernFrame.scrollFrame, "BOTTOMRIGHT", 0, 16)
    ConcernFrame.scrollFrame.content = CreateFrame("Frame", nil, ConcernFrame.scrollFrame)
    local content = ConcernFrame.scrollFrame.content
    content:SetPoint("TOPLEFT", ConcernFrame.scrollFrame, "TOPLEFT", 0, 0)
    content:SetSize(ConcernFrame.scrollFrame:GetWidth()-20, 100)
    ConcernFrame.scrollFrame:SetScrollChild(content)
    ConcernFrame.scrollFrame.ScrollBar:SetFrameLevel(content:GetFrameLevel()+100)
    -- ZL.background(content)

    ConcernFrame.entries = CreateDynamicTable()

    function ConcernFrame:Update()
        for i in ipairs(ConcernFrame.entries) do
            for j in ipairs(ConcernFrame.entries[i]) do
                if j ~= 1 then
                    ConcernFrame.entries[i][j]:Hide()
                end
            end
        end
        local selectCharKey = profileDropdown:GetText()
        if not selectCharKey then return end
        selectCharKey = selectCharKey:match("^|T%d+:0:0:2:0|t (.+)")
        if not selectCharKey then return end
        -- local charName = selectCharKey:match("|c%x%x%x%x%x%x%x%x(.+)%|r - .+")
        local w, h = 40, 40
        local wgap, hgap = 15, 15
        local maxWidth = 315
        local highlightTexture = [[Interface\Buttons\ButtonHilight-Square]]
        for i, invtype in ipairs(INVTYPE_LIST) do
            if not ConcernFrame.entries[i][1] then
                local btnText = (invtype == "远程" and concernList[selectCharKey]["classInfo"]["hasRelic"]) and RELICSLOT or invtype
                local texture = (invtype == "远程" and concernList[selectCharKey]["classInfo"]["hasRelic"]) and "Interface\\Paperdoll\\UI-PaperDoll-Slot-Relic.blp" or INVTYPETEXTURE_LIST[invtype]
                -- local button = CreateButton(nil, content, "|cffffffff" .. btnText .. "|r", "TOPLEFT", content, "TOPLEFT", 5, -(i-1)*(h+hgap), w, h, 15)
                local button = CreateFrame("Button", nil, content)
                button:SetNormalTexture(texture)
                button:SetHighlightTexture(highlightTexture)
                button:SetPoint("TOPLEFT", content, "TOPLEFT", 10, -(i-1)*(h+hgap))
                button:SetSize(w, h)
                button:SetNormalFontObject("GameFontNormal")
                button:SetHighlightFontObject("GameFontHighlight")
                button:SetDisabledFontObject("GameFontDisable")
                button:SetText("+")
                button:SetScript("OnClick", function (self)
                    if Bistooltip_bislists then
                        ZL.GUI:ToggleMenu(self, bisTooltipActivityMenu(self), 'TOPLEFT', self, 'BOTTOMRIGHT')
                    else
                        ZL.Print(nil, nil, 0, 1, 0, "没有安装BisTooltip插件，请使用物品ID方式添加！")
                    end
                end)
                local fontString = button:GetFontString()
                fontString:ClearAllPoints()
                fontString:SetPoint("BOTTOM", button, "BOTTOM", 0, 5)
                fontString:SetFont(GameFontNormal:GetFont(), 15)

                button.invType = invtype
                button.row = i
                button.lable = button:CreateFontString(nil, "OVERLAY")
                button.lable:SetPoint("TOP", button, "TOP", 0, -5)
                button.lable:SetFont(GameFontNormal:GetFont(), 14, "OUTLINE")
                button.lable:SetWordWrap(false)
                button.lable:SetJustifyH("CENTER")
                button.lable:SetTextColor(1, 1, 1, 1)
                button.lable:SetText(btnText)
                ConcernFrame.entries[i][1] = button
            end
            ConcernFrame.entries[i][1]:Show()
            if concernList[selectCharKey]["slotInfo"][invtype] then
                for j, itemId in ipairs(concernList[selectCharKey]["slotInfo"][invtype]) do
                    if not ConcernFrame.entries[i][j+1] then
                        local button = CreateFrame("Button", nil, content)
                        button:SetHighlightTexture(highlightTexture)
                        button:SetPoint("TOPLEFT", content, "TOPLEFT", j*(w+wgap)+10, -(i-1)*(h+hgap))
                        button:SetSize(w, h)
                        button:SetNormalFontObject("GameFontNormal")
                        button:SetHighlightFontObject("GameFontHighlight")
                        button:SetDisabledFontObject("GameFontDisable")
                        button:RegisterForClicks("LeftButtonUp", "RightButtonUp")
                        button:SetScript("OnEnter", function (self)
                            local uiScale, x, y = UIParent:GetEffectiveScale(), GetCursorPosition()
                            x, y = x/uiScale, y/uiScale
                            local screenWidth = GetScreenWidth() / 2
                            if x < screenWidth then
                                GameTooltipShow(self, "TOPLEFT", "TOPRIGHT")
                            else
                                GameTooltipShow(self, "TOPRIGHT", "TOPLEFT")
                            end
                            GameTooltip:SetHyperlink(self.itemLink)
                            GameTooltip:Show()
                            if _G["ShoppingTooltip1"] and _G["ShoppingTooltip1"]:IsShown() then
                                _G["ShoppingTooltip1"]:ClearAllPoints()
                                if x < screenWidth then
                                    _G["ShoppingTooltip1"]:SetPoint("TOPLEFT", GameTooltip, "TOPRIGHT", 0, 0)
                                else
                                    _G["ShoppingTooltip1"]:SetPoint("TOPRIGHT", GameTooltip, "TOPLEFT", 0, 0)
                                end
                            end
                            if _G["ShoppingTooltip2"] and _G["ShoppingTooltip2"]:IsShown() then
                                _G["ShoppingTooltip2"]:ClearAllPoints()
                                if x < screenWidth then
                                    _G["ShoppingTooltip2"]:SetPoint("TOPLEFT", _G["ShoppingTooltip1"], "TOPRIGHT", 0, 0)
                                else
                                    _G["ShoppingTooltip2"]:SetPoint("TOPRIGHT", _G["ShoppingTooltip1"], "TOPLEFT", 0, 0)
                                end
                            end

                            if type(self.editBox.targetAmount) ~= "number" then
                                self.editBox.lable:SetText("心理价")
                            end
                        end)
                        button:SetScript("OnLeave", function (self)
                            GameTooltip:Hide()
                            if type(self.editBox.targetAmount) ~= "number" then
                                self.editBox.lable:SetText("")
                            end
                        end)
                        button:SetScript("OnDoubleClick", function (self, btn)
                            --删除关注
                            concernList[selectCharKey]["itemList"][self.itemId] = nil
                            tremove(concernList[selectCharKey]["slotInfo"][self.invType], self.col-1)
                            ConcernFrame:Update()
                            ZL.Print(nil, nil, 0, 1, 0, self.itemLink .. "已取消关注")
                        end)
                        button.editBox = CreateEditBox(button, w+6, 15, 12, "CENTER", true)
                        local editBox = button.editBox
                        editBox.Background:SetColorTexture(0, 0, 0, 1)
                        editBox:SetPoint("TOP", button, "BOTTOM", 0, 0)
                        editBox:SetSize(w+20, 20)
                        editBox:SetScript("OnEditFocusGained", function (self)
                            self.Background:Show()
                            self.BorderTop:Show()
                            self.BorderLeft:Show()
                            self.BorderBottom:Show()
                            self.BorderRight:Show()
                            self:SetText(self.targetAmount or "")
                            self:HighlightText()
                            editBox.lable:Hide()
                        end)
                        editBox:SetScript("OnEditFocusLost", function (self)
                            self.Background:Hide()
                            self.BorderTop:Hide()
                            self.BorderLeft:Hide()
                            self.BorderBottom:Hide()
                            self.BorderRight:Hide()
                            self:SetText("")
                            editBox.lable:Show()
                        end)
                        editBox:SetScript("OnEnterPressed", function (self)
                            local targetAmount = self:GetNumber()
                            if targetAmount == 0 and self.targetAmount and self.targetAmount > 0 then
                                --删除心理价
                                concernList[selectCharKey]["itemList"][button.itemId] = true
                                editBox.lable:SetText("心理价")
                                self.targetAmount = nil
                            elseif targetAmount > 0 then
                                --保存心理价
                                concernList[selectCharKey]["itemList"][button.itemId] = targetAmount
                                editBox.lable:SetText(ZL.formatNumber(targetAmount))
                                self.targetAmount = targetAmount
                            end
                            self:ClearFocus()
                        end)
                        editBox:SetScript("OnEnter", function (self)
                            if type(self.targetAmount) ~= "number" then
                                self.lable:SetText("心理价")
                            end
                        end)
                        editBox:SetScript("OnLeave", function (self)
                            if type(self.targetAmount) ~= "number" then
                                self.lable:SetText("")
                            end
                        end)

                        editBox.lable = editBox:CreateFontString(nil, "OVERLAY")
                        local lable = editBox.lable
                        lable:SetPoint("TOP", button, "BOTTOM", 0, 0)
                        lable:SetWidth(w+6)
                        lable:SetFont(GameFontNormal:GetFont(), 12, "OUTLINE")
                        lable:SetWordWrap(false)
                        lable:SetJustifyH("CENTER")
                        lable:SetTextColor(1, 1, 1, 1)
                        
                        button.levelFont = button:CreateFontString(nil, "OVERLAY")
                        button.levelFont:SetPoint("TOPLEFT", button, "TOPLEFT", 3, -3)
                        button.levelFont:SetFont(GameFontNormal:GetFont(), 11, "OUTLINE")
                        button.levelFont:SetWordWrap(false)
                        button.levelFont:SetJustifyH("CENTER")
                        button.levelFont:SetTextColor(1, 1, 1, 1)
                        
                        button.itemNumbersIcon = button:CreateTexture(nil, "OVERLAY")
                        button.itemNumbersIcon:SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", 0, 2)
                        button.itemNumbersIcon:SetSize(25, 25)
                        ConcernFrame.entries[i][j+1] = button
                    end
                    local btn = ConcernFrame.entries[i][j+1]
                    btn:Show()
                    btn.itemId = itemId
                    btn.row = i
                    btn.col = j + 1
                    btn.invType = invtype
                    local itemTexture = cGetItemIcon(itemId)
                    btn:SetNormalTexture(itemTexture)
                    btn.editBox.Background:Hide()
                    btn.editBox.BorderTop:Hide()
                    btn.editBox.BorderLeft:Hide()
                    btn.editBox.BorderBottom:Hide()
                    btn.editBox.BorderRight:Hide()
                    local targetAmount = concernList[selectCharKey]["itemList"][itemId]
                    targetAmount = type(targetAmount) == "number" and targetAmount or nil
                    btn.editBox.targetAmount = targetAmount
                    local lable = btn.editBox.lable
                    lable:Show()
                    lable:SetTextScale(1)
                    lable:SetText(targetAmount and ZL.formatNumber(targetAmount) or "")
                    local bili = lable:GetStringWidth()>0 and (lable:GetWidth())/(lable:GetStringWidth()+5) or 1
                    if bili < 1 then
                        lable:SetTextScale(bili)
                    else
                        lable:SetTextScale(1)
                    end
                    local itemTotalCount = select(4, ZL.GetItemNumbers(itemId))
                    if itemTotalCount > 0 then
                        btn.itemNumbersIcon:SetTexture([[interface/raidframe/readycheck-ready]])
                    else
                        btn.itemNumbersIcon:SetTexture()
                    end
                    local item = Item:CreateFromItemID(itemId)
                    if item:GetItemID() then
                        item:ContinueOnItemLoad(function()
                            local _, itemLink, _, itemLevel = cGetItemInfo(itemId)
                            btn.itemLink = itemLink
                            btn.levelFont:SetText(itemLevel)
                        end)
                    end
                end
            end
            maxWidth = max(maxWidth, (#concernList[selectCharKey]["slotInfo"][invtype]+1)*(w+15)+20)
        end
        ConcernFrame:SetResizeBounds(maxWidth, 250, maxWidth, GetScreenHeight()-60)
        ConcernFrame:SetWidth(maxWidth)
    end
end