local _, ns = ...

--名字模式功能文本
function ns.PlateOnlyName(unitFrame)
	if not unitFrame.unit then return end
	
	if unitFrame:IsPlayer() then
		unitFrame.name:SetText(UnitName(unitFrame.unit))
	end
	if not unitFrame.NpcFuntext then
		unitFrame.NpcFuntext = unitFrame:CreateFontString(nil, "ARTWORK")
		unitFrame.NpcFuntext:SetPoint("TOP",unitFrame.name,"BOTTOM",0,0)
	end
	if ns.MM(UnitGUID(unitFrame.unit)) then
		unitFrame.NpcFuntext:Hide()
		return
	end
	if unitFrame.NpcFuntext then
		unitFrame.NpcFuntext:SetVertexColor(1,1,1)
		unitFrame.NpcFuntext:SetAlpha(0.9)
		unitFrame.NpcFuntext:SetShown(unitFrame.name:IsShown() and not unitFrame.healthBar:IsShown())
		
		local tooltipData = C_TooltipInfo.GetUnit(unitFrame.unit)
		local guildName =  GetGuildInfo(unitFrame.unit)
		if guildName and PlateColorDB.showGuildName and unitFrame:IsPlayer() then
			unitFrame.NpcFuntext:SetFont(ns.fonts, PlateColorDB.helpNameScale * 0.9, "OUTLINE")
			unitFrame.NpcFuntext:SetText(guildName)
		elseif tooltipData and tooltipData.lines[2] and not string.match(tooltipData.lines[2].leftText,LEVEL) and not unitFrame:IsPlayer() then
			unitFrame.NpcFuntext:SetFont(ns.fonts, PlateColorDB.helpNameScale * 0.8, "")
			unitFrame.NpcFuntext:SetText(tooltipData.lines[2].leftText)
		else
			unitFrame.NpcFuntext:SetFont(ns.fonts, PlateColorDB.helpNameScale * 0.8, "")
			unitFrame.NpcFuntext:Hide()
		end
		
	end
end

--设置时更新
function ns.SetOnlyNames(unitFrame)
	ns.PlateOnlyName(unitFrame)
	ns.SetSelectedScale()
	ns.SetPoints(unitFrame)
end

hooksecurefunc("CompactUnitFrame_UpdateName", function(unitFrame)
	if unitFrame:IsForbidden() then return end
	if not string.match(unitFrame.unit,"nameplate") then return end
	ns.PlateOnlyName(unitFrame)
	if not PlateColorDB.onlyNameNpc then return end
	if UnitCanAttack("player",unitFrame.unit) then
		if unitFrame.showOnlyName then
			unitFrame.showOnlyName = nil
		end
		if unitFrame.HealthBarsContainer.healthBar.showOnlyName then
			unitFrame.HealthBarsContainer.healthBar.showOnlyName = nil
			unitFrame.HealthBarsContainer.healthBar:Show()
		end
		if unitFrame.castBar then
			if unitFrame.castBar.showOnlyName then
				unitFrame.castBar.showOnlyName = nil
			end
			if unitFrame.castBar.widgetsOnly then
				unitFrame.castBar.widgetsOnly = nil
			end
		end
	end
end)

hooksecurefunc(NamePlateUnitFrameMixin, "OnUnitSet", function(self)
	if not self.unit then return end
	if self:IsForbidden() then
		SystemFont_NamePlate:SetFont(SystemFont_NamePlate:GetFont(),1,"OUTLINE")
		SystemFont_NamePlate_Outlined:SetFont(SystemFont_NamePlate_Outlined:GetFont(),1,"OUTLINE")
		SystemFont_NamePlate:SetFont(SystemFont_NamePlate:GetFont(),PlateColorDB.helpNameScale,"OUTLINE")
		SystemFont_NamePlate_Outlined:SetFont(SystemFont_NamePlate_Outlined:GetFont(),PlateColorDB.helpNameScale,"OUTLINE")
	end
	if not PlateColorDB.onlyNameNpc then return end
	if not self:IsPlayer() and (self:IsForbidden() or not UnitCanAttack("player",self.unit)) then
		TableUtil.TrySet(self, "showOnlyName")
	end
end)
hooksecurefunc(NamePlateUnitFrameMixin, "UpdateNameClassColor", function(self)
	if not self.unit then return end
	if not PlateColorDB.onlyNameNpc then return end
	if not self:IsPlayer() and (self:IsForbidden() or not UnitCanAttack("player",self.unit)) then
		TableUtil.TrySet(self.optionTable, "colorNameBySelection")
		TableUtil.TrySet(self.castBar, "showOnlyName")
		TableUtil.TrySet(self.castBar, "widgetsOnly")
		TableUtil.TrySet(self.HealthBarsContainer.healthBar, "showOnlyName")
		TableUtil.TrySet(self.ClassificationFrame, "showOnlyName")
	end
end)