Quantcast
local addonName, addon = ...
local L = addon.L

local class = UnitClass("player")

local info = {
    Druid = {
        ["Restoration (Healing)"] = {
            weights = {
                SPELL_POWER = 100,
                MASTERY_RATING = 100,
                HASTE_RATING = 57,
                INTELLECT = 51,
                SPIRIT = 32,
                CRIT_RATING = 11
            },
        },
        ["Feral (Tank)"] = {
            weights = {
                AGILITY = 100,
                MASTERY_RATING = 100,
                STAMINA = 75,
                DODGE_RATING = 65,
                EXPERTISE_RATING = 16,
                ARMOR = 10,
                STRENGTH = 10,
                HIT_RATING = 8,
                HASTE_RATING = 5,
                FERAL_ATTACK_POWER = 4,
                CRIT_RATING = 3,
            },
        },
        ["Feral (DPS)"] = {
            weights = {
                AGILITY = 100,
                MASTERY_RATING = 100,
                STRENGTH = 80,
                CRIT_RATING = 55,
                EXPERTISE_RATING = 50,
                HIT_RATING = 50,
                FERAL_ATTACK_POWER = 40,
                HASTE_RATING = 35,
            },
        },
        ["Balance (DPS)"] = {
            weights = {
                HIT_RATING = 100,
                MASTERY_RATING = 100,
                SPELL_POWER = 66,
                HASTE_RATING = 54,
                CRIT_RATING = 43,
                INTELLECT = 22,
                SPIRIT = 22,
            },
        },
    },
    Warrior = {
        ["Protection (Tank)"] = {
            weights = {
                STAMINA = 100,
                MASTERY_RATING = 100,
                DODGE_RATING = 90,
                PARRY_RATING = 67,
                AGILITY = 67,
                STRENGTH = 48,
                EXPERTISE_RATING = 19,
                HIT_RATING = 10,
                CRIT_RATING = 7,
                ARMOR = 6,
                HASTE_RATING = 1,
            },
        },
        ["Fury (DPS)"] = {
            weights = {
                MASTERY_RATING = 100,
                EXPERTISE_RATING = 100,
                STRENGTH = 82,
                CRIT_RATING = 66,
                AGILITY = 53,
                HIT_RATING = 48,
                HASTE_RATING = 36,
                ARMOR = 5,
            },
        },
        ["Arms (DPS)"] = {
            weights = {
                MASTERY_RATING = 100,
                STRENGTH = 100,
                HIT_RATING = 90,
                EXPERTISE_RATING = 85,
                CRIT_RATING = 80,
                AGILITY = 65,
                HASTE_RATING = 50,
                ARMOR = 1,
            },
        },
    },
}

local function GetWeights(link)
    if not info[class] then return {} end

    local scores = {}
    for wname, info in pairs(info[class]) do
        local stats = GetItemStats(link)
        if not link then
            return scores
        end
        local score = 0
        for stat, weight in pairs(info.weights) do
            local key = "ITEM_MOD_" .. stat .. "_SHORT"
            local value = stats[key]
            if not value then
                key = "ITEM_MOD_" .. stat
                value = stats[key]
            end
            if not value then
                value = 0
            end
            score = score + value * weight
        end

        scores[wname] = score
    end

    return scores
end


-- Hook all of the relevant tooltips
local lineAdded = {}
local function OnTooltipSetItem(tooltip, ...)
    if not lineAdded[tooltip] then
        -- Get the item link and fetch the scores
        local link = select(2, tooltip:GetItem())
        local scores = GetWeights(link)

        local sort = {}
        for name, score in pairs(scores) do
            table.insert(sort, name)
        end
        table.sort(sort)

        -- If all scores are 0, then don't display anything
        local max = 0
        for name, score in pairs(scores) do
            if score > max then
                max = score
            end
        end

        if max > 0 then
            tooltip:AddLine(" ")
            tooltip:AddLine("Equipment ratings from Wowhead.com:")
            for idx, name in ipairs(sort) do
                tooltip:AddDoubleLine(name .. ":", scores[name], 1.0, 1.0, 1.0)
            end
            tooltip:AddLine(" ")
        end
        lineAdded[tooltip] = true
    end
end

local function OnTooltipCleared(tooltip, ...)
    lineAdded[tooltip] = false
end

local tooltips = {
    GameTooltip,
    ItemRefTooltip,
    ShoppingTooltip1,
    ShoppingTooltip2,
    ShoppingTooltip3,
}

for idx, tooltip in ipairs(tooltips) do
    tooltip:HookScript("OnTooltipSetItem", OnTooltipSetItem)
    tooltip:HookScript("OnTooltipCleared", OnTooltipCleared)
end