package com.hyperfresh.mchyperchat;

import java.util.Collection;

/**
 * Event entry methods for HyperChat.
 *
 * When implementing a server API, redirect all their events to this class.
 */
public class HyperChatEventPoster
{
	private HyperChat hyperChat;

	public HyperChatEventPoster(HyperChat hyperChat)
	{
		this.hyperChat = hyperChat;
	}

	private User lastSpoke = null;

	/**
	 * Executes when a player has said something.
	 *
	 * @param spoke
	 * @param said
	 */
	public void onPlayerChat(Player spoke, String said)
	{
		hyperChat.processInlineFields(said); //replace fields they may have typed in here

		Collection<Player> players = hyperChat.getPlayers();

		spoke.setLastSaid(said);

		if(spoke != lastSpoke)
		{
			if(lastSpoke != null) //print the last player's footer
			{
				players.stream().forEach
				(
					p ->
					{
						String footerFormat = p.getTheme().getChatFooterFormat();
						if(footerFormat != null)
						{
							p.sendMessage(hyperChat.processDynamicFields(footerFormat, lastSpoke));
						}
					}
				);
			}

			//print this player's header
			players.stream().forEach
			(
				p ->
				{
					String headerFormat = spoke.getTheme().getChatHeaderFormat();
					if(headerFormat != null)
					{
						p.sendMessage(hyperChat.processDynamicFields(headerFormat, spoke));
					}
				}
			);
		}

		//print this player's message
		players.stream().forEach
		(
			p ->
			{
				String messageFormat = spoke.getTheme().getChatMessageFormat();
				if(messageFormat != null)
				{
					p.sendMessage(hyperChat.processDynamicFields(messageFormat, spoke));
				}
			}
		);

		lastSpoke = spoke;
	}

	/**
	 * Executes only when a console has run the "say" command.
	 * Everything <b>after</b> "say" should be included in the {@code said} argument.
	 *
	 * @param said what the console said.
	 */
	public void onConsoleChat(String said)
	{
		hyperChat.getConsole().setLastSaid(said);
	}
}
@@ -344,37 +344,248 @@
 %7D%0A%0A%09
-private User lastSpoke = null
+/**%0A%09 * The amount of time in MS between messages from the same player%0A%09 * until their header is printed again.%0A%09 */%0A%09private static final long REPRINT_HEADER_TIMEOUT = 8000;%0A%0A%09private User lastSpoke = null;%0A%09private long lastSpokeTime = 0L
 ;%0A%0A%09
@@ -908,60 +908,349 @@
 %0A%0A%09%09
-if(spoke != lastSpoke)%0A%09%09%7B%0A%09%09%09if(lastSpoke != null) 
+// print the previous player's footer and this player's header if either:%0A%09%09//  - the previous player is NOT this player or%0A%09%09//  - the time between messages from the same player is equal or greater than%0A%09%09//    REPRINT_HEADER_TIMEOUT%0A%09%09if(spoke != lastSpoke %7C%7C (System.currentTimeMillis() - lastSpokeTime) %3E= REPRINT_HEADER_TIMEOUT)%0A%09%09%7B%0A%09%09%09
 //pr
@@ -1278,16 +1278,41 @@
  footer%0A
+%09%09%09if(lastSpoke != null)%0A
 %09%09%09%7B%0A%09%09%09
@@ -2144,16 +2144,62 @@
  spoke;%0A
+%09%09lastSpokeTime = System.currentTimeMillis();%0A
 %09%7D%0A%0A%09/**

add a header timeout system