--	configuration
local BAR_TEXTURE = "Interface\\AddOns\\oUF_BossBars\\media\\normTex.tga"
local TEXT_FONT = "Interface\\Addons\\SharedMediaAdditionalFonts\\fonts\\pf_tempesta_five_condensed.ttf"

local fontcolor = {1, 1, 1, 1}
local hpBarColor = {45/255, 45/255, 45/255, 1}
local ppBarColor = {0/255, 79/255, 163/255, 1}
local bgBarColor = {0/255, 0/255, 0/255, 1}
local ppBgColor = {0/255, 37/255, 76/255}
local fontSize = 8

local ALTERNATE_POWER_INDEX = ALTERNATE_POWER_INDEX

oUF.TagEvents['BossBars:altpower'] = 'UNIT_POWER'
oUF.Tags['BossBars:altpower'] = function(unit)
	local cur = UnitPower(unit, ALTERNATE_POWER_INDEX)
	local max = UnitPowerMax(unit, ALTERNATE_POWER_INDEX)
	if(max > 0 and not UnitIsDeadOrGhost(unit)) then
		return ("%s%%"):format(math.floor(cur/max*100+.5))
	else
		return ''
	end
end

oUF.TagEvents['BossBars:health'] = 'UNIT_HEALTH'
oUF.Tags['BossBars:health'] = function(unit)
		local m = UnitHealthMax(unit)
		if(m == 0) then
			return 0
		else
			return math.floor(UnitHealth(unit)/m*100+.5)
		end
end

------------------------------------------------------------------------------

-- generate the frames
local function CreateUnitFrame(self, unit)
	local BarWidth = 155
	local BarHeight = 20
	
	-- size the overall unit frame
	self:SetSize(BarWidth, BarHeight)

	------------------------
	-- health bar
	------------------------
	local hpBar = CreateFrame("StatusBar", nil, self)
	hpBar:SetPoint("TOPLEFT", self, "TOPLEFT", 1, -1)
	hpBar:SetSize(BarWidth - 2, BarHeight - 2)
	hpBar:SetStatusBarTexture(BAR_TEXTURE)
	hpBar:GetStatusBarTexture():SetHorizTile(true)
	hpBar:SetStatusBarColor(unpack(hpBarColor))	
	hpBar:SetFrameStrata("MEDIUM")
	
	local hpBG = hpBar:CreateTexture(nil, "BORDER")
	hpBG:SetAllPoints(hpBar)
	hpBG:SetSize(BarWidth - 2, BarHeight - 2)
	hpBG:SetTexture(BAR_TEXTURE)
	hpBG:SetVertexColor(unpack(bgBarColor))	
	
	local hpBorder = hpBar:CreateTexture(nil, "BACKGROUND")
	hpBorder:SetPoint("TOPLEFT", hpBar, "TOPLEFT", -1, 1)
	hpBorder:SetSize(BarWidth, BarHeight)
	hpBorder:SetTexture(0, 0, 0, 1)
	
	-- > see "oUF/elements/health.lua" for health bar coloring options
	hpBar.frequentUpdates = true
	hpBar.colorHealth = false
	self.Health = hpBar
	
	------------------------
	-- power bar
	------------------------
	local ppBar = CreateFrame("StatusBar", nil, self)
	ppBar:SetPoint("BOTTOMLEFT", hpBar, "BOTTOMLEFT", 0, -5)
	ppBar:SetSize(BarWidth - 2, BarHeight - 2)
	ppBar:SetStatusBarTexture(BAR_TEXTURE)	
	ppBar:GetStatusBarTexture():SetHorizTile(true)
	ppBar:SetStatusBarColor(unpack(ppBarColor))
	ppBar:SetFrameStrata("LOW")
	
	local ppBG = ppBar:CreateTexture(nil, "BORDER")
	ppBG:SetAllPoints(ppBar)
	ppBG:SetSize(BarWidth - 2, BarHeight - 2)
	ppBG:SetTexture(BAR_TEXTURE)
	ppBG:SetVertexColor(unpack(ppBgColor))
	
	local ppBorder = ppBar:CreateTexture(nil, "BACKGROUND")
	ppBorder:SetPoint("TOPLEFT", ppBar, "TOPLEFT", -1, 1)
	ppBorder:SetSize(BarWidth, BarHeight)
	ppBorder:SetTexture(0, 0, 0, 1)
	
	-- > see "oUF/elements/power.lua" for power bar coloring options
	ppBar.frequentUpdates = true
	ppBar.colorPower = false
	self.Power = ppBar
	
	------------------------
	-- unit name text
	------------------------

	local Name = hpBar:CreateFontString(nil, "Overlay")
	Name:SetFont(TEXT_FONT, fontSize, "OUTLINE")
	Name:SetShadowOffset(0,-1)
	Name:SetTextColor(unpack(fontcolor))
	Name:SetJustifyH("LEFT")
	Name:SetPoint("TOPLEFT", hpBar, "TOPLEFT", 1, -4)
	
	-- > see "oUF/elements/tags.lua" for provided tags
	self:Tag(Name, "[name]")
	
	------------------------
	-- unit health text
	------------------------
	local hpText = hpBar:CreateFontString(nil, "Overlay")
	hpText:SetFont(TEXT_FONT, fontSize, "OUTLINE")
	hpText:SetShadowOffset(.8,-.8)
	hpText:SetTextColor(unpack(fontcolor))
	hpText:SetJustifyH("RIGHT")
	hpText:SetPoint("RIGHT", hpBar, "RIGHT", 2, 2)
	
	-- > "see oUF/elements/tags.lua" for provided tags
	self:Tag(hpText, "[BossBars:health]%")
	
