if select(2, UnitClass("player")) ~= "PALADIN" then return end

local IUF = InvenUnitFrames
local _G = _G
local unpack = _G.unpack
local UnitLevel = _G.UnitLevel
local UnitPower = _G.UnitPower
local texture, width
local num = MAX_HOLY_POWER
local level = PALADINPOWERBAR_SHOW_LEVEL
local power = SPELL_POWER_HOLY_POWER
local powerType = "HOLY_POWER"

function IUF:CreateClassBar(object)
	object.classBar = self:CreateStatusBar(object)
	if not(object:GetFrameLevel() and object:GetFrameLevel() > 3) then
		object:SetFrameLevel(4)
	end
	object.classBar:SetFrameLevel(object:GetFrameLevel() - 1)
	object.classBar.type = "PALADIN"
	object.classBar.fadeTex:SetFrameLevel(object:GetFrameLevel() - 2)
	object.classBar.border = CreateFrame("Frame", nil, object.classBar)
	object.classBar.border:SetFrameLevel(object:GetFrameLevel() - 3)
	object.classBar.border:SetPoint("TOPLEFT", -1, 1)
	object.classBar.border:SetPoint("BOTTOMRIGHT", 1, -1)
	object.classBar.border:SetBackdrop({
		bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Background",
		edgeSize = 1,
	})
	object.classBar.border:SetBackdropColor(0, 0, 0, 0.2)
	object.classBar.border:SetBackdropBorderColor(0, 0, 0, 1)
	object.classBar.lines = {}
	for i = 1, num - 1 do
		object.classBar.lines[i] = object.classBar:CreateTexture(nil, "OVERLAY")
		object.classBar.lines[i]:SetWidth(1)
		object.classBar.lines[i]:SetTexture(0, 0, 0, 1)
		object.classBar.lines[i]:SetPoint("TOP", 0, 0)
		object.classBar.lines[i]:SetPoint("BOTTOM", 0, 0)
	end
	object.classBar:RegisterEvent("PLAYER_ENTERING_WORLD")
	object.classBar:RegisterEvent("UNIT_DISPLAYPOWER")
	object.classBar:RegisterEvent("UNIT_POWER")
	object.classBar:RegisterEvent("PLAYER_LEVEL_UP")
	object.classBar:SetScript("OnEvent", function(self, event, unit, update)
		if event == "PLAYER_ENTERING_WORLD" or event == "PLAYER_LEVEL_UP" then
			IUF:ClassBarSetup(self:GetParent())
		elseif unit == "player" then
			if event == "UNIT_DISPLAYPOWER" or (event == "UNIT_POWER" and update == powerType) then
				self:GetParent().values.classpower = UnitPower(unit, power) or 0
			end
		end
	end)
	object.values.classpower = UnitPower("player", power) or 0
end

function IUF:ClassBarSetup(object)
	if object.classBar then
		if IUF.db.classBar.PALADIN.active and UnitLevel("player") >= SHARDBAR_SHOW_LEVEL then
			object.classBar:SetHeight(IUF.db.classBar.PALADIN.height)
			object.classBar:SetAlpha(1)
			object.classBar:SetTexture(LibStub("LibSharedMedia-3.0"):Fetch("statusbar", self.db.classBar.PALADIN.texture or "Smooth v2"))
			object.classBar:SetColor(1, 1, 0)
			for i = 1, num - 1 do
				object.classBar.lines[i]:SetPoint("LEFT", object.classBar, "LEFT", object.classBar:GetWidth() / num * i, 0)
			end
			self.callbacks.ClassPower(object)
		else
			object.classBar:SetHeight(0.001)
			object.classBar:SetAlpha(0)
		end
		object.classBar:Show()
	end
end

function IUF:RegisterClassBarHandlerEvents()
	self:RegisterObjectValueHandler("classpower", "ClassPower")
end

function IUF.callbacks:ClassPower()
	if self.classBar then
		self.classBar:SetBar(self.values.classpower, num, true)
	end
end