Quantcast

-- oUF_Simple: core/functions
-- zork, 2016

-----------------------------
-- Variables
-----------------------------

local A, L = ...

local floor, unpack = floor, unpack

--functions container
L.F = {}

-----------------------------
-- Functions
-----------------------------

--NumberFormat
local function NumberFormat(v)
  if v > 1E10 then
    return (floor(v/1E9)).."b"
  elseif v > 1E9 then
    return (floor((v/1E9)*10)/10).."b"
  elseif v > 1E7 then
    return (floor(v/1E6)).."m"
  elseif v > 1E6 then
    return (floor((v/1E6)*10)/10).."m"
  elseif v > 1E4 then
    return (floor(v/1E3)).."k"
  elseif v > 1E3 then
    return (floor((v/1E3)*10)/10).."k"
  else
    return v
  end
end
L.F.NumberFormat = NumberFormat

--CalcFrameSize
local function CalcFrameSize(numButtons,numCols,buttonWidth,buttonHeight,buttonMargin,framePadding)
  local numRows = ceil(numButtons/numCols)
  local frameWidth = numCols*buttonWidth + (numCols-1)*buttonMargin + 2*framePadding
  local frameHeight = numRows*buttonHeight + (numRows-1)*buttonMargin + 2*framePadding
  return frameWidth, frameHeight
end

--SetPoint
local function SetPoint(self,relativeTo,point)
  --adjut the setpoint function to make it possible to reference a relativeTo object that is set on runtime and it not available on config init
  local a,b,c,d,e = unpack(point)
  if not b then
    self:SetPoint(a)
  elseif b and type(b) == "string" and not _G[b] then
    self:SetPoint(a,relativeTo,b,c,d)
  else
    self:SetPoint(a,b,c,d,e)
  end
end

--SetPoints
local function SetPoints(self,relativeTo,points)
  for i, point in next, points do
    SetPoint(self,relativeTo,point)
  end
end

--CreateBackdrop
local function CreateBackdrop(self,relativeTo)
  local backdrop = L.C.backdrop
  local bd = CreateFrame("Frame", nil, self, BackdropTemplateMixin and "BackdropTemplate")
  bd:SetFrameLevel(self:GetFrameLevel()-1 or 0)
  bd:SetPoint("TOPLEFT", relativeTo or self, "TOPLEFT", -backdrop.inset, backdrop.inset)
  bd:SetPoint("BOTTOMRIGHT", relativeTo or self, "BOTTOMRIGHT", backdrop.inset, -backdrop.inset)
  bd:SetBackdrop(backdrop)
  bd:SetBackdropColor(unpack(backdrop.bgColor))
  bd:SetBackdropBorderColor(unpack(backdrop.edgeColor))
  return bd
end

--CreateIcon
local function CreateIcon(self,layer,sublevel,size,point)
  local icon = self:CreateTexture(nil,layer,nil,sublevel)
  icon:SetSize(unpack(size))
  SetPoint(icon,self,point)
  return icon
end

local function ColorHealthbarOnThreat(self,unit)
  if self.colorThreat and unit then
    local status = UnitThreatSituation("player", unit)
    if status and (status == 3 and not self.colorThreatInvers) or (status == 0 and self.colorThreatInvers) then
      self:SetStatusBarColor(unpack(L.C.colors.healthbar.threatInvers))
      self.bg:SetVertexColor(unpack(L.C.colors.healthbar.threatInversBG))
    elseif status and (status == 3 and self.colorThreatInvers) or (status == 0 and not self.colorThreatInvers) then
      self:SetStatusBarColor(unpack(L.C.colors.healthbar.threat))
      self.bg:SetVertexColor(unpack(L.C.colors.healthbar.threatBG))
    end
  end
end

--PostUpdateHealth
local function PostUpdateHealth(self, unit, min, max)
  ColorHealthbarOnThreat(self,unit)
end

--UpdateThreat
local function UpdateThreat(self,event,unit)
  if unit and self.unit ~= unit then
    return
  end
  self.Health:ForceUpdate()
