package protocolsupport.protocol.packet.middleimpl.clientbound.play.v_4_5_6_7_8_9r1_9r2_10_11_12r1_12r2;

import org.bukkit.ChatColor;

import protocolsupport.api.chat.components.TextComponent;
import protocolsupport.api.utils.Any;
import protocolsupport.protocol.ConnectionImpl;
import protocolsupport.protocol.packet.middle.clientbound.play.MiddleScoreboardTeam;
import protocolsupport.protocol.packet.middle.clientbound.play.MiddleScoreboardTeam.ScoreboardTeamsTracker.TrackedScoreboardTeam;
import protocolsupport.protocol.storage.netcache.AttributesCache;
import protocolsupport.protocol.typeremapper.legacy.chat.LegacyChat;

public abstract class AbstractScoreboardTeam extends MiddleScoreboardTeam {

	public AbstractScoreboardTeam(ConnectionImpl connection) {
		super(connection);
	}

	protected final AttributesCache clientCache = cache.getAttributesCache();


	protected final Any<String, String> formatPrefixSuffix() {
		return isDisplayLineTeam() ? formatDisplayLinePrefixSuffix() : new Any<>(formatLegacyClampedPrefix(), formatLegacyClampedSuffix());
	}

	protected boolean isDisplayLineTeam() {
		if (
			suffix.isSimple() &&
			(suffix instanceof TextComponent) &&
			((TextComponent) suffix).getText().isEmpty()
		) {
			TrackedScoreboardTeam team = teamsTracker.getTeam(name);
			for (String player : team.getPlayers()) {
				//TODO: improve performance (by checking for non color codes presence)
				if (!ChatColor.stripColor(player).isEmpty()) {
					return false;
				}
			}
			return true;
		}
		return false;
	}

	protected static final int FORMAT_CODE_LENGTH = 2;
	protected static final int NFIX_LIMIT = 16;
	protected static final int NFIX_LIMIT_1F = NFIX_LIMIT - FORMAT_CODE_LENGTH;
	protected static final int NFIX_LIMIT_2F = NFIX_LIMIT_1F - FORMAT_CODE_LENGTH;
	protected static final int NFIX_LIMIT_FCUT = NFIX_LIMIT - 1;

	protected String formatLegacyClampedPrefix() {
		String prefix = this.prefix.toLegacyText(clientCache.getLocale());

		if (format != ChatColor.RESET) {
			prefix = addStartFormat(prefix);
		}

		String prefixLastFormatString = ChatColor.getLastColors(prefix);
		String formatString = format.toString();
		if (
			(prefixLastFormatString.isEmpty() && (format != ChatColor.RESET)) ||
			!formatString.equals(prefixLastFormatString)
		) {
			if (format.isColor()) {
				return LegacyChat.clampLegacyText(prefix, NFIX_LIMIT_1F) + formatString;
			} else {
				return LegacyChat.clampLegacyText(prefix, NFIX_LIMIT_2F) + ChatColor.RESET + formatString;
			}
		}
		return LegacyChat.clampLegacyText(prefix, NFIX_LIMIT);
	}

	protected String formatLegacyClampedSuffix() {
		String suffix = this.suffix.toLegacyText(clientCache.getLocale());
		suffix = addStartFormat(suffix);
		return LegacyChat.clampLegacyText(suffix, NFIX_LIMIT);
	}

	protected Any<String, String> formatDisplayLinePrefixSuffix() {
		String prefix = this.prefix.toLegacyText(clientCache.getLocale());

		if (format != ChatColor.RESET) {
			prefix = addStartFormat(prefix);
		}

		if (prefix.length() <= NFIX_LIMIT) {
			return new Any<>(prefix, "");
		}

		int limit = prefix.charAt(NFIX_LIMIT_FCUT) == ChatColor.COLOR_CHAR ? NFIX_LIMIT_FCUT : NFIX_LIMIT;

		String sPrefix = prefix.substring(0, limit);

		String sSuffix = ChatColor.getLastColors(sPrefix) + prefix.substring(limit);
		if (!isStringFormatOverride(sSuffix, ChatColor.RESET)) {
			sSuffix = ChatColor.RESET + sSuffix;
		}
		sSuffix = LegacyChat.clampLegacyText(sSuffix, NFIX_LIMIT);

		return new Any<>(sPrefix, sSuffix);
	}

	protected String addStartFormat(String text) {
		if (text.isEmpty() || isStringFormatOverride(text, format)) {
			return text;
		}
		return format + text;
	}

	protected static boolean isStringFormatOverride(String string, ChatColor format) {
		if ((string.length() >= 2) && (string.charAt(0) == ChatColor.COLOR_CHAR)) {
			ChatColor formatStringColor = ChatColor.getByChar(string.charAt(1));
			if ((formatStringColor != null) && (formatStringColor.isColor() || (formatStringColor == format))) {
				return true;
			}
		}
		return false;
	}

}

@@ -1986,32 +1986,96 @@
  ChatColor.RESET
+ && !prefix.isEmpty() && !isStringFormatOverride(prefix, format)
 ) %7B%0A%09%09%09prefix = 
@@ -2074,38 +2074,31 @@
 refix = 
-addStartF
+f
 ormat
-(
+ + 
 prefix
-)
 ;%0A%09%09%7D%0A%0A%09
@@ -2741,42 +2741,163 @@
 ));%0A
-%09%09suffix = addStartFormat(suffix);
+%0A%09%09if (!isStringFormatOverride(suffix, format)) %7B%0A%09%09%09suffix = format + suffix;%0A%09%09%09if (format.isFormat()) %7B%0A%09%09%09%09suffix = ChatColor.RESET + suffix;%0A%09%09%09%7D%0A%09%09%7D%0A
 %0A%09%09r
@@ -3120,16 +3120,59 @@
 or.RESET
+ && !isStringFormatOverride(prefix, format)
 ) %7B%0A%09%09%09p
@@ -3183,30 +3183,23 @@
 x = 
-addStartF
+f
 ormat
-(
+ + 
 prefix
-)
 ;%0A%09%09
@@ -3672,16 +3672,16 @@
 IMIT);%0A%0A
+
 %09%09return
@@ -3718,168 +3718,8 @@
 %09%7D%0A%0A
-%09protected String addStartFormat(String text) %7B%0A%09%09if (text.isEmpty() %7C%7C isStringFormatOverride(text, format)) %7B%0A%09%09%09return text;%0A%09%09%7D%0A%09%09return format + text;%0A%09%7D%0A%0A
 %09pro

Fix suffix team color prepend (suffix needs reset prepend in case of format format code)