local _, ns = ...

--施法条
local trueColor,falseColor = CreateColor(0.6,0.6,0.6,1),CreateColor(1,0,0,1)
local function SetPlateCastBar(self,event)
	if event == "PLAYER_ENTERING_WORLD" then return end
	if self:IsForbidden() then return end
	if not self.unit then return end

	if ns.HpTextures[PlateColorDB.castTexture] == "ui-castingbar-filling-standard" then return end--选择了原版材质就用默认的
	
	self:SetStatusBarTexture(ns.HpTextures[PlateColorDB.castTexture])

	self.Background:SetTexture(130937)
	self.Background:SetVertexColor(0.1,0.1,0.1,0.9)
	
	local CastingInfo = select(8, UnitCastingInfo(self.unit))
	local ChanelInfo = select(7, UnitChannelInfo(self.unit))
	local uninterruptable = CastingInfo
	if uninterruptable == nil then
		uninterruptable = ChanelInfo
	end
	if uninterruptable == nil then
		uninterruptable = false
	end
	
	if event=="UNIT_SPELLCAST_FAILED" or event=="UNIT_SPELLCAST_INTERRUPTED" then
		falseColor = CreateColor(1,0,0,1)
	elseif self.casting then
		falseColor = CreateColor(0.9,0.9,0,1)
	elseif self.channeling then
		falseColor = CreateColor(0,1,0,1)
	end
	local barTexture = self:GetStatusBarTexture()
	if barTexture then
		barTexture:SetVertexColorFromBoolean(uninterruptable, trueColor, falseColor)
	end
end
hooksecurefunc(NamePlateCastingBarMixin,"OnEvent",SetPlateCastBar)
ns.event("NAME_PLATE_UNIT_ADDED", function(event, unit)
	local namePlate = C_NamePlate.GetNamePlateForUnit(unit,false)
	local unitFrame = namePlate.UnitFrame
	if unitFrame.castBar then
		SetPlateCastBar(unitFrame.castBar)
	end
end)
hooksecurefunc(NamePlateCastingBarMixin,"FinishSpell",function(self)
	if not PlateColorDB.castold then return end
	if self:IsForbidden() then return end
	if not self.unit then return end
	self:SetStatusBarTexture(ns.HpTextures[PlateColorDB.castTexture])
end)

--施法时间,获取施法剩余时间API抄的Platynator\Display\CastTimeText.lua
hooksecurefunc(NamePlateCastingBarMixin,"OnUpdate", function(self,elapsed)
	if not PlateColorDB.castTime then 
		if self.PCCastTimeText then
			self.PCCastTimeText:SetText("")
		end
		return 
	end
	if not self then return end
	if not self.unit then return end
	if self:IsForbidden() then return end
	if not self.PCCastTimeText then return end
	
	if self.casting and UnitCastingDuration and UnitCastingDuration(self.unit) then
		self.PCCastTimeText:SetText(string.format("%.1f", UnitCastingDuration(self.unit):GetRemainingDuration()))
	elseif self.channeling and UnitChannelDuration and UnitChannelDuration(self.unit) then
		self.PCCastTimeText:SetText(string.format("%.1f", UnitChannelDuration(self.unit):GetRemainingDuration()))
	end
end)