----------------------------------------
-- Buff Information for Micro Spheres --
----------------------------------------
local AddOnName, nsVars = ...
local ss = nsVars.SS

local setmetatable = setmetatable
local UnitDebuff, UnitStagger, UnitHealthMax = UnitDebuff, UnitStagger, UnitHealthMax
local string, GetTime, tonumber, strlower, select = string, GetTime, tonumber, strlower, select

local getMinMax = function(buff)
  local duration, expires = select(6, UnitDebuff("target", buff))
  return buff, (duration and 1 or 0), 1, (duration or 1), (expires or 1)
end

-- Used for buffs that have stacks.
local getStackingMinMax = function(spell)
  local buffName = string.gsub(spell, "%d", "")
  local index = strlower(string.gsub(spell, "[ :'%(%)%d]", ""))
  
  local stacks, _, duration, expires = select(4, UnitDebuff("target", buffName))
  
  if stacks then
    local buffStack = string.gsub(spell, "[^%d]", "")
    buffStack = (buffStack and buffStack ~= "") and tonumber(buffStack) or 1
    
    duration = (stacks >= buffStack) and duration or nil
    expires = (stacks >= buffStack) and expires or nil
  end
  
  return index, (duration and 1 or 0), 1, (duration or 1), (expires or 1)
end

local mtDebuffData = {
  warrior = {
    ["colossussmash"]          = { "Colossus Smash",           { r=220, g=150, b=25, a=1 },        getMinMax = getMinMax,         name = "Colossus Smash",},
    ["executionersprecision"]  = { "Executioner's Precision",  { r=0.843, g=0.196, b=0.078, a=1 }, getMinMax = getStackingMinMax, name = "Executioner's Precision",},
    ["executionersprecision2"] = { "Executioner's Precision2", { r=0.843, g=0.196, b=0.078, a=1 }, getMinMax = getStackingMinMax, name = "Executioner's Precision",},
  },
};

mtDebuffData.get = function(class, buff)
  local res = mtDebuffData[strlower(class)][strlower(buff)]
  res.ssClass = "DebuffData"
  return res
end

mtDebuffData.init = function(resource)
  local index = strlower(string.gsub(resource[1], "[ :'%(%)]", ""))
  
  if mtDebuffData[ss.curClass][index] then
    -- update identifier, color and getMinMax functions.
    resource[1]        = mtDebuffData[ss.curClass][index][1]
    resource[2]        = mtDebuffData[ss.curClass][index][2]
    resource.getMinMax = mtDebuffData[ss.curClass][index].getMinMax
  else
    resource.getMinMax = function() return "",0,0,0,0 end
  end
end

ss["DebuffData"] = setmetatable({}, { __index = mtDebuffData });