--[[	-- handle cast bar
	self.Castbar = CreateFrame('StatusBar', nil, self)
	self.Castbar:SetFrameStrata("HIGH")
	self.Castbar:SetPoint("LEFT", self, "LEFT", 1, 0)
	self.Castbar:SetWidth(BarWidth - 2 )
	self.Castbar:SetHeight(BarHeight - 2)
	self.Castbar:SetStatusBarTexture(BAR_TEXTURE)	
	self.Castbar:GetStatusBarTexture():SetHorizTile(true)
	self.Castbar:SetStatusBarColor(29/255, 63/255, 72/255, 1)	
	
	-- cast bar background
	self.Castbar.bg = self.Castbar:CreateTexture(nil, "BACKGROUND")
	self.Castbar.bg:SetAllPoints(self.Castbar)
	self.Castbar.bg:SetTexture(0, 0, 0, .8)

	-- cast bar spell name text
	self.Castbar.Text = self.Castbar:CreateFontString(nil, 'OVERLAY')
	self.Castbar.Text:SetPoint('LEFT', self.Castbar, 2, 0)
	self.Castbar.Text:SetSize((BarWidth-2)* (2/3), BarHeight - 2)
	self.Castbar.Text:SetFont(TEXT_FONT, fontSize)
	self.Castbar.Text:SetJustifyH("LEFT")
	self.Castbar.Text:SetShadowOffset(.8,-.8)
	self.Castbar.Text:SetTextColor(unpack(fontcolor))

	-- cast bar spell time text
	self.Castbar.Time = self.Castbar:CreateFontString(nil, 'OVERLAY')
	self.Castbar.Time:SetPoint('RIGHT', self.Castbar, -3, 0)
	self.Castbar.Time:SetSize((BarWidth-2.1) * (1/3), BarHeight - 2.1)
	self.Castbar.Time:SetFont(TEXT_FONT, fontSize)
	self.Castbar.Time:SetShadowOffset(.8,-.8)
	self.Castbar.Time:SetTextColor(unpack(fontcolor))
	self.Castbar.Time:SetJustifyH("RIGHT")
	self.Castbar.CustomTimeText = function(_, t)
		self.Castbar.Time:SetText(("%.1f/%.1f"):format(self.Castbar.castIsChanneled and t or self.Castbar.max - t, self.Castbar.max))
	end ]]
	
	--[[ alt power bar
	local AltPowerBar = CreateFrame("StatusBar", nil, self)
	AltPowerBar:SetFrameLevel(self.Health:GetFrameLevel() + 1)
	AltPowerBar:SetSize(98, 13)
	AltPowerBar:SetStatusBarTexture(BAR_TEXTURE)
	AltPowerBar:GetStatusBarTexture():SetHorizTile(false)
	AltPowerBar:SetStatusBarColor(1, 0, 0)
	AltPowerBar:SetPoint("BOTTOMRIGHT", self, "TOPRIGHT", 0, 5)
	self.AltPowerBar = AltPowerBar
	
	local bgAltPowerBar = AltPowerBar:CreateTexture(nil, "BORDER")
	bgAltPowerBar:SetAllPoints(AltPowerBar)
	bgAltPowerBar:SetTexture(BAR_TEXTURE)
	bgAltPowerBar:SetVertexColor(unpack(bgBarColor))
	
	local borderAltPowerBar = AltPowerBar:CreateTexture(nil, "BACKGROUND")
	borderAltPowerBar:SetPoint("TOPLEFT", AltPowerBar, "TOPLEFT", -1, 1)
	borderAltPowerBar:SetSize(100.1, 15.1)
	borderAltPowerBar:SetTexture(0, 0, 0, 1)
	
	-- alt power bar text
	local pbText = AltPowerBar:CreateFontString(nil, "Overlay")
	pbText:SetFont(TEXT_FONT, fontSize)
	pbText:SetShadowOffset(.8,-.8)
	pbText:SetTextColor(unpack(fontcolor))
	pbText:SetJustifyH("RIGHT")
	pbText:SetPoint("RIGHT", AltPowerBar, "RIGHT", -4, 0)
	
	self:Tag(pbText, "[BossBars:altpower]")]]

	--[[
	-- create buff at left of unit if they are boss units
	local buffs = CreateFrame("Frame", nil, self)
	buffs:SetHeight(26)
	buffs:SetWidth(252)
	buffs:SetPoint("RIGHT", self, "LEFT", TukuiDB.Scale(-4), 0)
	buffs.size = 26
	buffs.num = 3
	buffs.spacing = 2
	buffs.initialAnchor = 'RIGHT'
	buffs["growth-x"] = "LEFT"
	buffs.PostCreateIcon = TukuiDB.PostCreateAura
	buffs.PostUpdateIcon = TukuiDB.PostUpdateAura
	self.Buffs = buffs
	]]