end
L.F.UpdateThreat = UpdateThreat

--UpdateHappiness
local function UpdateHappiness(self,event,unit)
  if(not unit or self.unit ~= unit) then return end
	self.Health:ForceUpdate()
end
L.F.UpdateHappiness = UpdateHappiness

--CreateText
local function CreateText(self,font,size,outline,align,noshadow)
  local text = self:CreateFontString(nil, "ARTWORK") --"BORDER", "OVERLAY"
  text:SetFont(font or STANDARD_TEXT_FONT, size or 14, outline or "OUTLINE")
  text:SetJustifyH(align or "LEFT")
  if not noshadow then
    text:SetShadowColor(0,0,0,0.25)
    text:SetShadowOffset(1,-2)
  end
  --fix some wierd bug
  text:SetText("Bugfix")
  text:SetMaxLines(1)
  text:SetHeight(text:GetStringHeight())
  return text
end
L.F.CreateText = CreateText

--AltPowerBarOverride
local function AltPowerBarOverride(self, event, unit, powerType)
  if self.unit ~= unit or powerType ~= "ALTERNATE" then return end
  local ppmax = UnitPowerMax(unit, ALTERNATE_POWER_INDEX, true) or 0
  local ppcur = UnitPower(unit, ALTERNATE_POWER_INDEX, true)
  local _, r, g, b = GetUnitPowerBarTextureInfo(unit, 2)
  local _, ppmin = UnitPowerBarID(unit)
  local el = self.AlternativePower
  el:SetMinMaxValues(ppmin or 0, ppmax)
  el:SetValue(ppcur)
  if b then
    el:SetStatusBarColor(r, g, b)
    if el.bg then
      local mu = el.bg.multiplier or 0.3
      el.bg:SetVertexColor(r*mu, g*mu, b*mu)
    end
  else
    el:SetStatusBarColor(1, 0, 1)
    if el.bg then
      local mu = el.bg.multiplier or 0.3
      el.bg:SetVertexColor(1*mu, 0*mu, 1*mu)
    end
  end
  if ppmax == 0 then
    el:Hide()
  else
    el:Show()
  end
end

--CreateAltPowerBar
local function CreateAltPowerBar(self)
  if not self.cfg.altpowerbar or not self.cfg.altpowerbar.enabled then return end
  --statusbar
  local s = CreateFrame("StatusBar", nil, self)
  s:SetStatusBarTexture(L.C.textures.statusbar)
  s:SetSize(unpack(self.cfg.altpowerbar.size))
  s:SetOrientation(self.cfg.altpowerbar.orientation or "HORIZONTAL")
  SetPoint(s,self,self.cfg.altpowerbar.point)
  --bg
  local bg = s:CreateTexture(nil, "BACKGROUND")
  bg:SetTexture(L.C.textures.statusbar)
  bg:SetAllPoints()
  s.bg = bg
  --backdrop
  CreateBackdrop(s)
  --attributes
  s.Override = AltPowerBarOverride
  s.bg.multiplier = L.C.colors.bgMultiplier
  return s
end
L.F.CreateAltPowerBar = CreateAltPowerBar

--CreateSwing
local function CreateSwing(self)
  if not self.cfg.swing or not self.cfg.swing.enabled then return end
  --statusbar
  local s = CreateFrame("Frame", nil, self)
  s.texture = L.C.textures.statusbar
  s.textureBG = L.C.textures.statusbarBG
  s.color = self.cfg.swing.color
  s.colorBG = self.cfg.swing.colorBG
  s.disableRanged = self.cfg.swing.disableRanged
  s.disableMelee = self.cfg.swing.disableMelee

  s:SetSize(unpack(self.cfg.swing.size))
  SetPoint(s,self,self.cfg.swing.point)
  s:SetFrameStrata("MEDIUM")

  --bg
  local bg = s:CreateTexture(nil, "BACKGROUND")
  bg:SetAllPoints()
  s.bg = bg

  return s
end
L.F.CreateSwing = CreateSwing

