local _, ns = ...
local LinePointColor = C_CurveUtil.CreateColorCurve()--斩杀变色曲线
LinePointColor:SetType(Enum.LuaCurveType.Step)
--血条变色
function ns.UpdateHpbarColor(unitFrame)
	if unitFrame:IsForbidden() then return end
	if not unitFrame.unit then return end
	if not UnitIsPlayer(unitFrame.unit) then
		local DB = PlateColorDB
		local Threat = UnitThreatSituation("player", unitFrame.unit) or 0
		local IsAttackPet = not ns.MM(UnitPlayerControlled(unitFrame.unit.."target")) and UnitPlayerControlled(unitFrame.unit.."target") and not UnitIsPlayer(unitFrame.unit.."target")
		local IsTank = UnitGroupRolesAssigned("player") == "TANK" and (DB.TankthreatUse == 2 or DB.TankthreatUse == 3)
		local NoTank = UnitGroupRolesAssigned("player") ~= "TANK" and (DB.threatUse == 2 or DB.threatUse == 3)
		local InCombat = not ns.MM(UnitAffectingCombat(unitFrame.unit)) and UnitAffectingCombat(unitFrame.unit)
		local Reaction = not ns.MM(UnitReaction(unitFrame.unit, "player")) and UnitReaction(unitFrame.unit, "player")

		local hr,hg,hb
		if UnitIsTapDenied(unitFrame.unit) then--被别人先摸了没拾取
			unitFrame.healthBar:GetStatusBarTexture():SetVertexColor(0.5, 0.5, 0.5);
			return
		end
			
		if Threat == 0 and InCombat and IsTank then--坦克无仇恨
			hr,hg,hb = DB["TANKnoThreatColor"]["r"],DB["TANKnoThreatColor"]["g"],DB["TANKnoThreatColor"]["b"]
			
		elseif Threat == 1 and IsTank then--坦克高仇恨
			hr,hg,hb = DB["TANKhighThreatColor"]["r"],DB["TANKhighThreatColor"]["g"],DB["TANKhighThreatColor"]["b"]
			
		elseif Threat == 2 and IsTank then--坦克仇恨降低
			hr,hg,hb = DB["TANKlowThreatColor"]["r"],DB["TANKlowThreatColor"]["g"],DB["TANKlowThreatColor"]["b"]
			
		elseif Threat == 1 and NoTank then--高仇恨
			hr,hg,hb = DB["highThreatColor"]["r"],DB["highThreatColor"]["g"],DB["highThreatColor"]["b"]
			
		elseif Threat == 2 and NoTank then--仇恨降低
			hr,hg,hb = DB["lowThreatColor"]["r"],DB["lowThreatColor"]["g"],DB["lowThreatColor"]["b"]
			
		elseif Threat == 3  and NoTank then--仇恨是你
			hr,hg,hb = DB["myThreatColor"]["r"],DB["myThreatColor"]["g"],DB["myThreatColor"]["b"]
			
		elseif unitFrame.isTarget and DB.myTarget then-- 你的目标颜色
			hr,hg,hb = DB["myTargetColor"]["r"],DB["myTargetColor"]["g"],DB["myTargetColor"]["b"]
			
		elseif UnitIsUnit(unitFrame.unit,"focus") and DB.myFocus then--焦点变色
			hr,hg,hb = DB["myFocusColor"]["r"],DB["myFocusColor"]["g"],DB["myFocusColor"]["b"]
			
		elseif (DB.UseNpc == 2 or DB.UseNpc == 3)  and ns.NpcLevelColor(unitFrame) then--NPC染色
			hr,hg,hb = ns.NpcLevelColor(unitFrame)["r"],ns.NpcLevelColor(unitFrame)["g"],ns.NpcLevelColor(unitFrame)["b"]
			
		elseif Reaction and Reaction >= 5 then-- 友方npc 绿色
			hr,hg,hb = 0, 1, 0
			
		elseif (Threat == 3 or IsAttackPet) and IsTank then--坦克仇恨是你
			hr,hg,hb = DB["TANKmyhreatColor"]["r"],DB["TANKmyhreatColor"]["g"],DB["TANKmyhreatColor"]["b"]
			
		elseif Threat == 0 and InCombat and NoTank then--无仇恨
			hr,hg,hb = DB["noThreatColor"]["r"],DB["noThreatColor"]["g"],DB["noThreatColor"]["b"]
			
		elseif Reaction and Reaction == 4 then-- 中立怪 黄色
			hr,hg,hb = 1, 1, 0
			
		else
			hr,hg,hb = DB["allColor"]["r"],DB["allColor"]["g"],DB["allColor"]["b"]
		end
		
		if DB["SlayHp"] and ns.LinePoint > 0 and C_SpellBook.IsSpellKnown(ns.SlaylineSpellID) and hr and hg and hb then	--斩杀变色
			LinePointColor:ClearPoints()
			LinePointColor:AddPoint(0, CreateColor(DB["SlayHpColor"]["r"],DB["SlayHpColor"]["g"],DB["SlayHpColor"]["b"], 0))
			LinePointColor:AddPoint(ns.LinePoint/100, CreateColor(hr,hg,hb, 1))
			local color = UnitHealthPercent(unitFrame.unit, true, LinePointColor);
			if color and color:GetRGB() then
				hr,hg,hb = color:GetRGB()
			end
		end
		
		unitFrame.healthBar:GetStatusBarTexture():SetVertexColor(hr,hg,hb);
	end
end

hooksecurefunc("CompactUnitFrame_UpdateSelectionHighlight", function(frame)
	if not frame.unit then return end
	if frame.unit:lower():match("nameplate") then
		ns.UpdateHpbarColor(frame)
	end
end)
hooksecurefunc("CompactUnitFrame_UpdateHealthColor", function(frame)
--UNIT_NAME_UPDATE
--UNIT_THREAT_LIST_UPDATE
--UNIT_CONNECTION
	if not frame.unit then return end
	if string.match(frame.unit,"nameplate") then
		ns.UpdateHpbarColor(frame)
	end
end)
hooksecurefunc(NamePlateHealthBarMixin,"UpdateTextStringWithValues", function(self)
	if not PlateColorDB["Slayline"]  then return end
	if ns.LinePoint == 0 then return end
	if self:IsForbidden() then return end
	if not self:GetParent():GetParent().unit then return end
	ns.UpdateHpbarColor(self:GetParent():GetParent())
end)

local function NoFrameUpdateHpBar()
	for i, namePlate in ipairs(C_NamePlate.GetNamePlates()) do
		ns.UpdateHpbarColor(namePlate.UnitFrame)
	end
end
ns.event("PLAYER_FOCUS_CHANGED", function()
	if not PlateColorDB.myFocus then return end
	NoFrameUpdateHpBar()
end)

ns.event("PLAYER_DEAD", NoFrameUpdateHpBar)
ns.event("PLAYER_ALIVE", NoFrameUpdateHpBar)

