YapiDB = YapiDB or {}

local raidReward1 = {
    ["LFR"]    = {drop1=233, drop2=237, drop3=240, drop4=243, dropTrack="Veteran",   crest="Champion"},
    ["Normal"] = {drop1=246, drop2=250, drop3=253, drop4=256, dropTrack="Champion",  crest="Hero"},
    ["Heroic"] = {drop1=259, drop2=263, drop3=266, drop4=269, dropTrack="Hero",      crest="Myth"},
    ["Mythic"] = {drop1=272, drop2=276, drop3=279, drop4=282, dropTrack="Myth",      crest=nil},
}
local raidReward2 = {
    ["LFR"]   = {drop1=237, drop2=240, drop3=243, dropTrack="Veteran", crest="Champion"},
    ["Normal"] = {drop1=250, drop2=255, drop3=256, dropTrack="Champion", crest="Hero"},
    ["Heroic"] = {drop1=263, drop2=266, drop3=269, dropTrack="Hero", crest="Myth"},
    ["Mythic"] = {drop1=276, drop2=279, drop3=282, dropTrack="Myth", crest=nil},
}

local delveReward = {
    [1]  = {drop=220, vault=233,   dropTrack="Adventurer", vaultTrack="Veteran", crest="", crestTrack=""},
    [2]  = {drop=224, vault=237,   dropTrack="Adventurer", vaultTrack="Veteran", crest="", crestTrack=""},
    [3]  = {drop=227, vault=240,   dropTrack="Adventurer", vaultTrack="Veteran", crest="", crestTrack=""},
    [4]  = {drop=230, vault=243,   dropTrack="Adventurer", vaultTrack="Veteran", crest=5, crestTrack="Adventurer", bounty=237, bountyTrack="Veteran"},
    [5]  = {drop=233, vault=246,   dropTrack="Veteran",    vaultTrack="Champion", crest=5, crestTrack="Veteran", bounty=243, bountyTrack="Veteran"},
    [6]  = {drop=237, vault=253,   dropTrack="Veteran",    vaultTrack="Champion", crest=5, crestTrack="Veteran", bounty=250, bountyTrack="Champion"},
    [7]  = {drop=246, vault=256,   dropTrack="Champion",   vaultTrack="Champion", crest=5, crestTrack="Champion", bounty=256, bountyTrack="Champion"},
    [8]  = {drop=250, vault=259,   dropTrack="Champion",   vaultTrack="Hero", crest="6+5", crestTrack="Champion", bounty=259, bountyTrack="Hero"},
    [9]  = {drop=250, vault=259,   dropTrack="Champion",   vaultTrack="Hero", crest="8+5", crestTrack="Champion", bounty=259, bountyTrack="Hero"},
    [10] = {drop=250, vault=259,   dropTrack="Champion",       vaultTrack="Hero", crest="10+5", crestTrack="Champion", bounty=259, bountyTrack="Hero"},
    [11] = {drop=250, vault=259,   dropTrack="Champion",       vaultTrack="Hero", crest="5+5", crestTrack="Hero", bounty=225, bountyTrack="Hero"},
}
--------
local dungeonReward = {
    ["Normal"]   = {drop=0,   vault=0,   dropTrack="",        vaultTrack="",        crest=0, crestTrack=""},
    ["Hero"]     = {drop=230, vault=243, dropTrack="Adventurer", vaultTrack="Veteran", crest=15, crestTrack="Champion"},
    [0]          = {drop=246, vault=256, dropTrack="Champion",   vaultTrack="Champion", crest=15, crestTrack="Champion"},
    [2]          = {drop=250, vault=259, dropTrack="Champion",   vaultTrack="Hero",     crest=18, crestTrack="Champion"},
    [3]          = {drop=250, vault=259, dropTrack="Champion",   vaultTrack="Hero",     crest=20, crestTrack="Champion"},
    [4]          = {drop=253, vault=263, dropTrack="Champion",   vaultTrack="Hero",     crest=12, crestTrack="Hero"},
    [5]          = {drop=256, vault=263, dropTrack="Champion",   vaultTrack="Hero",     crest=14, crestTrack="Hero"},
    [6]          = {drop=259, vault=266, dropTrack="Hero",       vaultTrack="Hero",     crest=16, crestTrack="Hero"},
    [7]          = {drop=259, vault=269, dropTrack="Hero",       vaultTrack="Hero",     crest=18, crestTrack="Hero"},
    [8]          = {drop=263, vault=269, dropTrack="Hero",       vaultTrack="Hero",     crest=20, crestTrack="Hero"},
    [9]          = {drop=263, vault=269, dropTrack="Hero",       vaultTrack="Hero",     crest=14, crestTrack="Myth"},
    [10]         = {drop=266, vault=272, dropTrack="Hero",       vaultTrack="Myth",     crest=16, crestTrack="Myth"},
    [11]         = {drop=266, vault=272, dropTrack="Hero",       vaultTrack="Myth",     crest=19, crestTrack="Myth"},
    [12]         = {drop=266, vault=272, dropTrack="Hero",       vaultTrack="Myth",     crest=20, crestTrack="Myth"},
}