--CreateAbsorbBar
local function CreateAbsorbBar(self)
  --like health the absorb bar cannot be disabled
  --statusbar
  local s = CreateFrame("StatusBar", nil, self.Health)
  s:SetAllPoints()
  s:SetOrientation(self.cfg.healthbar.orientation or "HORIZONTAL")
  s:SetStatusBarTexture(L.C.textures.absorb)
  s:SetStatusBarColor(unpack(L.C.colors.healthbar.absorb))
  s:SetReverseFill(true)
  return s
end
L.F.CreateAbsorbBar = CreateAbsorbBar

--CreateHealCommBar
local function CreateHealCommBar(self, type)
  local s = CreateFrame("StatusBar", nil, self.Health)
  s:SetAllPoints()
  s:SetOrientation(self.cfg.healthbar.orientation or "HORIZONTAL")
  s:SetStatusBarTexture(L.C.textures[type] or L.C.textures.absorb)
  s:SetStatusBarColor(unpack(L.C.colors.healthbar[type] or L.C.colors.healthbar.absorb))
  s:SetReverseFill(true)
  s:SetMinMaxValues(0, 1)
  s:SetValue(0)
  return s
end

--CreateHealComm
local function CreateHealComm(self)
  --like health the absorb bar cannot be disabled
  --statusbar
  return {
    myBar = CreateHealCommBar(self, 'selfcomm'),
    otherBar = CreateHealCommBar(self, 'othercomm'),
    absorbBar = CreateHealCommBar(self, 'absorb'),
    maxOverflow = 1,
  }
end
L.F.CreateHealComm = CreateHealComm

--CreateClassBar
local function CreateClassBar(self)
  if not self.cfg.classbar or not self.cfg.classbar.enabled then return end
  --statusbar
  local s = CreateFrame("StatusBar", nil, self)
  s:SetStatusBarTexture(L.C.textures.statusbar)
  s:SetSize(unpack(self.cfg.classbar.size))
  s:SetOrientation(self.cfg.classbar.orientation or "HORIZONTAL")
  SetPoint(s,self,self.cfg.classbar.point)
  --bg
  local bg = s:CreateTexture(nil, "BACKGROUND")
  bg:SetTexture(L.C.textures.statusbar)
  bg:SetAllPoints()
  s.bg = bg
  --backdrop
  CreateBackdrop(s)
  --attributes
  s.bg.multiplier = L.C.colors.bgMultiplier
  return s
end
L.F.CreateClassBar = CreateClassBar

--CreateHealthBar
local function CreateHealthBar(self)
  --disabling the healthbar makes no sense, no check for enabled
  --statusbar
  local s = CreateFrame("StatusBar", nil, self)
  s:SetStatusBarTexture(L.C.textures.statusbar)
  s:SetAllPoints()
  s:SetOrientation(self.cfg.healthbar.orientation or "HORIZONTAL")
  if L.C.colors.healthbar and L.C.colors.healthbar.default then
    s:SetStatusBarColor(unpack(L.C.colors.healthbar.default))
  end
  --bg
  local bg = s:CreateTexture(nil, "BACKGROUND")
  bg:SetTexture(L.C.textures.statusbarBG)
  bg:SetAllPoints()
  if L.C.colors.healthbar and L.C.colors.healthbar.defaultBG then
    bg:SetVertexColor(unpack(L.C.colors.healthbar.defaultBG))
  end
  s.bg = bg
  --backdrop
  s.bdf = CreateBackdrop(s)
  if self.cfg.healthbar.debuffHighlight then
    self.DebuffHighlight = s.bdf
    self.DebuffHighlightBackdropBorder = true
    self.DebuffHighlightAlpha = 1
    self.DebuffHighlightFilter = self.cfg.healthbar.debuffHighlightFilter or false
  end
  --attributes
  s.colorTapping = self.cfg.healthbar.colorTapping
  s.colorDisconnected = self.cfg.healthbar.colorDisconnected
  s.colorReaction = self.cfg.healthbar.colorReaction
  s.colorClass = self.cfg.healthbar.colorClass
  s.colorHealth = self.cfg.healthbar.colorHealth
  s.colorThreat = self.cfg.healthbar.colorThreat
  s.colorThreatInvers = self.cfg.healthbar.colorThreatInvers
  if select(2, UnitClass("player")) == "HUNTER" and self.settings.template == "pet" then
    s.colorHappiness = true
  end
  s.bg.multiplier = L.C.colors.bgMultiplier
  --hooks
  s.PostUpdateColor = PostUpdateHealth
  if s.colorThreat then
    self:RegisterEvent("PLAYER_REGEN_ENABLED", L.F.UpdateThreat, true)
    self:RegisterEvent("PLAYER_REGEN_DISABLED", L.F.UpdateThreat, true)
    self:RegisterEvent("UNIT_THREAT_SITUATION_UPDATE", L.F.UpdateThreat, false)
    self:RegisterEvent("UNIT_THREAT_LIST_UPDATE", L.F.UpdateThreat, false)
  end
  if s.colorHappiness then
    self:RegisterEvent("UNIT_HAPPINESS", L.F.UpdateHappiness, false)
  end
  return s
