--------------------------------
-- Class Resource Information --
--------------------------------
local AddOnName, nsVars = ...
local ss = nsVars.SS

local setmetatable = setmetatable
local strlower = strlower

local mtClassResources = {
  classModes = {
    deathknight = {
      ["1"] = { --blood
        ["default"] = { {0, ss.ClassResourceData.classColor("deathknight")}, ss.ClassResourceData.resourceInfo("runic power"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("deathknight")}, ss.ClassResourceData.resourceInfo("runic power"), },
      },
      ["2"] = { --frost
        ["default"] = { {0, ss.ClassResourceData.classColor("deathknight")}, ss.ClassResourceData.resourceInfo("runic power"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("deathknight")}, ss.ClassResourceData.resourceInfo("runic power"), },
      },
      ["3"] = { --unholy
        ["default"] = { {0, ss.ClassResourceData.classColor("deathknight")}, ss.ClassResourceData.resourceInfo("runic power"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("deathknight")}, ss.ClassResourceData.resourceInfo("runic power"), },
      },
    },
    demonhunter = {
      ["1"] = { --havoc
        ["default"] = { {0, ss.ClassResourceData.classColor("demonhunter")}, ss.ClassResourceData.resourceInfo("fury"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("demonhunter")}, ss.ClassResourceData.resourceInfo("fury"), },
      },
      ["2"] = { --vengeance
        ["default"] = { {0, ss.ClassResourceData.classColor("demonhunter")}, ss.ClassResourceData.resourceInfo("pain"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("demonhunter")}, ss.ClassResourceData.resourceInfo("pain"), },
      },
    },
    druid = {
      ["1"] = { --balance
        ["default"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("mana"),
          ss.ClassResourceData.resourceInfo("energy"),
          ss.ClassResourceData.resourceInfo("lunar power"),
        },
        ["combat"]  = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("mana"),
          ss.ClassResourceData.resourceInfo("energy"),
          ss.ClassResourceData.resourceInfo("lunar power"),
        },
        ["bear"]    = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("rage"),
          ss.ClassResourceData.resourceInfo("energy"),
          ss.ClassResourceData.resourceInfo("mana"),
        },
        ["cat"]     = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("energy"),
          ss.ClassResourceData.resourceInfo("lunar power"),
          ss.ClassResourceData.resourceInfo("mana"),
        },
        ["moonkin"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("lunar power"),
          ss.ClassResourceData.resourceInfo("mana"),
        },
      },
      ["2"] = { --feral
        ["default"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("mana"),
          ss.ClassResourceData.resourceInfo("energy"),
        },
        ["combat"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("mana"),
          ss.ClassResourceData.resourceInfo("energy"),
        },
        ["bear"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("rage"),
          ss.ClassResourceData.resourceInfo("energy"),
        },
        ["cat"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("energy"),
          ss.ClassResourceData.resourceInfo("mana"),
        },
        ["moonkin"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("lunar power"),
          ss.ClassResourceData.resourceInfo("energy"),
          ss.ClassResourceData.resourceInfo("mana"),
        },
      },
      ["3"] = { --guardian
        ["default"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("mana"),
          ss.ClassResourceData.resourceInfo("energy"),
          ss.ClassResourceData.resourceInfo("rage"),
        },
        ["combat"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("mana"),
          ss.ClassResourceData.resourceInfo("energy"),
          ss.ClassResourceData.resourceInfo("rage"),
        },
        ["bear"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("rage"),
          ss.ClassResourceData.resourceInfo("mana"),
        },
        ["cat"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("energy"),
          ss.ClassResourceData.resourceInfo("rage"),
        },
        ["moonkin"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("lunar power"),
          ss.ClassResourceData.resourceInfo("energy"),
          ss.ClassResourceData.resourceInfo("rage"),
        },
      },
      ["4"] = { --restoration
        ["default"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("mana"),
          ss.ClassResourceData.resourceInfo("energy"),
        },
        ["combat"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("mana"),
          ss.ClassResourceData.resourceInfo("energy"),
        },
        ["bear"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("rage"),
          ss.ClassResourceData.resourceInfo("energy"),
          ss.ClassResourceData.resourceInfo("mana"),
        },
        ["cat"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("energy"),
          ss.ClassResourceData.resourceInfo("mana"),
        },
        ["moonkin"] = {
          {0, ss.ClassResourceData.classColor("druid")},
          ss.ClassResourceData.resourceInfo("lunar power"),
          ss.ClassResourceData.resourceInfo("energy"),
          ss.ClassResourceData.resourceInfo("mana"),
        },
      },
    },
    hunter = {
      ["1"] = { --beast mastery
        ["default"] = { {0, ss.ClassResourceData.classColor("hunter")}, ss.ClassResourceData.resourceInfo("focus"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("hunter")}, ss.ClassResourceData.resourceInfo("focus"), },
      },
      ["2"] = { --marksmanship
        ["default"] = { {0, ss.ClassResourceData.classColor("hunter")}, ss.ClassResourceData.resourceInfo("focus"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("hunter")}, ss.ClassResourceData.resourceInfo("focus"), },
      },
      ["3"] = { --survival
        ["default"] = { {0, ss.ClassResourceData.classColor("hunter")}, ss.ClassResourceData.resourceInfo("focus"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("hunter")}, ss.ClassResourceData.resourceInfo("focus"), },
      },
    },
    mage = {
      ["1"] = { --arcane
        ["default"] = { {0, ss.ClassResourceData.classColor("mage")}, ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("mage")}, ss.ClassResourceData.resourceInfo("mana"), },
      },
      ["2"] = { --fire
        ["default"] = { {0, ss.ClassResourceData.classColor("mage")}, ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("mage")}, ss.ClassResourceData.resourceInfo("mana"), },
      },
      ["3"] = { --frost
        ["default"] = { {0, ss.ClassResourceData.classColor("mage")}, ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("mage")}, ss.ClassResourceData.resourceInfo("mana"), },
      },
    },
    monk = {
      ["1"] = { --brewmaster
        ["default"] = { {0, ss.ClassResourceData.classColor("monk")}, ss.ClassResourceData.resourceInfo("energy"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("monk")}, ss.ClassResourceData.resourceInfo("energy"), },
      },
      ["2"] = { -- mistweaver
        ["default"] = { {0, ss.ClassResourceData.classColor("monk")}, ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("monk")}, ss.ClassResourceData.resourceInfo("mana"), },
      },
      ["3"] = { --windwalker
        ["default"] = { {0, ss.ClassResourceData.classColor("monk")}, ss.ClassResourceData.resourceInfo("energy"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("monk")}, ss.ClassResourceData.resourceInfo("energy"), },
      },
    },
    paladin = {
      ["1"] = { --holy
        ["default"] = { {0, ss.ClassResourceData.classColor("paladin")}, ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("paladin")}, ss.ClassResourceData.resourceInfo("mana"), },
      },
      ["2"] = { --protection
        ["default"] = { {0, ss.ClassResourceData.classColor("paladin")}, ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("paladin")}, ss.ClassResourceData.resourceInfo("mana"), },
      },
      ["3"] = { --retribution
        ["default"] = { {0, ss.ClassResourceData.classColor("paladin")}, ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("paladin")}, ss.ClassResourceData.resourceInfo("mana"), },
      },
    },
    priest = {
      ["1"] = { -- discipline
        ["default"] = { {0, ss.ClassResourceData.classColor("priest")}, ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("priest")}, ss.ClassResourceData.resourceInfo("mana"), },
      },
      ["2"] = { --holy
        ["default"] = { {0, ss.ClassResourceData.classColor("priest")}, ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("priest")}, ss.ClassResourceData.resourceInfo("mana"), },
      },
      ["3"] = { --shadow
        ["default"] = { {0, ss.ClassResourceData.classColor("priest")}, ss.ClassResourceData.resourceInfo("insanity"), ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("priest")}, ss.ClassResourceData.resourceInfo("insanity"), ss.ClassResourceData.resourceInfo("mana"), },
      },
    },
    rogue = {
      ["1"] = { --assassination
        ["default"] = { {0, ss.ClassResourceData.classColor("rogue")}, ss.ClassResourceData.resourceInfo("energy"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("rogue")}, ss.ClassResourceData.resourceInfo("energy"), },
      },
      ["2"] = { --outlaw
        ["default"] = { {0, ss.ClassResourceData.classColor("rogue")}, ss.ClassResourceData.resourceInfo("energy"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("rogue")}, ss.ClassResourceData.resourceInfo("energy"), },
      },
      ["3"] = { --subtlety
        ["default"] = { {0, ss.ClassResourceData.classColor("rogue")}, ss.ClassResourceData.resourceInfo("energy"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("rogue")}, ss.ClassResourceData.resourceInfo("energy"), },
      },
    },
    shaman = {
      ["1"] = { --elemental
        ["default"] = { {0, ss.ClassResourceData.classColor("shaman")}, ss.ClassResourceData.resourceInfo("maelstrom"), ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("shaman")}, ss.ClassResourceData.resourceInfo("maelstrom"), ss.ClassResourceData.resourceInfo("mana"), },
      },
      ["2"] = { --enhancement
        ["default"] = { {0, ss.ClassResourceData.classColor("shaman")}, ss.ClassResourceData.resourceInfo("maelstrom"), ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("shaman")}, ss.ClassResourceData.resourceInfo("maelstrom"), ss.ClassResourceData.resourceInfo("mana"), },
      },
      ["3"] = { --restoration
        ["default"] = { {0, ss.ClassResourceData.classColor("shaman")}, ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("shaman")}, ss.ClassResourceData.resourceInfo("mana"), },
      },
    },
    warlock = {
      ["1"] = { --affliction
        ["default"] = { {0, ss.ClassResourceData.classColor("warlock")}, ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("warlock")}, ss.ClassResourceData.resourceInfo("mana"), },
      },
      ["2"] = { --demonology
        ["default"] = { {0, ss.ClassResourceData.classColor("warlock")}, ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("warlock")}, ss.ClassResourceData.resourceInfo("mana"), },
      },
      ["3"] = { --destruction
        ["default"] = { {0, ss.ClassResourceData.classColor("warlock")}, ss.ClassResourceData.resourceInfo("mana"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("warlock")}, ss.ClassResourceData.resourceInfo("mana"), },
      },
    },
    warrior = {
      ["1"] = { --arms
        ["default"] = { {0, ss.ClassResourceData.classColor("warrior")}, ss.ClassResourceData.resourceInfo("rage"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("warrior")}, ss.ClassResourceData.resourceInfo("rage"), },
      },
      ["2"] = { --fury
        ["default"] = { {0, ss.ClassResourceData.classColor("warrior")}, ss.ClassResourceData.resourceInfo("rage"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("warrior")}, ss.ClassResourceData.resourceInfo("rage"), },
      },
      ["3"] = { --protection
        ["default"] = { {0, ss.ClassResourceData.classColor("warrior")}, ss.ClassResourceData.resourceInfo("rage"), },
        ["combat"]  = { {0, ss.ClassResourceData.classColor("warrior")}, ss.ClassResourceData.resourceInfo("rage"), },
      },
    },
  },
};