local dungeonOrder = {"Normal", "Hero", 0,2,3,4,5,6,7,8,9,10,11,12}
local raidOrder = {"LFR", "Normal", "Heroic", "Mythic"}

local function iLevelTable(tableType, tableNo)
    local levelColor     = "|cff00ff00"
    local mythic         = levelColor .. "|cff00ff00M"
    local function itemTrack (track, iLevel)
        local array = {
            ["Adventurer"] = {text="冒险者", color="|cffffffff"},  
            ["Veteran"] = {text="老兵", color="|cff1eff00"},
            ["Champion"] = {text="勇士", color="|cff0070dd"},
            ["Hero"] = {text="英雄", color="|cffa335ee"},
            ["Myth"] = {text="神话", color="|cffff8000"},
        }
        local color = array[track].color
        if array[track] == nil or color == nil then
            return iLevel
        end
        if YapiDB.tiIColorCustom then
            color = ColorText("", YapiDB.tiIColor)
        end
        if YapiDB.tiIPrefix then
            return color .. array[track].text .. iLevel .. "|r"
        else
            return color .. iLevel .. "|r"
        end
    end

    local function crestTrack (track, crest)
        if track=="" or crest=="" then
            return nil
        end
        local array = {
            ["Adventurer"] = {text="冒险者", color="|cff1eff00"},
            ["Veteran"] = {text="老兵", color="|cff1eff00"},
            ["Champion"] = {text="勇士", color="|cff0070dd"},
            ["Hero"] = {text="英雄", color="|cffa335ee"},
            ["Myth"] = {text="神话", color="|cffff8000"},
        }
        local color = array[track].color
        if array[track] == nil or color == nil then
            return track .. crest
        end
        if YapiDB.tiCrestColorCustom then
            color = ColorText("", YapiDB.tiCrestColor)
        end
        if crest then
            return color .. array[track].text .. " " .. crest .. "|r"
        else
            return color .. array[track].text .. "|r"
        end
    end

    local function enhancedText(text, textType, padding)
        if YapiDB.tiDPrefix == "English" then
            mythic = ColorText("M", YapiDB.tiLevelColor)
        elseif YapiDB.tiDPrefix == "Chinese" then
            mythic = ColorText("史诗", YapiDB.tiLevelColor)
        elseif YapiDB.tiDPrefix == "none" then
            mythic = ColorText("", YapiDB.tiLevelColor)
        end
        if textType==("level") then
            if text == "Normal" then
                text = "普通"
                return text
            elseif text == "Hero" then
                text = ColorText("英雄", YapiDB.tiLevelColor)
                return text
            elseif type(text) == "number" and text<10 then
                text = mythic .. text .. "|r"
                return text
            elseif type(text) == "number" and text>=10 then
                text = mythic .. text .. "|r"
                return text
            end
            return text
        elseif textType==("item") then
                text = itemTrack(text.track, text.iLevel)
                if YapiDB.tiIPrefix then
                    return text
                end
                return text
        elseif textType==("crest") then
                text = crestTrack(text.crestTrack, text.crest)
                return text
        elseif textType==("difficulty") then
            if text == "LFR" then
                text = ColorText("随机", YapiDB.tiLevelColor)
            elseif text == "Normal" then
                text = ColorText("普通", YapiDB.tiLevelColor)
            elseif text == "Heroic" then
                text = ColorText("英雄", YapiDB.tiLevelColor)
            elseif text == "Mythic" then
                text = ColorText("史诗", YapiDB.tiLevelColor)
            end
            return text
        elseif textType==("delveLevel") then
            return ColorText(text, YapiDB.tiLevelColor)
        end
        return text
    end
   
    local function getLevel(value)
        if value == -1 then return "Normal"
        elseif value == 0 then return "Hero"
        elseif value == 1 then return 0
        end
        return value
    end
    local temp = {}
    local rewardTable = {}
    local data = {}
    local order = dungeonOrder
    if tableType == "Dungeon" then
        local min = getLevel(YapiDB.tiDungeonMinLevel)
        local max = getLevel(YapiDB.tiDungeonMaxLevel)
        order = dungeonOrder
        for _, level in ipairs(dungeonOrder) do
            data = dungeonReward[level]
            if level == "Normal" then
            elseif type(max) ~= "number" and type(level) == "number" then break
            elseif max == "Normal" and level == "Hero" then break
            elseif type(max) == "number" and type(level) == "number" and max < level then break
            elseif type(min) == "number" and type(level) == "number" and min > level then
            elseif type(min) == "number" and type(level) ~= "number" then
            else
                level = enhancedText(level, "level", 0)
                local drop = enhancedText({iLevel=data.drop, track=data.dropTrack}, "item", 0)
                local vault = enhancedText({iLevel=data.vault, track=data.vaultTrack}, "item", 1)
                local crest = enhancedText({crestTrack=data.crestTrack, crest=data.crest}, "crest", 0)
                if YapiDB.tiCrest then
                    table.insert(temp, {level, drop, vault, crest})
                else
                    table.insert(temp, {level, drop, vault})
                end
            end
        end
    elseif tableType == "Raid" then
        order = raidOrder
        for _, difficulty in ipairs(raidOrder) do
            data = raidReward1[difficulty]
            local drop1 = enhancedText({iLevel=data.drop1, track=data.dropTrack}, "item", 0)
            local drop2 = enhancedText({iLevel=data.drop2, track=data.dropTrack}, "item", 1)
            local drop3 = enhancedText({iLevel=data.drop3, track=data.dropTrack}, "item", 0)
            local drop4= enhancedText({iLevel=data.drop4, track=data.dropTrack}, "item", 0)
            --local crest = enhancedText({crest=data.crest}, "crest", 1)
            difficulty = enhancedText(difficulty, "difficulty", 0)
            
            if YapiDB.tiCrest and drop4 then
                table.insert(temp, {difficulty, drop1, drop2, drop3, drop4}) --, crest
            elseif YapiDB.tiCrest and not drop4 then
                table.insert(temp, {difficulty, drop1, drop2, drop3}) --, crest
            elseif not YapiDB.tiCrest and drop4 then
                table.insert(temp, {difficulty, drop1, drop2, drop3, drop4})
            elseif not YapiDB.tiCrest and not drop4  then
                table.insert(temp, {difficulty, drop1, drop2, drop3})
            end
        end
    elseif tableType == "Delve" then
        local min = getLevel(YapiDB.tiDelveMinLevel)
        local max = getLevel(YapiDB.tiDelveMaxLevel)
        for level, d in ipairs(delveReward) do
            if max < level then break
            elseif min > level then
            else
                local drop = enhancedText({iLevel=d.drop, track=d.dropTrack}, "item", 0)
                local vault = enhancedText({iLevel=d.vault, track=d.vaultTrack}, "item", 0)
                local bounty = enhancedText({iLevel=d.bounty, track=d.bountyTrack}, "item", 0)
                local crest = enhancedText({crestTrack=d.crestTrack, crest=d.crest}, "crest", 0)
                level = enhancedText(level, "delveLevel", 0)
                if YapiDB.tiDelveBounty and YapiDB.tiCrest then
                    table.insert(temp, {level, drop, vault, bounty, crest})
                elseif YapiDB.tiDelveBounty and not YapiDB.tiCrest then
                    table.insert(temp, {level, drop, vault, bounty})
                elseif not YapiDB.tiDelveBounty and YapiDB.tiCrest then
                    table.insert(temp, {level, drop, vault, crest})
                else
                    table.insert(temp, {level, drop, vault})
                end
            end
        end
    end
    return temp
