PLUGIN.name = "Chat Control"
PLUGIN.author = "RefoselDev"
PLUGIN.description = "Удаление ненужных типов чатов"


hook.Add("InitializedChatClasses", "ixChatControl.RemoveChats", function()
	ix.chat.classes.it = nil
	ix.chat.classes.connect = nil
	ix.chat.classes.disconnect = nil
	ix.chat.classes.event = nil
	
	ix.chat.Register("rp", {
		OnChatAdd = function(self, speaker, text)
			local name = IsValid(speaker) and speaker:Name() or "Console"
			chat.AddText(Color(26, 221, 0), "[RP] ", name, ": ", text)
		end,
		prefix = {"/rp", "/RP"},
		description = "Чат РП отыгровок",
		color = Color(150, 200, 255),
		CanHear = function(self, speaker, listener)
			return listener:GetCharacter() != nil
		end,
		deadCanChat = false
	})
	
	-- Чат между фракциями
	ix.chat.Register("adgl", {
		OnChatAdd = function(self, speaker, text)
			local name = IsValid(speaker) and speaker:Name() or "Console"
			local prefix = "[ФРАКЦИЯ] "
			local prefixColor = Color(255, 200, 100)
			
			if (IsValid(speaker) and speaker:GetCharacter()) then
				local faction = speaker:GetCharacter():GetFaction()
				
				if (faction == FACTION_RUSSIAN) then
					prefix = "[ВС РФ → ВСУ] "
					prefixColor = Color(255, 196, 0)
				elseif (faction == FACTION_UKRAINE) then
					prefix = "[ВСУ → ВС РФ] "
					prefixColor = Color(255, 196, 0)
				end
			end
			
			chat.AddText(prefixColor, prefix, Color(255, 255, 255), name, ": ", text)
		end,
		prefix = {"/adgl"},
		description = "Чат между фракциями",
		color = Color(255, 200, 100),
		CanHear = function(self, speaker, listener)
			-- Слышат только игроки с персонажем и определенной фракцией
			if (!listener:GetCharacter()) then
				return false
			end
			
			local listenerFaction = listener:GetCharacter():GetFaction()
			return listenerFaction == FACTION_RUSSIAN or listenerFaction == FACTION_UKRAINE
		end,
		deadCanChat = false
	})
	
	-- Объявления стороны
	ix.chat.Register("ad", {
		OnChatAdd = function(self, speaker, text)
			local name = IsValid(speaker) and speaker:Name() or "Console"
			local prefix = "[ОБЪЯВЛЕНИЕ] "
			local prefixColor = Color(200, 200, 200)
			
			if (IsValid(speaker) and speaker:GetCharacter()) then
				local faction = speaker:GetCharacter():GetFaction()
				
				if (faction == FACTION_RUSSIAN) then
					prefix = "[ВС РФ] "
					prefixColor = Color(255, 100, 100)
				elseif (faction == FACTION_UKRAINE) then
					prefix = "[ВСУ] "
					prefixColor = Color(255, 196, 0)
				end
			end
			
			chat.AddText(prefixColor, prefix, Color(255, 255, 255), name, ": ", text)
		end,
		prefix = {"/ad"},
		description = "Объявления стороны",
		color = Color(200, 200, 200),
		CanHear = function(self, speaker, listener)
			-- Слышат только игроки той же фракции
			if (!listener:GetCharacter() or !speaker:GetCharacter()) then
				return false
			end
			
			local speakerFaction = speaker:GetCharacter():GetFaction()
			local listenerFaction = listener:GetCharacter():GetFaction()
			
			return speakerFaction == listenerFaction
		end,
		deadCanChat = false
	})
	
	-- Чат подразделения
	ix.chat.Register("podr", {
		OnChatAdd = function(self, speaker, text)
			local name = IsValid(speaker) and speaker:Name() or "Console"
			local prefix = "[ПОДРАЗДЕЛЕНИЕ] "
			local prefixColor = Color(100, 150, 255)
			
			if (IsValid(speaker) and speaker:GetCharacter()) then
				local character = speaker:GetCharacter()
				local faction = character:GetFaction()
				local podrID = (character.GetPodr and character:GetPodr()) or 1
				local factionTable = ix.faction.indices[faction]
				
				if (factionTable and factionTable.Podr and factionTable.Podr[podrID]) then
					local podrName = factionTable.Podr[podrID].name or "Неизвестно"
					prefix = "["..podrName.."] "
					
					if (faction == FACTION_RUSSIAN) then
						prefixColor = Color(255, 196, 0)
					elseif (faction == FACTION_UKRAINE) then
						prefixColor = Color(255, 196, 0)
					end
				end
			end
			
			chat.AddText(prefixColor, prefix, Color(255, 255, 255), name, ": ", text)
		end,
		prefix = {"/podr", "/p"},
		description = "Чат подразделения",
		color = Color(100, 150, 255),
		CanHear = function(self, speaker, listener)
			-- Слышат только игроки той же фракции и того же подразделения
			if (!listener:GetCharacter() or !speaker:GetCharacter()) then
				return false
			end
			
			local speakerChar = speaker:GetCharacter()
			local listenerChar = listener:GetCharacter()
			
			local speakerFaction = speakerChar:GetFaction()
			local listenerFaction = listenerChar:GetFaction()
			
			if (speakerFaction != listenerFaction) then
				return false
			end
			
			local speakerPodr = (speakerChar.GetPodr and speakerChar:GetPodr()) or 1
			local listenerPodr = (listenerChar.GetPodr and listenerChar:GetPodr()) or 1
			
			return speakerPodr == listenerPodr
		end,
		deadCanChat = false
	})
	
	if (SERVER) then
		print("[Chat Control] Удалены чаты: it, connect, disconnect, event")
		print("[Chat Control] Добавлены чаты: /rp, /adgl, /ad, /podr")
	end
end)