end
L.F.CreateHealthBar = CreateHealthBar

--CreateAdditionalPowerBar
local function CreateAdditionalPowerBar(self)
  if not self.cfg.addpowerbar or not self.cfg.addpowerbar.enabled then return end
  --statusbar
  local s = CreateFrame("StatusBar", nil, self)
  s:SetStatusBarTexture(L.C.textures.statusbar)
  s:SetSize(unpack(self.cfg.addpowerbar.size))
  s:SetOrientation(self.cfg.addpowerbar.orientation or "HORIZONTAL")
  SetPoint(s,self,self.cfg.addpowerbar.point)
  --bg
  local bg = s:CreateTexture(nil, "BACKGROUND")
  bg:SetTexture(L.C.textures.statusbarBG)
  bg:SetAllPoints()
  s.bg = bg
  --backdrop
  CreateBackdrop(s)
  --attributes
  s.colorPower = self.cfg.addpowerbar.colorPower
  s.bg.multiplier = L.C.colors.bgMultiplier
  return s
end
L.F.CreateAdditionalPowerBar = CreateAdditionalPowerBar

--CreatePowerBar
local function CreatePowerBar(self)
  if not self.cfg.powerbar or not self.cfg.powerbar.enabled then return end
  --statusbar
  local s = CreateFrame("StatusBar", nil, self)
  s:SetStatusBarTexture(L.C.textures.statusbar)
  s:SetSize(unpack(self.cfg.powerbar.size))
  s:SetOrientation(self.cfg.powerbar.orientation or "HORIZONTAL")
  SetPoint(s,self,self.cfg.powerbar.point)
  --bg
  local bg = s:CreateTexture(nil, "BACKGROUND")
  bg:SetTexture(L.C.textures.statusbarBG)
  bg:SetAllPoints()
  s.bg = bg
  --backdrop
  CreateBackdrop(s)
  --attributes
  s.colorPower = self.cfg.powerbar.colorPower
  s.bg.multiplier = L.C.colors.bgMultiplier
  return s
end
L.F.CreatePowerBar = CreatePowerBar

local function SetCastBarColorShielded(self)
  self.__owner:SetStatusBarColor(unpack(L.C.colors.castbar.shielded))
  self.__owner.bg:SetVertexColor(unpack(L.C.colors.castbar.shieldedBG))
end

local function SetCastBarColorDefault(self)
  self.__owner:SetStatusBarColor(unpack(L.C.colors.castbar.default))
  self.__owner.bg:SetVertexColor(unpack(L.C.colors.castbar.defaultBG))
end

