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

local IUF = InvenUnitFrames
local _G = _G
local unpack = _G.unpack
local UnitPower = _G.UnitPower
local UnitPowerMax = _G.UnitPowerMax
local UnitBuff = _G.UnitBuff
local texture, dir
local solar = GetSpellInfo(ECLIPSE_BAR_SOLAR_BUFF_ID)
local lunar = GetSpellInfo(ECLIPSE_BAR_LUNAR_BUFF_ID)

local function eclipseVisible(object)
	object.values.eclipse = (GetPrimaryTalentTree() == 1 and (GetShapeshiftFormID() == MOONKIN_FORM or not GetShapeshiftFormID())) and 1 or 0
end

local function eclipsePower(object)
	object.values.classpowermax, object.values.classpower = UnitPowerMax("player", SPELL_POWER_ECLIPSE), UnitPower("player", SPELL_POWER_ECLIPSE)
end

local function eclipseDirection(object)
	dir = GetEclipseDirection()
	if dir == "sun" then
		object.classBar.eclipseBar.tex:SetTexCoord(0, 1, 0, 1)
		object.classBar.eclipseBar.tex:Show()
	elseif dir == "moon" then
		object.classBar.eclipseBar.tex:SetTexCoord(1, 0, 0, 1)
		object.classBar.eclipseBar.tex:Show()
	else
		object.classBar.eclipseBar.tex:Hide()
	end
end

local function updateAura(object)
	if UnitBuff("player", lunar) then
		UIFrameFlashStop(object.classBar.eclipseBar.tex2alpha)
		object.classBar.eclipseBar.tex2alpha:Hide()
		if not object.classBar.eclipseBar.tex1alpha:IsShown() then
			object.classBar.eclipseBar.tex1alpha:Show()
			UIFrameFlash(object.classBar.eclipseBar.tex1alpha, 1, 1, 3600)
		end
	elseif UnitBuff("player", solar) then
		UIFrameFlashStop(object.classBar.eclipseBar.tex1alpha)
		object.classBar.eclipseBar.tex1alpha:Hide()
		if not object.classBar.eclipseBar.tex2alpha:IsShown() then
			object.classBar.eclipseBar.tex2alpha:Show()
			UIFrameFlash(object.classBar.eclipseBar.tex2alpha, 1, 1, 3600)
		end
	else
		UIFrameFlashStop(object.classBar.eclipseBar.tex1alpha)
		object.classBar.eclipseBar.tex1alpha:Hide()
		UIFrameFlashStop(object.classBar.eclipseBar.tex2alpha)
		object.classBar.eclipseBar.tex2alpha:Hide()
	end
end

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 = "DRUID"
	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, 0.75)
	object.classBar:SetBar(0, 1, true)
	object.classBar.eclipseBar = CreateFrame("Frame", nil, object.classBar)
	object.classBar.eclipseBar:SetAllPoints()
	object.classBar.eclipseBar:SetFrameLevel(object.classBar:GetFrameLevel())
	texture = object.classBar.eclipseBar:CreateTexture(nil, "BORDER", 1)
	texture:SetWidth(1)
	texture:SetPoint("TOP", 0, 0)
	texture:SetPoint("BOTTOM", 0, 0)
	texture:SetTexture(0, 0, 0, 0.55)
	object.classBar.eclipseBar.tex1 = object.classBar.eclipseBar:CreateTexture(nil, "BORDER")
	object.classBar.eclipseBar.tex1:SetPoint("TOPLEFT", 0, 0)
	object.classBar.eclipseBar.tex1:SetPoint("BOTTOMRIGHT", texture, "BOTTOMLEFT", 0, 0)
	object.classBar.eclipseBar.tex1:SetVertexColor(0.4, 0.4, 1)
	object.classBar.eclipseBar.tex1alpha = object.classBar.eclipseBar:CreateTexture(nil, "ARTWORK")
	object.classBar.eclipseBar.tex1alpha:SetAllPoints(object.classBar.eclipseBar.tex1)
	object.classBar.eclipseBar.tex1alpha:SetVertexColor(0.4, 0.4, 1)
	object.classBar.eclipseBar.tex1alpha:SetBlendMode("ADD")
	object.classBar.eclipseBar.tex1alpha:Hide()
	object.classBar.eclipseBar.tex2 = object.classBar.eclipseBar:CreateTexture(nil, "BORDER")
	object.classBar.eclipseBar.tex2:SetPoint("TOPLEFT", texture, "TOPRIGHT", 0, 0)
	object.classBar.eclipseBar.tex2:SetPoint("BOTTOMRIGHT", 0, 0)
	object.classBar.eclipseBar.tex2:SetVertexColor(1, 1, 0)
	object.classBar.eclipseBar.tex2alpha = object.classBar.eclipseBar:CreateTexture(nil, "ARTWORK")
	object.classBar.eclipseBar.tex2alpha:SetAllPoints(object.classBar.eclipseBar.tex2)
	object.classBar.eclipseBar.tex2alpha:SetVertexColor(1, 1, 0)
	object.classBar.eclipseBar.tex2alpha:SetBlendMode("ADD")
	object.classBar.eclipseBar.tex2alpha:Hide()
	object.classBar.eclipseBar.tex = object.classBar.eclipseBar:CreateTexture(nil, "OVERLAY")
	object.classBar.eclipseBar.tex:SetTexture("Interface\\ChatFrame\\ChatFrameExpandArrow")
	object.classBar.eclipseBar.tex:SetSize(16, 16)
	object.classBar.text = object.classBar:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
	object.classBar.text:SetPoint("CENTER", 0, 1)
	object.classBar:SetScript("OnEvent", function(self, event, unit, update)
		if event == "ECLIPSE_DIRECTION_CHANGE" then
			eclipseDirection(object)
		elseif event == "UNIT_AURA" then
			if unit == "player" then
				updateAura(self:GetParent())
			end
		else
			eclipseVisible(object)
			eclipseDirection(object)
			updateAura(object)
		end
	end)
	object.classBar:RegisterEvent("PLAYER_TALENT_UPDATE")
	object.classBar:RegisterEvent("PLAYER_ENTERING_WORLD")
	object.classBar:RegisterEvent("UPDATE_SHAPESHIFT_FORM")
	object.classBar:RegisterEvent("ECLIPSE_DIRECTION_CHANGE")
	object.classBar:RegisterEvent("UNIT_AURA")
	eclipseVisible(object)
	eclipsePower(object)
	eclipseDirection(object)
	updateAura(object)