end
----------------------------------------------------------------
TI_PreviewOptions = {
    { text = "鼠标左方", value="Left"},
    { text = "鼠标右方", value="Right"},
}

TI_DPrefixOptions = {
    { text = "只显示层数", value="none"},
    { text = "普通/英雄/史诗 + 层数", value="Chinese"},
    { text = "N/H/M + 层数", value="English"},
}

TI_ClickOptions = {
    { text = "无", value="none"},
    { text = "宏伟宝库", value="Vault"},
    --{ text = "冷却管理器-高级冷却设置", value="CoolDownManager"},
    { text = "史诗钥石", value="Keystone"},
    { text = "插件设置-装等表", value="IlvlTableSubcat", subcat="装等表"},
    { text = "插件设置-进入与离开战斗提示", value="CombatAlertSubcat", subcat="进入与离开战斗提示"},
    { text = "插件设置-聊天消息弹幕", value="ScrollingChatSubcat", subcat="聊天消息弹幕"},
    { text = "插件设置-鼠标提示增强", value="TooltipSubcat", subcat="鼠标提示增强"},
}


local function TI_ClickValuetoText(value)
    for _, option in ipairs(TI_ClickOptions) do
        if option.value == value then
            return option.text
        end
    end
    return nil
end

----------------------------------------------------------------
IconFrame = CreateFrame("Frame", "MyAlwaysIconFrame", UIParent)
IconFrame:SetSize(32, 32) -- width, height

