package speiger.src.coreengine.utils.helpers;

import java.text.DecimalFormat;
import java.util.Locale;

import speiger.src.coreengine.math.color.ColorUtils;
import speiger.src.coreengine.utils.io.GameLog;
import speiger.src.coreengine.utils.io.GameLog.LogLevel;
import speiger.src.coreengine.utils.misc.CodepointSequence;

public class TextUtil
{
	public static boolean parseBoolean(String data, boolean current, boolean original) {
		if(data.length() == 1) {
			return switch(data.charAt(0)) {
				case 'r' -> original;
				case 't' -> true;
				case 'f' -> false;
				default -> current;
			};
		}
		return Boolean.parseBoolean(data);
	}
	
	public static float parseFloat(String input, float defaultValue) {
		try { return Float.parseFloat(input); }
		catch (Exception e) { return defaultValue; }
	}
	
	public static int findEnd(CodepointSequence text, String prefix, String suffix) {
		 if(!text.startsWith(prefix)) return -1;
		 int count = 1;
		 for(int i = prefix.length(),m=text.length()-suffix.length();i<m;i++) {
			 if(text.startsWith(suffix, i)) {
				 count--;
				 if(count <= 0) return i+suffix.length();
				 i+=suffix.length()-1;
			 }
			 else if(text.startsWith(prefix, i)) {
				 count++;
				 i+=prefix.length()-1;
			 }
		 }
		 return -1;
	}
	
	public static String removeSpecialFormatters(String input) {
		return input.replaceAll("\\§\\<(.*?)\\>", "");
	}
	
	public static String makeColor(Integer color) {
		return "§<color"+(color == null ? "" : "="+color.intValue())+">";
	}
	
	public static int getColorFromText(String s) {
		s = s == null ? "" : s;
		return ColorUtils.rgb((s.hashCode() & 11184810) + 4473924);
	}
	
	public static String searchUntil(String s, int startIndex, char endChar, String invalidChars) {
		boolean found = false;
		invalidChars = invalidChars.toLowerCase(Locale.ROOT);
		StringBuilder builder = new StringBuilder();
		while(startIndex < s.length()) {
			char character = s.charAt(startIndex);
			if(character == endChar) {
				found = true;
				break;
			}
			if(invalidChars.contains(Character.toString(character).toLowerCase(Locale.ROOT))) break;
			builder.append(character);
			startIndex++;
		}
		return found ? builder.toString() : "";
	}
	
	public static boolean findFlag(String value, String search, boolean original) {
		if(value.startsWith(search)) {
			int equals = value.indexOf("=");
			return equals == -1 ? !original : (Boolean.parseBoolean(value.substring(equals + 1)));
		}
		return original;
	}
	
	public static int findColor(String value, String search, int current, int original) {
		if(value.startsWith(search)) {
			try {
				int equals = value.indexOf("=");
				return equals == -1 ? original : ColorUtils.rgb(Integer.decode(value.substring(equals + 1)).intValue());
			}
			catch(Exception e) {
				GameLog.error("Color Parsing failed", e, LogLevel.WARNING);
			}
		}
		return current;
	}
	
	public static float findFloat(String value, String search, float current, float toggleValue) {
		if(value.startsWith(search)) {
			int equals = value.indexOf("=");
			return equals == -1 ? toggleValue : (Float.parseFloat(value.substring(equals + 1)));
		}
		return current;
	}
	
	public static String convertTime(long time, String key, DecimalFormat format) {
		return key + (time >= 1000000 ? format.format(time /= 1000000)+"ms" : (time >= 1000 ? format.format(time /= 1000)+"qs" : format.format(time)+"ns"));
	}
}
