package io.domisum.lib.auxiliumlib.util;

import io.domisum.lib.auxiliumlib.PHR;
import io.domisum.lib.auxiliumlib.annotations.API;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

@API
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class StringListUtil
{
	
	// HORIZONTALLY
	@API
	public static String listHorizontally(List<?> list, String delimiter)
	{
		var combined = new StringBuilder();
		for(int i = 0; i < list.size(); i++)
		{
			var element = list.get(i);
			boolean isLastElement = (i+1) == list.size();
			
			combined.append(element);
			combined.append(isLastElement ? "" : delimiter);
		}
		
		return combined.toString();
	}
	
	@API
	public static String listHorizontally(List<?> list)
	{
		return listHorizontally(list, ", ");
	}
	
	
	@API
	public static String listHorizontally(Collection<?> collection, String delimiter)
	{
		return listHorizontally(new ArrayList<>(collection), delimiter);
	}
	
	@API
	public static String listHorizontally(Collection<?> collection)
	{
		return listHorizontally(collection, ", ");
	}
	
	
	@API
	public static String listHorizontally(Map<?, ?> map, String delimiter)
	{
		var mappings = new ArrayList<String>();
		for(var entry : map.entrySet())
		{
			String mapping = PHR.r("'{}'->'{}'", entry.getKey(), entry.getValue());
			mappings.add(mapping);
		}
		mappings.sort(Comparator.naturalOrder());
		
		return listHorizontally(mappings, delimiter);
	}
	
	@API
	public static String listHorizontally(Map<?, ?> map)
	{
		return listHorizontally(map, ", ");
	}
	
	
	// VERTICALLY
	@API
	public static String listVertically(List<?> list)
	{
		var itemDisplays = new ArrayList<String>();
		for(var item : list)
			itemDisplays.add(PHR.r(" - {}", item.toString()));
		
		return listHorizontally(itemDisplays, "\n");
	}
	
	@API
	public static String listVertically(Collection<?> collection)
	{
		return listVertically(new ArrayList<>(collection));
	}
	
	@API
	public static String listVertically(Map<?, ?> map)
	{
		var entryStrings = new ArrayList<String>();
		for(var entry : map.entrySet())
		{
			String entryString = "'"+entry.getKey()+"' -> '"+entry.getValue()+"'";
			entryStrings.add(entryString);
		}
		
		Collections.sort(entryStrings);
		return listVertically(entryStrings);
	}
	
}

@@ -346,16 +346,52 @@
 til.Map;
+%0Aimport java.util.function.Function;
 %0A%0A@API%0A@
@@ -1270,32 +1270,36 @@
 I%0A%09public static
+ %3CT%3E
  String listHori
@@ -1310,25 +1310,25 @@
 ally(Map%3C?, 
-?
+T
 %3E map, Strin
@@ -1330,32 +1330,67 @@
 String delimiter
+, Function%3CT, Object%3E valueFunction
 )%0A%09%7B%0A%09%09var mappi
@@ -1514,16 +1514,36 @@
 tKey(), 
+valueFunction.apply(
 entry.ge
@@ -1551,16 +1551,17 @@
 Value())
+)
 ;%0A%09%09%09map
@@ -1682,32 +1682,329 @@
 er);%0A%09%7D%0A%09%0A%09@API%0A
+%09public static %3CT%3E String listHorizontally(Map%3C?, T%3E map, Function%3CT, Object%3E valueFunction)%0A%09%7B%0A%09%09return listHorizontally(map, %22, %22, valueFunction);%0A%09%7D%0A%09%0A%09%0A%09@API%0A%09public static String listHorizontally(Map%3C?, ?%3E map, String delimiter)%0A%09%7B%0A%09%09return listHorizontally(map, delimiter, v-%3Ev);%0A%09%7D%0A%09%0A%09@API%0A
 %09public static S

Add list horizontally with map value function