local iconTexture = IconFrame:CreateTexture(nil, "ARTWORK")
iconTexture:SetAllPoints(IconFrame)
iconTexture:SetTexture("Interface\\Icons\\Inv_legion_paragoncache_armyofthelight")

local function RestorePosition()
    local x,y = IconFrame:GetCenter()
    IconFrame:ClearAllPoints()
    if x and y and YapiDB.point and x > 0 and y > -1200 then
        IconFrame:SetPoint(YapiDB.point, UIParent, YapiDB.relativePoint, YapiDB.xOfs, YapiDB.yOfs)
    else
        IconFrame:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 2, -50) -- default 
    end
end

IconFrame:SetMovable(true)
IconFrame:EnableMouse(true)
IconFrame:RegisterForDrag("LeftButton")

IconFrame:SetScript("OnDragStart", function(self)
    self:StartMoving()
end)

IconFrame:SetScript("OnDragStop", function(self)
    self:StopMovingOrSizing()

    local x,y = IconFrame:GetCenter()
    if x < 0 or y < -1200 or false then
        IconFrame:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 2, -50)
    end

    local point, _, relativePoint, xOfs, yOfs = self:GetPoint()
    YapiDB.point = point
    YapiDB.relativePoint = relativePoint
    YapiDB.xOfs = xOfs
    YapiDB.yOfs = yOfs
end)


local tableFrame = CreateFrame("Frame", "TableFrame", UIParent)
local function showTable()
    local fw = 0
    local frameWidth = 0
    local y = - YapiDB.tiCellHeight
    tableFrame = CreateFrame("Frame", "TableFrame", UIParent)
    local function setFontSize(text, fontSize)
        local font, _, flags = text:GetFont()
        text:SetFont(font, fontSize or YapiDB.tiFontSize, flags)
    end
    local function CreateCell(parent, text, justify, color)
        local fs = parent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        setFontSize(fs)
        fs:SetHeight(YapiDB.tiCellHeight)
        fs:SetJustifyH(justify)
        fs:SetJustifyV("MIDDLE")
        if color then
            fs:SetTextColor(unpack(color))
        end
        fs:SetText(text or "")
        return fs
    end