--CreateCastBar
local function CreateCastBar(self)
  if not self.cfg.castbar or not self.cfg.castbar.enabled then return end
  --statusbar
  local s = CreateFrame("StatusBar", nil, self)
  s:SetStatusBarTexture(L.C.textures.statusbar)
  s:SetFrameStrata("MEDIUM")
  s:SetSize(unpack(self.cfg.castbar.size))
  s:SetOrientation(self.cfg.castbar.orientation or "HORIZONTAL")
  SetPoint(s,self,self.cfg.castbar.point)
  s:SetStatusBarColor(unpack(L.C.colors.castbar.default))
  --bg
  local bg = s:CreateTexture(nil, "BACKGROUND")
  bg:SetTexture(L.C.textures.statusbar)
  bg:SetAllPoints()
  bg:SetVertexColor(unpack(L.C.colors.castbar.defaultBG)) --bg multiplier
  s.bg = bg
  --backdrop
  CreateBackdrop(s)
  --icon for player and target only
  if self.cfg.castbar.icon and self.cfg.castbar.icon.enabled then
    --icon
    local i = s:CreateTexture(nil,"BACKGROUND",nil,-8)
    i:SetSize(unpack(self.cfg.castbar.icon.size))
    SetPoint(i,s,self.cfg.castbar.icon.point)
    if self.cfg.castbar.icon.texCoord then
      i:SetTexCoord(unpack(self.cfg.castbar.icon.texCoord))
    else
      i:SetTexCoord(0.1, 0.9, 0.1, 0.9)
    end
    s.Icon = i
    --backdrop (for the icon)
    CreateBackdrop(s,i)
  end
  --shield
  local shield = s:CreateTexture(nil,"BACKGROUND",nil,-8)
  shield.__owner = s
  s.Shield = shield
  --use a trick here...we use the show/hide on the shield texture to recolor the castbar
  hooksecurefunc(shield,"Show",SetCastBarColorShielded)
  hooksecurefunc(shield,"Hide",SetCastBarColorDefault)
  --text
  if self.cfg.castbar.name and self.cfg.castbar.name.enabled then
    local cfg = self.cfg.castbar.name
    local name = CreateText(s,cfg.font,cfg.size,cfg.outline,cfg.align,cfg.noshadow)
    if cfg.points then
      SetPoints(name,s,cfg.points)
    else
      SetPoint(name,s,cfg.point)
    end
    s.Text = name
  end
  return s
end
L.F.CreateCastBar = CreateCastBar

local function CreateRaidMark(self)
  if not self.cfg.raidmark or not self.cfg.raidmark.enabled then return end
  return CreateIcon(self.Health,"OVERLAY",-8,self.cfg.raidmark.size,self.cfg.raidmark.point)
end
L.F.CreateRaidMark = CreateRaidMark

local function CreateReadyCheck(self)
  if not self.cfg.readycheck or not self.cfg.readycheck.enabled then return end
  return CreateIcon(self.Health,"OVERLAY",2,self.cfg.readycheck.size,self.cfg.readycheck.point)
end
L.F.CreateReadyCheck = CreateReadyCheck

local function CreateRange(self)
  if not self.cfg.range or not self.cfg.range.enabled then return end
  return {
		insideAlpha = 1,
		outsideAlpha = self.cfg.range.alpha,
	}
end
L.F.CreateRange = CreateRange

local function CreateRaidDebuffs(self)
  if not self.cfg.debuffwatch or not self.cfg.debuffwatch.enabled then return end
  local cfg = self.cfg.debuffwatch
  local frame = CreateFrame("Frame", nil, self)
  SetPoint(frame,self.Health,cfg.point)
  frame:SetSize(cfg.size,cfg.size)
  frame:SetFrameLevel(self.Health:GetFrameLevel() + 10)
  frame.Backdrop = CreateBackdrop(frame) -- create backdrop
  frame.icon = frame:CreateTexture(nil, "ARTWORK")
  frame.icon:SetTexCoord(0.1, 0.9, 0.1, 0.9)
  frame.icon:SetAllPoints()
  frame.count = frame:CreateFontString(nil, "OVERLAY")
  frame.count:SetFont(L.C.fonts.expressway, cfg.size/2, "OUTLINE")
  frame.count:SetShadowColor(0,0,0,0.6)
  frame.count:SetShadowOffset(1,-1)
  frame.count:ClearAllPoints()
  frame.count:SetPoint("BOTTOMRIGHT", cfg.size/10, -cfg.size/10)
  frame.onlyMatchSpellID = true
  frame.showDispellableDebuff = true
  frame.time = frame:CreateFontString(nil, "OVERLAY")
  frame.time:SetFont(L.C.fonts.expressway, cfg.size/1.65, "OUTLINE")
  frame.time:SetPoint("CENTER",frame,1,0)
  return frame
