local _, ns = ...
--名字颜色

function ns.SetNameColor(unitFrame)
	if not unitFrame.name then return end
	if not unitFrame.unit then return end
	if UnitIsPlayer(unitFrame.unit) then return end
	local DB = PlateColorDB
	local Threat = UnitThreatSituation("player", unitFrame.unit) or 0
	local IsPlayerPet = UnitIsOtherPlayersPet(unitFrame.unit) and not UnitIsPlayer(unitFrame.unit)
	local IsTank = UnitGroupRolesAssigned("player") == "TANK" and (DB.TankthreatUse == 1 or DB.TankthreatUse == 3)
	local NoTank = UnitGroupRolesAssigned("player") ~= "TANK" and (DB.threatUse == 1 or DB.threatUse == 3)
	local InCombat = not ns.MM(UnitAffectingCombat(unitFrame.unit)) and UnitAffectingCombat(unitFrame.unit)
	local useExtendedColors = unitFrame.optionTable.colorNameWithExtendedColors;
	local reaction = UnitReaction(unitFrame.unit, "player") or 0
	local NotAttack = not UnitCanAttack("player",unitFrame.unit)

	--被别人摸了尸体
	if ( CompactUnitFrame_IsTapDenied(unitFrame) or (UnitIsDead(unitFrame.unit) and not UnitIsPlayer(unitFrame.unit))) then
	-- Use grey if not a player and can't get tap on unit
		unitFrame.name:SetVertexColor(0.5, 0.5, 0.5);
		return
	end
	
	if Threat == 0 and InCombat and IsTank then--坦克无仇恨
		unitFrame.name:SetVertexColor(DB["TANKnoThreatColor"]["r"],DB["TANKnoThreatColor"]["g"],DB["TANKnoThreatColor"]["b"])
			
	elseif Threat == 1 and IsTank then--坦克高仇恨
		unitFrame.name:SetVertexColor(DB["TANKhighThreatColor"]["r"],DB["TANKhighThreatColor"]["g"],DB["TANKhighThreatColor"]["b"])
		
	elseif Threat == 2 and IsTank then--坦克仇恨降低
		unitFrame.name:SetVertexColor(DB["TANKlowThreatColor"]["r"],DB["TANKlowThreatColor"]["g"],DB["TANKlowThreatColor"]["b"])
		
	elseif Threat == 1 and NoTank then--高仇恨
		unitFrame.name:SetVertexColor(DB["highThreatColor"]["r"],DB["highThreatColor"]["g"],DB["highThreatColor"]["b"])
		
	elseif Threat == 2 and NoTank then--仇恨降低
		unitFrame.name:SetVertexColor(DB["lowThreatColor"]["r"],DB["lowThreatColor"]["g"],DB["lowThreatColor"]["b"])
		
	elseif Threat == 3  and NoTank then--仇恨是你
		unitFrame.name:SetVertexColor(DB["myThreatColor"]["r"],DB["myThreatColor"]["g"],DB["myThreatColor"]["b"])
	
	elseif (DB.UseNpc == 1 or DB.UseNpc == 3)  and ns.NpcLevelColor(unitFrame) then--NPC染色
		unitFrame.name:SetVertexColor(ns.NpcLevelColor(unitFrame)["r"],ns.NpcLevelColor(unitFrame)["g"],ns.NpcLevelColor(unitFrame)["b"])

	elseif NotAttack and IsPlayerPet then
		unitFrame.name:SetVertexColor(0, 1, 1);
		
	elseif reaction >= 5 then
		unitFrame.name:SetVertexColor(0, 1, 0);
	
	elseif PlateColorDB.whiteName then
		unitFrame.name:SetVertexColor(1.0, 1.0, 1.0);

	elseif CompactUnitFrame_IsOnThreatListWithPlayer(unitFrame.displayedUnit) then
		unitFrame.name:SetVertexColor(1.0, 0.0, 0.0);
		
	elseif reaction == 4 then
		unitFrame.name:SetVertexColor(1, 1, 0);
		
	else
		unitFrame.name:SetVertexColor(1.0, 0.0, 0.0);
		
	end

end

hooksecurefunc("CompactUnitFrame_UpdateName", function(unitFrame)
	if unitFrame:IsForbidden() then return end
	if not string.match(unitFrame.unit,"nameplate") then return end
	ns.SetNameColor(unitFrame)
end)