-----------------------------------
    local function createTable(tableType, columns, columns2)
        y = y - YapiDB.tiCellHeight
        local title = tableFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        title:SetPoint("TOPLEFT", 14, y)
        title:SetText(YapiDB["ti" .. tableType .. "Title"])
        setFontSize(title)
        local titleColor = YapiDB["ti" .. tableType .. "TitleColor"]
        title:SetTextColor(titleColor.r, titleColor.g, titleColor.b, titleColor.a)
        y = y - YapiDB.tiCellHeight

        local colOffsets = {0}
        for i = 1, #columns do
            if i == 1 then colOffsets[i] = 14 end
            colOffsets[i+1] = colOffsets[i] + columns[i].width + 14
            fw = fw + columns[i].width + 14
        end
        if fw > frameWidth then frameWidth = fw fw = 0 end

        for i, col in ipairs(columns) do
            local cell = CreateCell(tableFrame, col.name, col.justify, {1, 0.82, 0.2})
            cell:SetPoint("TOPLEFT", tableFrame, "TOPLEFT", colOffsets[i], y)
            cell:SetWidth(col.width)
        end
        y = y - YapiDB.tiCellHeight -- spacing after header
        
        if columns2 then
            for i, col in ipairs(columns2) do
                local cell = CreateCell(tableFrame, col.name, col.justify, {1, 0.82, 0.2})
                cell:SetPoint("TOPLEFT", tableFrame, "TOPLEFT", colOffsets[i], y)
                if i<3 then
                    cell:SetPoint("TOPLEFT", tableFrame, "TOPLEFT", colOffsets[i], y+YapiDB.tiCellHeight/2)
                end
                cell:SetWidth(col.width)
            end
            y = y - YapiDB.tiCellHeight -- spacing after header
        end

        local table = iLevelTable(tableType)
        for _, row in ipairs(table) do
            for col = 1, #row do
                local cell = CreateCell(tableFrame, row[col], columns[col].justify)
                cell:SetPoint("TOPLEFT", tableFrame, "TOPLEFT", colOffsets[col], y)
                cell:SetWidth(columns[col].width)
            end
            y = y - YapiDB.tiCellHeight
        end
        
        if YapiDB["ti" .. tableType .. "Note"] ~= "" then
            y = y - YapiDB.tiCellHeight
            local note = tableFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
            note:SetPoint("TOPLEFT", 14, y)
            setFontSize(note)
            note:SetText(YapiDB["ti" .. tableType .. "Note"])
            local noteColor = YapiDB["ti" .. tableType .. "NoteColor"]
            note:SetTextColor(noteColor.r, noteColor.g, noteColor.b, noteColor.a)
            y = y - YapiDB.tiCellHeight
        end
    end