mtClassResources.getModes = function(self, class)
  return self.classModes[strlower(class)]
end

mtClassResources.hasSecondaryResource = function()
  return ss.db.additionalResource.modes[ss.curSpec] and ss.db.additionalResource.modes[ss.curSpec]["default"] or false
end

mtClassResources.initModes = function()
  ss.db.health.modes             = {};
  ss.db.mainResource.modes       = {};
  ss.db.additionalResource.modes = {};

  ss.profile.modeInfo.health.modeSpec             = ss.curSpec
  ss.profile.modeInfo.mainResource.modeSpec       = ss.curSpec
  ss.profile.modeInfo.additionalResource.modeSpec = ss.curSpec

  ss.profile.modeInfo.health.mode             = "default"
  ss.profile.modeInfo.mainResource.mode       = "default"
  ss.profile.modeInfo.additionalResource.mode = "default"

  local classModeData = ss.ClassResources.getModes(ss.ClassResources, ss.curClass)
  local i             = 1

  for spec,modeList in pairs(classModeData) do
    for mode,resourceList in pairs(modeList) do
      for k,resourceData in pairs(resourceList) do
        if i < 4 then
          ss.ClassResources.registerMode(
            ss.private.layout.frameList[i],
            spec,
            mode,
            resourceData,
            resourceData[2],
            { {r=1,g=1,b=1,a=1}, {r=1,g=1,b=1,a=1}, },
            { id = "galaxy", groupId = "swirl", speed = 5, },
            { ["fillGroup"] = "primary", ["fillId"] = "moon" },
            ss.FillTextures.get("primary","moon"))
        elseif i == 4 then
          ss.ClassResources.registerMode(
            ss.private.layout.frameList[i],
            spec,
            mode,
            resourceData,
            resourceData[2],
            { {r=1,g=1,b=1,a=1}, {r=1,g=1,b=1,a=1}, },
            { id = "galaxy", groupId = "swirl", speed = 5, },
            { ["fillGroup"] = "split", ["fillId"] = 1 },
            ss.FillTextures.get("split", 1))
        end
        i = i + 1
      end
      i = 1
    end
  end

  ss.db.Initialized = true
  ss.Layout:Refresh()