end
L.F.CreateRaidDebuffs = CreateRaidDebuffs

local function CreateRaidRoleIndicator(self)
  if not self.cfg.raidrole or not self.cfg.raidrole.enabled then return end
  return CreateIcon(self.Health,"OVERLAY",2,self.cfg.raidrole.size,self.cfg.raidrole.point)
end
L.F.CreateRaidRoleIndicator = CreateRaidRoleIndicator

local function CreateGroupRoleIndicator(self)
  if not self.cfg.grouprole or not self.cfg.grouprole.enabled then return end
  return CreateIcon(self.Health,"OVERLAY",2,self.cfg.grouprole.size,self.cfg.grouprole.point)
end
L.F.CreateGroupRoleIndicator = CreateGroupRoleIndicator

--CreateNameText
local function CreateNameText(self)
  if not self.cfg.healthbar or not self.cfg.healthbar.name or not self.cfg.healthbar.name.enabled then return end
  local cfg = self.cfg.healthbar.name
  local text = CreateText(self.rAbsorbBar or self.Health,cfg.font,cfg.size,cfg.outline,cfg.align,cfg.noshadow)
  if cfg.points then
    SetPoints(text,self.rAbsorbBar or self.Health,cfg.points)
  else
    SetPoint(text,self.rAbsorbBar or self.Health,cfg.point)
  end
  self:Tag(text, cfg.tag)
end
L.F.CreateNameText = CreateNameText

--CreateHealthText
local function CreateHealthText(self)
  if not self.cfg.healthbar or not self.cfg.healthbar.health or not self.cfg.healthbar.health.enabled then return end
  local cfg = self.cfg.healthbar.health
  local text = CreateText(self.rAbsorbBar or self.Health,cfg.font,cfg.size,cfg.outline,cfg.align,cfg.noshadow)
  if cfg.points then
    SetPoints(text,self.rAbsorbBar or self.Health,cfg.points)
  else
    SetPoint(text,self.rAbsorbBar or self.Health,cfg.point)
  end
  self:Tag(text, cfg.tag)
end
L.F.CreateHealthText = CreateHealthText

--CreatePowerText
local function CreatePowerText(self)
  if not self.cfg.powerbar or not self.cfg.powerbar.power or not self.cfg.powerbar.power.enabled then return end
  local cfg = self.cfg.powerbar.power
  local text = CreateText(self.Power,cfg.font,cfg.size,cfg.outline,cfg.align,cfg.noshadow)
  if cfg.points then
    SetPoints(text,self.Power,cfg.points)
  else
    SetPoint(text,self.Power,cfg.point)
  end
  self:Tag(text, cfg.tag)
end
L.F.CreatePowerText = CreatePowerText

--PostCreateAura
local function PostCreateAura(self,button)
  local bg = button:CreateTexture(nil,"BACKGROUND",nil,-8)
  bg:SetTexture(L.C.textures.aura)
  bg:SetVertexColor(0,0,0)
  bg:SetPoint("TOPLEFT", -self.size/4, self.size/4)
  bg:SetPoint("BOTTOMRIGHT", self.size/4, -self.size/4)
  button.icon:SetTexCoord(0.1, 0.9, 0.1, 0.9)
  button.count:SetFont(L.C.fonts.expressway, self.size/1.65, "OUTLINE")
  button.count:SetShadowColor(0,0,0,0.6)
  button.count:SetShadowOffset(1,-1)
  button.count:ClearAllPoints()
  button.count:SetPoint("BOTTOMRIGHT", self.size/10, -self.size/10)
  button:SetFrameStrata("LOW")
end

--DesaturateBuffs
local function DesaturateBuffs(unit,button)
  if button.icon then
		button.icon:SetDesaturated(not UnitIsFriend("player",unit) and not button.isPlayer)
	end