-----------------------------------
    tableFrame:SetSize(420, 480)

    local title = tableFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    title:SetPoint("TOPLEFT", 14, -15)
    title:SetText("装等表")
    setFontSize(title, YapiDB.tiFontSize + 2)
    local columns = {}
    local w = YapiDB.tiCellWidth
    if YapiDB.tiDungeonEnabled then
        columns = {
            { name = "层数",   width = w,   justify = "LEFT" },
            { name = "掉落",   width = w,   justify = "LEFT"   },
            { name = "低保",   width = w,   justify = "LEFT"   },
        }
        if YapiDB.tiCrest then
            table.insert(columns, { name = "纹章", width = w + 20, justify = "LEFT" })
        end
        createTable("Dungeon", columns)
    end
    if YapiDB.tiRaidEnabled then
        
        local columns2 = {
            { name = "难度",   width = w,   justify = "LEFT" },
            { name = "虚影1",   width = w,   justify = "LEFT"   },
            { name = "虚影2-3",   width = w,   justify = "LEFT"   },
            { name = "虚影4-5", width = w, justify = "LEFT" },
            { name = "虚影6", width = w, justify = "LEFT" },
        }
        local columns1 = {
            { name = "",   width = w,   justify = "LEFT" },
            { name = "",   width = w,   justify = "LEFT" },
            { name = "梦境1",   width = w,   justify = "LEFT"   },
            { name = "奎岛1",   width = w,   justify = "LEFT"   },
            { name = "奎岛2",   width = w,   justify = "LEFT"   },
        }
        columns = {
            { name = "难度",   width = w,   justify = "LEFT" },
            { name = "1-3",   width = w,   justify = "LEFT"   },
            { name = "4-6",   width = w,   justify = "LEFT"   },
            { name = "7-8", width = w, justify = "LEFT" },
        }
        if YapiDB.tiRaidRare then
            --table.insert(columns, { name = "稀有掉落", width = w, justify = "LEFT" })
        end
        if YapiDB.tiCrest then
            --table.insert(columns, { name = "纹章", width = w, justify = "LEFT" })
            --table.insert(columns, { name = "虚影6纹章", width = w, justify = "LEFT" })
            --table.insert(columns, { name = "奎岛2纹章", width = w, justify = "LEFT" })
        end
        createTable("Raid", columns1, columns2)
    end
    
    if YapiDB.tiDelveEnabled then
        columns = {
            { name = "层数",   width = w,   justify = "LEFT" },
            { name = "丰裕",   width = w,   justify = "LEFT"   },
            { name = "低保",   width = w,   justify = "LEFT"   },
        }
        if YapiDB.tiDelveBounty then
            table.insert(columns, { name = "藏宝图", width = w, justify = "LEFT" })
        end
        if YapiDB.tiCrest then
            table.insert(columns, { name = "纹章", width = w*1.2, justify = "LEFT" })
        end
        createTable("Delve", columns)
    end

    y = y - YapiDB.tiCellHeight

    if false then
        local lbText = tableFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        lbText:SetPoint("TOPLEFT", 14, y)
        lbText:SetText("左键：" .. YapiDB.tiLB)
        y = y - YapiDB.tiCellHeight

        local rbText = tableFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        rbText:SetPoint("TOPLEFT", 14, y)
        rbText:SetText("右键：")
        y = y - YapiDB.tiCellHeight

        local mbText = tableFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        mbText:SetPoint("TOPLEFT", 14, y)
        mbText:SetText("中键：")
        y = y - YapiDB.tiCellHeight
    end

    tableFrame:SetSize(frameWidth, -y + 10)
    tableFrame:SetFrameStrata(YapiDB.tiTableStrata)
    tableFrame:SetPoint("BOTTOMLEFT", IconFrame, "TOPLEFT", 0, 0)
    local points = {"TOPLEFT"}
    local ptX,ptY = tableFrame:GetRight(), tableFrame:GetTop()
    if ptY > GetScreenHeight() then
        tableFrame:ClearAllPoints()
        tableFrame:SetPoint("TOPLEFT", IconFrame, "BOTTOMLEFT", 0, 0)
    end
    if ptX > GetScreenWidth() then
        tableFrame:ClearAllPoints()
        tableFrame:SetPoint("BOTTOMRIGHT", IconFrame, "TOPLEFT", 0, 0)
    end
    if ptY > GetScreenHeight() and ptX > GetScreenWidth() then
        tableFrame:ClearAllPoints()
        tableFrame:SetPoint("TOPRIGHT", IconFrame, "BOTTOMLEFT", 0, 0)
    end

    tableFrame.bg = tableFrame:CreateTexture(nil, "BACKGROUND")
    tableFrame.bg:SetAllPoints(tableFrame)
    tableFrame.bg:SetColorTexture(0, 0, 0, 0.9)
end
    

local function refreshTable(obj)
    tableFrame:Hide()
    tableFrame:ClearAllPoints()
    tableFrame:SetParent(nil)
end

-- Tooltip
IconFrame:SetScript("OnEnter", function(self)
    showTable()
end)

IconFrame:SetScript("OnLeave", function(self)
    tableFrame:Hide()
    tableFrame:ClearAllPoints()
    tableFrame:SetParent(nil)
end)

local function lock(isLock)
    IconFrame:SetMovable(not isLock)
    if isLock then
        IconFrame:RegisterForDrag()
    else
        IconFrame:RegisterForDrag("LeftButton")
    end
end

-------------------------------------

Frames = {}
local frame = Frames["装等表"]