end

mtClassResources.modeResourceUnitId = function(dbConfig)
  if dbConfig == "mainResource" then
    local resource = mtClassResources.classModes[ss.curClass][strlower(ss.curSpec)][ss.curShapeshift or "default"]

    if ss.curShapeshift and ss.curShapeshift == "travel" then
      resource = mtClassResources.classModes[ss.curClass][strlower(ss.curSpec)]["default"]
    end

    return (resource and resource[2]) and resource[2][1] or 0
  elseif dbConfig == "additionalResource" then
    local resource = mtClassResources.classModes[ss.curClass][strlower(ss.curSpec)][ss.curShapeshift or "default"]

    if ss.curShapeshift and ss.curShapeshift == "travel" then
      resource = mtClassResources.classModes[ss.curClass][strlower(ss.curSpec)]["default"]
    end

    return ((resource and resource[3]) and resource[3][1] or 0), ((resource and resource[4]) and resource[4][1] or 0)
  else
    return 0, 0
  end
end

mtClassResources.registerMode = function(sphere, specialization, label, fillColor, swirlColor, fontColor, swirl, fillInfo, fillTexture, monitorValue)
  if ss.db[sphere] and ss.db[sphere].modes then
    ss.db[sphere].modes[specialization] = ss.db[sphere].modes[specialization] or {};

    if ss.db[sphere].modes[specialization][label] then
      ss.db[sphere].modes[specialization][label]["fillColor"][2] = fillColor
      ss.db[sphere].modes[specialization][label]["fillInfo"]     = fillInfo
      ss.db[sphere].modes[specialization][label]["fillTexture"]  = fillTexture
    else
      ss.db[sphere].modes[specialization][label] = {
        ["fillColor"] = {
          fillColor,
        },
        ["swirlColor"] = {
          swirlColor,
        },
        ["fontColor"]   = fontColor,
        ["fillInfo"]    = fillInfo,
        ["fillTexture"] = {
          fillTexture,
        },
        ["monitorValue"] = monitorValue or 0,
        ["swirl"] = swirl,
      };
    end
  end
end

ss["ClassResources"] = setmetatable({}, { __index = mtClassResources });