end

function IUF:ClassBarSetup(object)
	if object.classBar then
		texture = LibStub("LibSharedMedia-3.0"):Fetch("statusbar", self.db.classBar.DRUID.texture or "Smooth v2")
		object.classBar:SetTexture(texture)
		object.classBar.eclipseBar.tex1:SetTexture(texture)
		object.classBar.eclipseBar.tex1alpha:SetTexture(texture)
		object.classBar.eclipseBar.tex2:SetTexture(texture)
		object.classBar.eclipseBar.tex2alpha:SetTexture(texture)
		object.classBar:SetColor(unpack(self.colordb.power[0]))
		self:SetFontString(object.classBar.text, self.db.classBar.DRUID.fontFile, self.db.classBar.DRUID.fontSize, self.db.classBar.DRUID.fontAttribute, self.db.classBar.DRUID.fontShadow)
		self.callbacks.DruidMana(object)
	end
end

function IUF:RegisterClassBarHandlerEvents()
	self:RegisterObjectValueHandler("vehicle", "DruidMana")
	self:RegisterObjectValueHandler("powertype", "DruidMana")
	self:RegisterObjectValueHandler("mana", "DruidMana")
	self:RegisterObjectValueHandler("manamax", "DruidMana")
	self:RegisterObjectValueHandler("eclipse", "DruidMana")
	self:RegisterObjectValueHandler("eclipsevalue", "DruidMana")
	self:RegisterObjectValueHandler("classpower", "DruidMana")
	self:RegisterObjectValueHandler("classpowermax", "DruidMana")
end

function IUF:ClassBarOnUpdate(object)
	if not object.values.vehicle then
		if object.values.eclipse == 1 then
			eclipsePower(object)
		elseif object.values.powertype ~= 0 then
			object.values.manamax, object.values.mana = UnitPowerMax("player", 0), UnitPower("player", 0)
		end
	end
end

function IUF.callbacks:DruidMana()
	if self.classBar then
		if not IUF.db.classBar.DRUID.active or self.values.vehicle then
			self.classBar:SetHeight(0.001)
			self.classBar:SetAlpha(0)
		elseif self.values.eclipse == 1 then
			self.classBar:SetValue(0)
			self.classBar.eclipseBar:Show()
			self.classBar.text:SetText(nil)
			self.classBar:SetHeight(IUF.db.classBar.DRUID.height)
			self.classBar:SetAlpha(1)
			if self.values.classpower and self.values.classpowermax then
				self.classBar.eclipseBar.tex:SetPoint("CENTER", (self.classBar:GetWidth() / 2) * (self.values.classpower / self.values.classpowermax), 0)
			end
		elseif self.values.powertype ~= 0 and self.classBar:SetBar(self.values.mana, self.values.manamax, true) then
			IUF:SetStatusBarValue(self.classBar.text, IUF.db.classBar.DRUID.textType, self.values.mana, self.values.manamax)
			self.classBar:SetHeight(IUF.db.classBar.DRUID.height)
			self.classBar:SetAlpha(1)
			self.classBar.eclipseBar:Hide()
		else
			self.classBar:SetHeight(0.001)
			self.classBar:SetAlpha(0)
		end
		self.classBar:Show()
	end
end