end

local function CreateTargetFrame(self, unit)
	local BarWidth = 70
	local BarHeight = 10	
	
	self:SetSize(BarWidth, BarHeight)
	
	local hpBar = CreateFrame("StatusBar", nil, self)
	hpBar:SetStatusBarTexture(BAR_TEXTURE)
	hpBar:GetStatusBarTexture():SetHorizTile(true)	
	hpBar:SetStatusBarColor(unpack(hpBarColor))
	hpBar:SetSize(BarWidth - 2, BarHeight - 2)
	hpBar:SetPoint("TOPLEFT", self, "TOPLEFT", 1, -4)
	
	local hpBG = hpBar:CreateTexture(nil, "BORDER")
	hpBG:SetAllPoints(hpBar)
	hpBG:SetSize(BarWidth - 2, BarHeight - 2.1)
	hpBG:SetTexture(BAR_TEXTURE)
	hpBG:SetVertexColor(unpack(bgBarColor))

	local hpBorder = hpBar:CreateTexture(nil, "BACKGROUND")
	hpBorder:SetPoint("TOPLEFT", hpBar, "TOPLEFT", -1, 1)
	hpBorder:SetSize(BarWidth, BarHeight)
	hpBorder:SetTexture(0, 0, 0, 1)
	
	-- > see "oUF/elements/health.lua" for health bar coloring options
	hpBar.frequentUpdates = true
	hpBar.colorHealth = false
	self.Health = hpBar
	
	local Name = hpBar:CreateFontString(nil, "Overlay")
	Name:SetFont(TEXT_FONT, fontSize, "OUTLINE")
	Name:SetShadowOffset(.8,-.8)
	Name:SetTextColor(unpack(fontcolor))
	Name:SetJustifyH("LEFT")
	Name:SetPoint("CENTER", hpBar, "BOTTOM", 0, 4)
	
	-- > see "oUF/elements/tags.lua" for provided tags
	self:Tag(Name, "[name]")	
