local Proximo = Proximo
local prox_list
local arena_list

local GetTime = GetTime
local GetSpellInfo = GetSpellInfo
local UnitAura = UnitAura
local UnitGUID = UnitGUID
local COMBAT_EVENTS = Proximo.COMBAT_EVENTS

local spell_list = {
  --druid
  [GetSpellInfo(33786)] = 6, --Cyclone
  [GetSpellInfo(339)] = 10, --Entangling Roots
  [GetSpellInfo(2637)] = 10, --Hibernate
  [GetSpellInfo(29166)] = 20, --Innervate
  --hunter
  [GetSpellInfo(34692)] = 18, --The Beast Within
  [GetSpellInfo(3355)] = 10, --Freezing Trap Effect
  --mage
  [GetSpellInfo(45438)] = 10, --Ice Block
  [GetSpellInfo(118)] = 10, --Polymorph
  [GetSpellInfo(122)] = 8, --Frost Nova
  [GetSpellInfo(12051)] = 8, --Evocation
  --paladin
  [GetSpellInfo(642)] = 12, --Divine Shield
  [GetSpellInfo(1022)] = 10, --Blessing of Protection
  [GetSpellInfo(1044)] = 10, --Blessing of Freedom
  --priest
  [GetSpellInfo(33206)] = 8, --Pain Suppression
  [GetSpellInfo(8122)] = 8, --Psychic Scream
  --rogue
  [GetSpellInfo(6770)] = 10, --Sap
  [GetSpellInfo(2094)] = 10, --Blind
  --shaman
  [GetSpellInfo(2825)] = 40, --Bloodlust
  [GetSpellInfo(32182)] = 40, --Heroism
  --warlock
  [GetSpellInfo(5782)] =10, --Fear
  [GetSpellInfo(6789)] = 3, --Death Coil
  [GetSpellInfo(6358)] =10, --Seduction
  [GetSpellInfo(5484)] = 8, --Howl of Terror
  --warrior
  [GetSpellInfo(5246)] = 8, --Intimidating Shout
  --generic
  [GetSpellInfo(746)] = 8, --First Aid
  [GetSpellInfo(42292)] = 120, --PvP Trinket
  [GetSpellInfo(59752)] = 120, --PvP Trinket
}

function Proximo:RegisterAuras()
  if self.db.profile.useinbg then
    COMBAT_EVENTS["SPELL_AURA_APPLIED"] = "BUFF"
    COMBAT_EVENTS["SPELL_PERIODIC_AURA_APPLIED"] = "BUFF"
    COMBAT_EVENTS["SPELL_AURA_APPLIED_DOSE"] = "BUFF"
    COMBAT_EVENTS["SPELL_PERIODIC_AURA_APPLIED_DOSE"] = "BUFF"
  end
  COMBAT_EVENTS["SPELL_AURA_REMOVED"] = "FADE"
  COMBAT_EVENTS["SPELL_PERIODIC_AURA_REMOVED"] = "FADE"
  COMBAT_EVENTS["SPELL_AURA_REMOVED_DOSE"] = "FADE"
  COMBAT_EVENTS["SPELL_PERIODIC_AURA_REMOVED_DOSE"] = "FADE"
  self:RegisterEvent("UNIT_AURA")
  if not prox_list then prox_list = self:GetList() end
  if not arena_list then arena_list = self:GetArenaList() end
end

function Proximo:UnregisterAuras()
  COMBAT_EVENTS["SPELL_AURA_APPLIED"] = nil
  COMBAT_EVENTS["SPELL_PERIODIC_AURA_APPLIED"] = nil
  COMBAT_EVENTS["SPELL_AURA_APPLIED_DOSE"] = nil
  COMBAT_EVENTS["SPELL_PERIODIC_AURA_APPLIED_DOSE"] = nil
  COMBAT_EVENTS["SPELL_AURA_REMOVED"] = nil
  COMBAT_EVENTS["SPELL_PERIODIC_AURA_REMOVED"] = nil
  COMBAT_EVENTS["SPELL_AURA_REMOVED_DOSE"] = nil
  COMBAT_EVENTS["SPELL_PERIODIC_AURA_REMOVED_DOSE"] = nil
  self:UnregisterEvent("UNIT_AURA")
end

function Proximo:UNIT_AURA(event, unit)
  if not arena_list[unit] then return end
  local guid = UnitGUID(unit)
  if not prox_list[guid] or prox_list[guid].dead then return end
  local enemy = prox_list[guid]
  local index = 1
  local spell, rank, icon, count, dtype, duration, spelltime, mine, steal
  --buffs
  while (true) do
    spell, rank, icon, count, dtype, duration, spelltime, mine, steal = UnitAura(unit, index, "HELPFUL")
    if not spell then break end
    --if not already active buff
    if enemy.button.aura.icon:GetTexture() ~= icon and spelltime then
      self:AuraApplied(enemy.button.aura, enemy.name, enemy.class, spell, icon, spelltime-GetTime())
    end
    index = index+1
  end
  index = 1
  --debuffs
  while (true) do
    spell, rank, icon, count, dtype, duration, spelltime, mine, steal = UnitAura(unit, index, "HARMFUL")
    if not spell then break end
    --if not already active debuff
    if enemy.button.aura.icon:GetTexture() ~= icon and spelltime then
      self:AuraApplied(enemy.button.aura, enemy.name, enemy.class, spell, icon, spelltime-GetTime())
    end
    index = index+1
  end
end

function Proximo:AuraApplied(frame, name, class, spell, icon, spelltime)
  if spell_list[spell] then
    local timer = spelltime or spell_list[spell]
    self:HandleAuraGain(frame, icon, timer)
    if self.db.profile.announceauragain then
      local announcement = name..": ++ "..spell.."++"
      local r, g, b = self:GetClassColor(class)
      self:SendAnnouncement(announcement, r, g, b, icon)
    end
  end
end

function Proximo:AuraRemoved(frame, name, class, spell, icon)
  if spell_list[spell] then
    self:HandleAuraFade(frame, icon)
    if self.db.profile.announceaurafade then
      local announcement = name..": -- "..spell.."--"
      local r, g, b = self:GetClassColor(class)
      self:SendAnnouncement(announcement, r, g, b, icon)
    end
  end
end

function Proximo:HandleAuraGain(frame, icon, spelltime)
  frame.icon:SetTexture(icon)
  frame.icon:SetTexCoord(0,0,0,1,1,0,1,1)
  frame.text:SetText(spelltime)
  frame.time = spelltime
  frame:Show()
end

function Proximo:HandleAuraFade(frame, icon)
  if frame.icon:GetTexture() == icon then
    self:AuraFade(frame)
  end
end

function Proximo:AuraFade(frame)
  frame.icon:SetTexture(nil)
  frame.text:SetText(nil)
  frame.time = nil
  frame:Hide()
end