---------------------------------------------------
-- Caster Stats v0.9.5.1 by RMS / (mod) 2018
---------------------------------------------------
--[[

Adds a line to the stat section of the character frame below armor. This line can be
set to either show your total "+Spell Damage" or "+Healing" via the command line. 
Like the other stats in the character screen it provides more information when you 
mouse over it. The tooltip will display the totals for plus healing, spell damage, 
all individual schools of damage, spell crit, spell to hit, mana regen etc...

The information that is displayed is only additional stats provided by your gear. 
If you do not have any of the the listed stats it will not be visible. Caster Stats 
does not currently calculate crit, or mana regen from stats like int, spirit, or 
base crit. It is only mean't to be a quick way of finding the bonuses you are 
getting from your gear. I may implement more features in the future however.

While there are other mods out there that can total these stats for you, I wanted 
something that was more intune with the melee character stats. Something simple 
yet informative.

-------------------------------------------------------------------------------------
(mod) 2018  Merged with BonusScanner and altered/improved in many ways

v0.9.5.1
-- Now totals +damage to individual schools of magic as before

v0.9.5
-- Updated for 1.10
-- Now uses Bonus Scanner (http://www.curse-gaming.com/mod.php?addid=2384) to find bonuses instead of reinventing the wheel each patch/new item stat with my own scanner. This also allows prevents FR DE localizations from needing to be created multiple times. Bonusscanner is included in the download.
-- Added detection for negative spell resist to target (not yet implemented by Bonus Scanner)
-- Relocated the stats to be inside the frame with other stats
-- As before Caster Stats is not visible if you are a Warrior/Rogue, but now in addition healing does not show if you are not a healing class
-- Added FE DE localization. Please report if they are functional as I reformatted previous submissions. Will need modified version for both for the negative resists either way.

v0.9.4
-- Updated for 1.9
-- Change the comand line to /cstats to be compatible with "Combat Stats"
-- Mana regen now works again as wording was changed in 1.9
-- Wizard and Brilliant Oils are now detected

v0.9.3
-- Sets are now correctly identified and set bonuses only counted once.
-- Now also totals health regeneration.
-- Added the Winter's Might enchant.

v0.9.2
-- Updated mana regen and spell crit detection.

v0.9.1
-- Now detects TankPoints and moves CasterStats to a new location if needed.
-- Fixed issues with +spell crit and +spell hit not showing under certain conditions.
-- Fixed issues where values weren't getting reset on rescan.

]]-- 

local casterStats = {
    stats = {};
};

function CS_OnLoad()
    -- Register the command prompt command
    --SLASH_CSTATS1 = "/cstats";  
    --SlashCmdList["CSTATS"] = CS_CommandHandler;

    -- Hook the Bonus Scanner function
    BonusScanner_Update = UpdateCS;
end


-- Rescan the inventory
function UpdateCS()
    -- Only update if the Character Frame is visible to the user
    if (CharacterFrame:IsVisible()) then
        -- Reacquire the stats information
        for i, type in BonusScanner.types do
            casterStats.stats[type] = BonusScanner:GetBonus(type);
        end

        if (ThereAreStats() or 1) then
            SetFrameValues();
        else
            ClearFrameValues();
        end
    end
end


-- Setup and show the tooltip on mouse over
function CSFrame_OnEnter()

   GameTooltip:SetOwner(this, "ANCHOR_RIGHT");
   GameTooltip:SetText("Bonuses from Equipment", 1, 1, 1);

   -- Loop through all the stats and add the ones that exist to the tooltip
   for i, type in BonusScanner.types do
    if (casterStats.stats[type] ~= 0) then
        if (string.find(type, ".+DMG")) then
            GameTooltip:AddDoubleLine(BONUSSCANNER_NAMES[type], "(+" .. casterStats.stats[type] .. ") +" .. (casterStats.stats[type] + casterStats.stats['DMG']), NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b, 0, 1, 0);
        -- Check for NEGRES TYPE.
        elseif (string.find(type, "NEGRES")) then
            GameTooltip:AddDoubleLine(BONUSSCANNER_NAMES[type], "-" .. casterStats.stats[type], NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b, 0, 1, 0);
        -- Check for REGEN TYPE.
        elseif (string.find(type, "REG")) then
            GameTooltip:AddDoubleLine(BONUSSCANNER_NAMES[type], casterStats.stats[type] .. "/5s", NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b, 0, 1, 0);
        -- Check for CRIT/HIT TYPE.
        elseif (string.find(BONUSSCANNER_NAMES[type], "%%")) then
            GameTooltip:AddDoubleLine(BONUSSCANNER_NAMES[type], "+" .. casterStats.stats[type] .. " %", NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b, 0, 1, 0);
        -- No special output
        else
            GameTooltip:AddDoubleLine(BONUSSCANNER_NAMES[type], "+" .. casterStats.stats[type], NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b, 0, 1, 0);
        end
    end

    GameTooltip:Show();
   end
end


-- Populate the frame
function SetFrameValues()
    local text = getglobal("CSFrameText");

    local cl = UnitClass("player")
    if cl == "Warrior" or cl == "Rogue" then
       local base, bonus = UnitDefense("player")
       text:SetText((base+bonus - 5*UnitLevel("player")).."+");
    else
       text:SetText("+"..casterStats.stats["DMG"]);
    end
end


-- Empty the frame
function ClearFrameValues()
    local text = getglobal("CSFrameText");
    text:SetText("");
end


-- Returns true if at least 1 stat has been recorded
function ThereAreStats()
    for i, currentStat in casterStats.stats do
        if (currentStat ~= 0) then
            return true;
        end
    end
end


--[[ Handles the command line
function CS_CommandHandler( msg )
    if (msg == CS_DMG_TOGGLE) then
        reportThis = 0;
        UpdateCS();
        DEFAULT_CHAT_FRAME:AddMessage(CS_FEEDBACK_STATCHANGE.." \""..CS_DMG_TOGGLE.."\"");
    elseif (msg == CS_HEALING_TOGGLE) then
        reportThis = 1;
        UpdateCS();
        DEFAULT_CHAT_FRAME:AddMessage(CS_FEEDBACK_STATCHANGE.." \""..CS_HEALING_TOGGLE.."\"");
    else
        if(reportThis == 0) then
                reportThis = 1;
            else
                reportThis = 0;
            end 
    end
end
]]
