local AddOnName, nsVars = ...
local ss = nsVars.SS

local setmetatable = setmetatable
local strlower, GetSpellCharges, string = strlower, GetSpellCharges, string
local select, UnitBuff, GetSpellCooldown = select, UnitBuff, GetSpellCooldown
local table = table

local getStormBringerMinMax = function(spell)
  -- Handle Stormstrike
  -- Stormstrike may have 2/3 (See Tempest talent) stacks with the Stormbringer buff, if active use this value
  local usingTempestTalent = select(4, GetTalentInfo(5,1,1))
  local max = usingTempestTalent and 3 or 2

  if UnitBuff("player", "Stormbringer") then
    local min = select(4, UnitBuff("player",  "Stormbringer"))
    return spell, min + 1, max
  else
    local start, dur = GetSpellCooldown(17364)

    if dur < max then
      return spell, 1, max
    else
      return spell, 0, max, start, dur
    end
  end
end

local getMinMax = function(spell)
  return spell, GetSpellCharges(spell)
end

local mtSpellChargeData = {
  ["demonhunter"] = {
    ["throwglaive"]    = { 185123, { r=0.227, g=0.788, b=0.533, a=1 }, getMinMax = getMinMax, name = "Throw Glaive",},
    ["felbarrage"]     = { 211053, { r=0.137, g=0.624, b=0.125, a=1 }, getMinMax = getMinMax, name = "Fel Barrage",},
    ["demonspikes"]    = { 203720, { r=0.784, g=0.725, b=0.000, a=1 }, getMinMax = getMinMax, name = "Demon Spikes",},
    ["infernalstrike"] = { 189110, { r=0.549, g=0.784, b=0.275, a=1 }, getMinMax = getMinMax, name = "Infernal Strike",},
    ["felrush"]        = { 195072, { r=0.176, g=0.569, b=0.353, a=1 }, getMinMax = getMinMax, name = "Fel Rush",},
  },
  ["warrior"] = {
    ["charge"]      = { 100, { r=0.784, g=0.176, b=0.118, a=1 }, getMinMax = getMinMax, name = "Charge",},
    ["shieldblock"] = { 2565, { r=0.294, g=0.549, b=0.784, a=1 }, getMinMax = getMinMax, name = "Shield Block",},
    ["intercept"]   = { 198304, { r=0.922, g=0.804, b=0.137, a=1 }, getMinMax = getMinMax, name = "Intercept",},
  },
  ["paladin"] = {
    ["divinesteed"]          = { 190784, { r=0.588, g=0.824, b=0.961, a=1 }, getMinMax = getMinMax, name = "Divine Steed",},
    ["shieldoftherighteous"] = { 53600, { r=0.471, g=0.137, b=0.627, a=1 }, getMinMax = getMinMax, name = "Shield of the Righteous",},
    ["blessedhammer"]        = { 204019, { r=0.549, g=0.255, b=0.509, a=1 }, getMinMax = getMinMax, name = "Blessed Hammer",},
    ["judgment"]             = { 20271, { r=0.843, g=0.627, b=0.098, a=1 }, getMinMax = getMinMax, name = "Judgement",},
    ["crusaderstrike"]       = { 35395, { r=1.000, g=0.863, b=0.431, a=1 }, getMinMax = getMinMax, name = "Crusader Strike",},
  },
  ["mage"] = {
    ["phoenixsflames"] = { 194466, { r=0.941, g=0.529, b=0.098, a=1 }, getMinMax = getMinMax, name = "Phoenix's Flames",},
    ["fireblast"]      = { 108853, { r=1.000, g=0.686, b=0.471, a=1 }, getMinMax = getMinMax, name = "Fire Blast",},
    ["shimmer"]        = { 212653, { r=0.706, g=0.549, b=1.000, a=1 }, getMinMax = getMinMax, name = "Shimmer",},
    ["icefloes"]       = { 108839, { r=0.333, g=0.627, b=0.804, a=1 }, getMinMax = getMinMax, name = "Ice Floes",},
    ["runeofpower"]    = { 116011, { r=0.451, g=0.863, b=1.000, a=1 }, getMinMax = getMinMax, name = "Rune of Power",},
  },
  ["shaman"] = {
    ["boulderfist"]        = { 201897, { r=0.588, g=0.333, b=0.118, a=1 }, getMinMax = getMinMax, name = "Boulderfist",},
    ["healingstreamtotem"] = { 5394, { r=0.510, g=0.882, b=1.000, a=1 }, getMinMax = getMinMax, name = "Healing Stream Totem",},
    ["riptide"]            = { 61295, { r=0.157, g=0.392, b=0.569, a=1 }, getMinMax = getMinMax, name = "Riptide",},
    ["lavaburst"]          = { 51505, { r=1.000, g=0.373, b=0.059, a=1 }, getMinMax = getMinMax, name = "Lava Burst",},
    ["stormstrike"]        = { 17364, { r=0.686, g=0.843, b=1.000, a=1 }, getMinMax = getStormBringerMinMax, name = "Stormstrike",},
  },
  ["priest"] = {
    ["angelicfeather"]  = { 121536, { r=1.000, g=0.737, b=0.318, a=1 }, getMinMax = getMinMax, name = "Angelic Feather",},
    ["shadowworddeath"] = { 199911, { r=1.000, g=0.392, b=0.000, a=1 }, getMinMax = getMinMax, name = "Shadow Word: Death",},
  },
  ["druid"] = {
    ["frenziedregeneration"] = { 22842, { r=0.588, g=0.353, b=0.157, a=1 }, getMinMax = getMinMax, name = "Frenzied Regeneration", },
    ["swiftmend"]            = { 18562, { r=0.196, g=0.314, b=0.549, a=1 }, getMinMax = getMinMax, name = "Swiftmend", },
  },
  ["warlock"] = {
    ["conflagrate"] = { 17962, { r=0.588, g=1.000, b=0.000, a=1 }, getMinMax = getMinMax, name = "Conflagrate", },
  },
  ["hunter"] = {
    ["mongoosebite"] = { 190928, { r=0.804, g=0.157, b=0.098, a=1 }, getMinMax = getMinMax, name = "Mongoose Bite", },
    ["throwingaxes"] = { 200163, { r=0.137, g=0.490, b=0.824, a=1 }, getMinMax = getMinMax, name = "Throwing Axes", },
  },
  ["monk"] = {
    ["roll"] = { 109132, { r=0.451, g=1.000, b=0.275, a=1 }, getMinMax = getMinMax, name = "Roll", },
--    ["ironskinbrew"] = { 115308, { r=0.529, g=1.000, b=0.843, a=1 }, getMinMax = getMinMax, name = "Ironskin Brew", },
--    ["purifyingbrew"] = { 119582, { r=0.392, g=0.882, b=0.510, a=1 }, getMinMax = getMinMax, name = "Purifying Brew", },
  },
  ["deathknight"] = {
    ["bloodboil"] = { 50842, { r=1.000, g=0.235, b=0.175, a=1 }, getMinMax = getMinMax, name = "Blood Boil", },
  },
};

mtSpellChargeData.hasSpells = function()
  return mtSpellChargeData[strlower(ss.curClass)] and true or nil
end

mtSpellChargeData.get = function(class, spell)
  local res = mtSpellChargeData[strlower(class)][strlower(spell)]

  if mtSpellChargeData[strlower(class)][strlower(spell)] then
    res.ssClass = "SpellChargeData"
    return res
  else
    return nil
  end
end

mtSpellChargeData.init = function(resource)
  local index = strlower(string.gsub(resource.name, " ", ""))
  index = strlower(string.gsub(index, ":", ""))
  index = strlower(string.gsub(index, "'", ""))

  if mtSpellChargeData[strlower(ss.curClass)][index] then
    resource.getMinMax = mtSpellChargeData[strlower(ss.curClass)][index].getMinMax
  else
    resource.getMinMax = function() return "",0,0,0,0 end
  end
end

ss["SpellChargeData"] = setmetatable({}, { __index = mtSpellChargeData });