end

------------------------------------------------------------------------------

oUF:RegisterStyle("oUF_BossBars", CreateUnitFrame)
oUF:RegisterStyle("oUF_BossTargets", CreateTargetFrame)

local index = GetCurrentResolution()
local resolution = select(index, GetScreenResolutions())

if resolution == "1920x1080" then
	oUF:SetActiveStyle("oUF_BossBars")
	local boss1 = oUF:Spawn("boss1", "oUF_Boss1")
	boss1:SetPoint("TOPLEFT", UIParent, "TOP", 753, -360)

	local boss2 = oUF:Spawn("boss2", "oUF_Boss2")
	boss2:SetPoint("BOTTOMLEFT", oUF_Boss1, "LEFT", 0, -60)

	local boss3 = oUF:Spawn("boss3", "oUF_Boss3")
	boss3:SetPoint("BOTTOMLEFT", oUF_Boss2, "LEFT", 0, -60)

	local boss4 = oUF:Spawn("boss4", "oUF_Boss4")
	boss4:SetPoint("BOTTOMLEFT", oUF_Boss3, "LEFT", 0, -60)

	oUF:SetActiveStyle("oUF_BossTargets")
	local boss1T = oUF:Spawn("boss1target", "oUF_Boss1T")
	boss1T:SetPoint("BOTTOMLEFT", oUF_Boss1, "TOPLEFT", 80, -35)

	local boss2T = oUF:Spawn("boss2target", "oUF_Boss2T")
	boss2T:SetPoint("BOTTOMLEFT", oUF_Boss2, "TOPLEFT", 80, -35)

	local boss3T = oUF:Spawn("boss3target", "oUF_Boss3T")
	boss3T:SetPoint("BOTTOMLEFT", oUF_Boss3, "TOPLEFT", 80, -35)

	local boss4T = oUF:Spawn("boss4target", "oUF_Boss4T")
	boss4T:SetPoint("BOTTOMLEFT", oUF_Boss4, "TOPLEFT", 80, -35)
else
	oUF:SetActiveStyle("oUF_BossBars")
	local boss1 = oUF:Spawn("boss1", "oUF_Boss1")
	boss1:SetPoint("TOPLEFT", UIParent, "TOP", 637, -360)

	local boss2 = oUF:Spawn("boss2", "oUF_Boss2")
	boss2:SetPoint("BOTTOMLEFT", oUF_Boss1, "LEFT", 0, -60)

	local boss3 = oUF:Spawn("boss3", "oUF_Boss3")
	boss3:SetPoint("BOTTOMLEFT", oUF_Boss2, "LEFT", 0, -60)

	local boss4 = oUF:Spawn("boss4", "oUF_Boss4")
	boss4:SetPoint("BOTTOMLEFT", oUF_Boss3, "LEFT", 0, -60)

	oUF:SetActiveStyle("oUF_BossTargets")
	local boss1T = oUF:Spawn("boss1target", "oUF_Boss1T")
	boss1T:SetPoint("BOTTOMLEFT", oUF_Boss1, "TOPLEFT", 80, -35)

	local boss2T = oUF:Spawn("boss2target", "oUF_Boss2T")
	boss2T:SetPoint("BOTTOMLEFT", oUF_Boss2, "TOPLEFT", 80, -35)

	local boss3T = oUF:Spawn("boss3target", "oUF_Boss3T")
	boss3T:SetPoint("BOTTOMLEFT", oUF_Boss3, "TOPLEFT", 80, -35)

	local boss4T = oUF:Spawn("boss4target", "oUF_Boss4T")
	boss4T:SetPoint("BOTTOMLEFT", oUF_Boss4, "TOPLEFT", 80, -35)
end