end

--PostUpdateDebuff
local function PostUpdateDebuff(self,unit,button)
  if not button then return end

  if self.desaturateAura then
    DesaturateBuffs(unit,button)
  end
end

--CreateBuffs
local function CreateBuffs(self)
  if not self.cfg.buffs or not self.cfg.buffs.enabled then return end
  local cfg = self.cfg.buffs
  local frame = CreateFrame("Frame", nil, self)
  SetPoint(frame,self,cfg.point)
  frame.num = cfg.num
  frame.size = cfg.size
  frame.spacing = cfg.spacing
  frame.initialAnchor = cfg.initialAnchor
  frame["growth-x"] = cfg.growthX
  frame["growth-y"] = cfg.growthY
  frame.disableCooldown = cfg.disableCooldown
  frame.filter = cfg.filter
  frame.showStealableBuffs = cfg.stealable or false
  frame.CustomFilter = cfg.CustomFilter
  frame.PostCreateIcon = cfg.PostCreateAura or PostCreateAura
  --frame.PostUpdateIcon = PostUpdateBuff
  frame:SetSize(CalcFrameSize(cfg.num,cfg.cols,cfg.size,cfg.size,cfg.spacing,0))
  --local t = frame:CreateTexture(nil,"BACKGROUND",nil,-8)
  --t:SetAllPoints()
  --t:SetColorTexture(0,1,0,0.2)
  return frame
end
L.F.CreateBuffs = CreateBuffs

--CreateDebuffs
local function CreateDebuffs(self)
  if not self.cfg.debuffs or not self.cfg.debuffs.enabled then return end
  local cfg = self.cfg.debuffs
  local frame = CreateFrame("Frame", nil, self)
  SetPoint(frame,self,cfg.point)
  frame.desaturateAura = cfg.desaturate or false
  frame.num = cfg.num
  frame.size = cfg.size
  frame.spacing = cfg.spacing
  frame.initialAnchor = cfg.initialAnchor
  frame["growth-x"] = cfg.growthX
  frame["growth-y"] = cfg.growthY
  frame.disableCooldown = cfg.disableCooldown
  frame.filter = cfg.filter
  frame.CustomFilter = cfg.CustomFilter
  frame.PostCreateIcon = cfg.PostCreateAura or PostCreateAura
  frame.PostUpdateIcon = PostUpdateDebuff
  frame:SetSize(CalcFrameSize(cfg.num,cfg.cols,cfg.size,cfg.size,cfg.spacing,0))
  --local t = frame:CreateTexture(nil,"BACKGROUND",nil,-8)
  --t:SetAllPoints()
  --t:SetColorTexture(1,0,0,0.2)
  return frame
end
L.F.CreateDebuffs = CreateDebuffs

--SetupHeader
local function SetupHeader(self)
  if not self.settings.setupHeader then return end
  self:RegisterForClicks("AnyDown")
  self:SetScript("OnEnter", UnitFrame_OnEnter)
  self:SetScript("OnLeave", UnitFrame_OnLeave)
end
L.F.SetupHeader = SetupHeader

--SetupFrame
local function SetupFrame(self)
  if not self.settings.setupFrame then return end
  self:SetSize(unpack(self.cfg.size))
  SetPoint(self,nil,self.cfg.point)
  self:SetScale(self.cfg.scale)
  --print(self:GetName(),self:GetScale(),self:GetEffectiveScale(),self:GetWidth(),self:GetHeight(),self:GetPoint())
end
L.F.SetupFrame = SetupFrame

--CreateDragFrame
local function CreateDragFrame(self)
  if not self.settings.createDrag then return end
  rLib:CreateDragFrame(self, L.dragFrames, -2, true)
end
L.F.CreateDragFrame = CreateDragFrame

--ToggleSmooth
local function ToggleSmooth(self, enabled)
  self.Health.smoothing = enabled
  if self.Power then self.Power.smoothing = enabled end
  if self.HealthPrediction then self.HealthPrediction.smoothing = enabled end
end
L.F.ToggleSmooth = ToggleSmooth