TI_Options = {
    {variable = "", label="通用", category = frame, type = "Header"
    },
    {variable = "tiEnable", label="启用", category = frame, type = "Checkbox", default = true,
    onClick = function()
        IconFrame:SetShown(YapiDB.tiEnable)
        tableFrame:Hide()
    end
    },
    {variable = "tiLock", label="锁定", category = frame, type = "Checkbox", default = false,
    onClick = function()
        lock(YapiDB.tiLock)
    end, inline = true
    },
    --{variable = "tiPreview", label="预览", category = frame, type = "Checkbox", default = false, inline = true, onClick = function() if YapiDB.tiPreview then showTable(IconFrame) else tableFrame:Hide() end end },
    --{variable = "tiPosition", label="显示位置", category = frame, type="Dropdown", default="Right", dropdownOptions = TI_PreviewOptions, onValueChanged = function()  end},
    {variable = "tiIconHeight", label="图标高度", category = frame, type = "Slider",
    default = 50, min = 1, max = 200, unit = "",
    onValueChanged = function()
        IconFrame:SetSize(YapiDB.tiIconWidth or YapiDB.tiIconHeight, YapiDB.tiIconHeight)
     end
    },
    {variable = "tiIconWidth", label="图标宽度", category = frame, type = "Slider",
    default = 50, min = 1, max = 200, inline = true, unit = "",
    onValueChanged = function()
        IconFrame:SetSize(YapiDB.tiIconWidth, YapiDB.tiIconHeight) end
    },
    --{variable = "tiIconFrame", label="启用图标外框", category = frame, type = "Checkbox", default = true, onClick = function() end },
    --{variable = "tiIconFrameColor", label="图标外框颜色", category = frame, type = "Color", default = Colors.White, inline = true, onClick = function() end},
    --{variable = "tiIconFrameThickness", label="图标外框粗细", category = frame, type = "Slider", min = 1, max = 10, default = 2, inline = true, onClick = function() end },
    {variable = "tiFontSize", label="字体大小", category = frame, type = "Slider",
    default = 16, min = 1, max = 50,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiCellHeight", label="表格高度", category = frame, type = "Slider",
    default = 20, min = 1, max = 80, unit = "",
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiCellWidth", label="表格宽度", category = frame, type = "Slider",
    default = 70, min = 1, max = 200, unit = "", inline = true,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiIconStrata", label="图标框体层级", category = frame, type = "Dropdown", default="MEDIUM",
    dropdownOptions = StrataOptions,
    onValueChanged = function() IconFrame:SetFrameStrata(YapiDB.tiIconStrata) end
    },
    {variable = "tiTableStrata", label="表格框体层级", category = frame, type = "Dropdown", default="MEDIUM", inline=true,
    dropdownOptions = StrataOptions,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiLB", label="左键", category = frame, type = "Dropdown", default="Vault",
    dropdownOptions = TI_ClickOptions,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiRB", label="右键", category = frame, type = "Dropdown", default="IlvlTableSubcat", inline=true,
    dropdownOptions = TI_ClickOptions,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiIPrefix", label="显示装备品质", category = frame, type = "Checkbox", default = false,
    onClick = function() refreshTable(IconFrame) end
    },
    {variable = "tiIColorCustom", label="自定义装等颜色", category = frame, type="Checkbox", default=false,inline=true,
    onClick = function() refreshTable(IconFrame) end
    },
    {variable = "tiIColor", label="装等颜色", category = frame, type="Color", default={r=1,g=1,b=1,a=1}, inline=true,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiCrest", label="显示纹章掉落", category = frame, type = "Checkbox", default = true,
    onClick = function() refreshTable(IconFrame) end
    },
    {variable = "tiCrestColorCustom", label="自定义纹章颜色",  category = frame, type="Checkbox", default=false,inline=true,
    onClick = function() refreshTable(IconFrame) end
    },
    {variable = "tiCrestColor", label="纹章颜色", category = frame, type = "Color", default={r=1,g=1,b=1,a=1}, inline=true,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiLevelColor", label="层数与难度颜色", category = frame, type="Color", default={r=0,g=1,b=0,a=1},
    onValueChanged = function() refreshTable(IconFrame) end
    },
------------------------
    {variable = "", label="地下城", category = frame, type = "Header"
    },
    {variable = "tiDungeonEnabled", label="启用", category = frame, type = "Checkbox", default = "地下城",
    onClick = function() refreshTable(IconFrame) end
    },
    {variable = "tiDungeonTitle", label="标题", category = frame, type = "InputBox", default = "地下城",
    onTextChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiDungeonTitleColor", label="标题颜色", category = frame, type = "Color", default={r=1,g=1,b=1,a=1}, inline=true,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiDungeonNote", label="备注", category = frame, type = "InputBox", default = "",
    onTextChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiDungeonNoteColor", label="备注颜色", category = frame, type = "Color", default={r=1,g=1,b=1,a=1}, inline=true,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiDPrefix", label="显示难度与层数", category = frame, type="Dropdown", default="Chinese",
    dropdownOptions = TI_DPrefixOptions,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiDungeonMinLevel", label="最低显示层数", category = frame, type = "Slider",
    default = 2, min = -0, max = 12,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiDungeonMaxLevel", label="最高显示层数", category = frame, type = "Slider", inline = true,
    default = 10, min = -0, max = 12,
    onValueChanged = function() refreshTable(IconFrame) end
    },
------------------------
    {variable = "", label="团队副本", category = frame, type = "Header"
    },
    {variable = "tiRaidEnabled", label="启用", category = frame, type = "Checkbox", default = true,
    onClick = function() refreshTable(IconFrame) end
    },
    {variable = "tiRaidTitle", label="标题", category = frame, type = "InputBox", default = "团队副本",
    onTextChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiRaidTitleColor", label="标题颜色", category = frame, type = "Color", default={r=1,g=1,b=1,a=1}, inline=true,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiRaidNote", label="备注", category = frame, type = "InputBox", default = "",
    onTextChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiRaidNoteColor", label="备注颜色", category = frame, type = "Color", default={r=1,g=1,b=1,a=1}, inline=true,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    --{variable = "tiRaidRare", label="显示稀有掉落装等", category = frame, type = "Checkbox", default = false, onClick = function() refreshTable(IconFrame) end },
------------------------
    {variable = "", label="地下堡", category = frame, type = "Header"
    },
    {variable = "tiDelveEnabled", label="启用", category = frame, type = "Checkbox", default = false,
    onClick = function() refreshTable(IconFrame) end
    },
    {variable = "tiDelveTitle", label="标题", category = frame, type = "InputBox", default = "地下堡",
    onTextChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiDelveTitleColor", label="标题颜色", category = frame, type = "Color", default={r=1,g=1,b=1,a=1}, inline=true,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiDelveNote", label="备注", category = frame, type = "InputBox", default = "",
    onTextChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiDelveNoteColor", label="备注颜色", category = frame, type = "Color", default={r=1,g=1,b=1,a=1}, inline=true,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiDelveBounty", label="显示藏宝图", category = frame, type = "Checkbox", default = true, onClick = function() refreshTable(IconFrame) end },
    {variable = "tiDelveMinLevel", label="最低显示层数", category = frame, type = "Slider",
    default = 4, min = 1, max = 11,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    {variable = "tiDelveMaxLevel", label="最高显示层数", category = frame, type = "Slider", inline = true,
    default = 8, min = 1, max = 11,
    onValueChanged = function() refreshTable(IconFrame) end
    },
    
}
--CreateOptions(TI_Options)
--UpdateContentHeight(frame)


local function iconClick(option)
    local addon = nil
    local function load(addOn)
        if not C_AddOns.IsAddOnLoaded(addOn) then
            local loaded, reason = C_AddOns.LoadAddOn(addOn)
        end
    end
    if option == "Vault" then
        addon = "Blizzard_WeeklyRewards"
        load(addon)
        WeeklyRewardsFrame:Show()
    elseif option == "Keystone" then
        PVEFrame_ShowFrame("ChallengesFrame")
    elseif option == "CoolDownManager" then

        
    elseif string.find(option, "Subcat") then
        for _, opt in ipairs(TI_ClickOptions) do
            if option == opt["value"] then
                Settings.OpenToCategory(opt["subcat"]:GetID())
            end
            
        end
    end
end

IconFrame:SetScript("OnMouseUp", function(self, button)
    if button == "LeftButton" then
        iconClick(YapiDB.tiLB)
    elseif button == "RightButton" then
        iconClick(YapiDB.tiRB)
    end
end)
--[[---------------------------------
frame:HookScript("OnShow", function()
end)

frame:HookScript("OnHide", function()
    GameTooltip:Hide()
end)



EventListenerFrame:HookScript("OnEvent", function(self, event)
    if event == "ADDON_LOADED" then
        InitialzeDB(TI_Options)
        RestorePosition()
        IconFrame:SetFrameStrata(YapiDB.tiIconStrata)
        IconFrame:Show()
        IconFrame:SetShown(YapiDB.tiEnable)
        IconFrame:SetSize(YapiDB.tiIconWidth, YapiDB.tiIconHeight) -- width, height
        lock(YapiDB.tiLock)
        --Settings.OpenToCategory(CombatAlertSubcat.handle:GetID())
    end
end)]]