--CreateTrinket
local function CreateTrinket(self)
  if not self.cfg.trinket or not self.cfg.trinket.enabled then return end

  local s = CreateFrame("Frame", nil, self)
  s:SetSize(self.cfg.trinket.size, self.cfg.trinket.size)
  SetPoint(s,self,self.cfg.trinket.point)
  --backdrop
  CreateBackdrop(s)
  --icon
  local i = s:CreateTexture(nil, "ARTWORK")
  i:SetAllPoints()
  i:SetTexture(select(2, UnitFactionGroup("player")) == "Horde" and "Interface\\Icons\\inv_jewelry_trinketpvp_01" or "Interface\\Icons\\inv_jewelry_trinketpvp_02")
  i:SetTexCoord(0.1, 0.9, 0.1, 0.9)
  s.Icon = i

  return s
end
L.F.CreateTrinket = CreateTrinket

--CreateEneryTicker
local function CreateEneryTicker(self)
	if not self.cfg.emr then return end

	local s = CreateFrame("StatusBar", nil, self.Power)
	s:SetFrameLevel(self.Power:GetFrameLevel() + 3)
	s:SetAllPoints()
	s.Spark = s:CreateTexture(nil, "OVERLAY")

	return s
end
L.F.CreateEneryTicker = CreateEneryTicker

local function RunesPostUpdate(self, runemap)
	local bar = self
	local runemap = runemap

	for i, runeId in next, runemap do
		local isReady = select(3, GetRuneCooldown(runeId))

		if isReady then
			bar[i]:SetAlpha(1)
		else
			bar[i]:SetAlpha(0.5)
		end
	end
end

--CreateRunesBar
local function CreateRunesBar(self)
  if not self.cfg.runebar or not self.cfg.runebar.enabled then return end
  if(select(2, UnitClass('player')) ~= 'DEATHKNIGHT') then return end

  local s = CreateFrame("Frame", self:GetName().."RuneBar", self)
  local w, h = unpack(self.cfg.runebar.size)
  -- Runes
	-- s:SetFrameLevel(self.Health:GetFrameLevel() + 1)
  s:SetSize(w, h)
  SetPoint(s,self,self.cfg.runebar.point)

	-- s:SetHeight(6)
	-- s:SetPoint("TOPLEFT", self.Health, 0, 10)
	-- s:SetPoint("TOPRIGHT", self.Health, 0, 10)

  --backdrop
  CreateBackdrop(s)

  for i = 1, 6 do
		s[i] = CreateFrame("StatusBar", self:GetName().."Rune"..i, s)
		s[i]:SetHeight(s:GetHeight())
		s[i]:SetStatusBarTexture(L.C.textures.statusbar)
		s[i]:SetStatusBarColor(self.Power:GetStatusBarColor())

		s[i].bg = s[i]:CreateTexture(nil, "BACKGROUND")
		s[i].bg:SetAllPoints(s[i])
		s[i].bg:SetTexture(L.C.textures.statusbar)
		s[i].bg:SetAlpha(0.4)
    --attributes
    s[i].bg.multiplier = L.C.colors.bgMultiplier

		if i == 1 then
			s[i]:SetWidth(w/6)
			s[i]:SetPoint("LEFT", s, "LEFT", 0, 0)
		else
			s[i]:SetWidth(w/6 - 2)
			s[i]:SetPoint("LEFT", s[i-1], "RIGHT", 2, 0)
		end
	end

  s.PostUpdate = RunesPostUpdate
	s.colorSpec = true
  return s
end
L.F.CreateRunesBar = CreateRunesBar

local function CreateResurrectIndicator(self)
  if not self.cfg.resurrect or not self.cfg.resurrect.enabled then return end
  if self.settings.template ~= "raid" then return end
  local s = self.Health:CreateTexture(nil, "OVERLAY")
  s:SetSize(unpack(self.cfg.resurrect.size))
  SetPoint(s, self.Health, self.cfg.resurrect.point)
  return s
end
L.F.CreateResurrectIndicator = CreateResurrectIndicator