package net.mc_pandacraft.java.util.bukkit;

import org.bukkit.ChatColor;
import org.bukkit.util.NumberConversions;

public class TextProgressBar {
	private static String pattern_start = "[";
	private static String pattern_end = "]";
	private static ChatColor color_empty = ChatColor.DARK_GRAY;
	private static ChatColor color_decoration = ChatColor.GOLD;
	private static ChatColor color_default = ChatColor.RESET;
	private static String pattern_empty = ".";
	private static String pattern_full = "|";
	
	public static String progressBar(double[] values, ChatColor[] colors, double total, int nbCar)
	{
		int[] sizes = new int[values.length];
		
		int max_size = nbCar - pattern_start.length() - pattern_end.length();
		
		for (int i=0; i<values.length; i++)
		{
			double sum_values_before = 0;
			for (int j = i ; j>=0; j--)
				sum_values_before += values[j];
			
			int car_position = NumberConversions.round(max_size * sum_values_before / total);
			
			// évite les barre de progressions plus grandes que la taille demandée
			if (car_position > max_size) car_position = max_size;
			
			int sum_sizes_before = 0;
			for (int j = i-1 ; j>=0; j--)
				sum_sizes_before += sizes[j];
			
			sizes[i] = car_position - sum_sizes_before;
		}
		int sum_sizes = 0;
		
		
		String bar = color_decoration+pattern_start;
		for (int i=0; i<sizes.length; i++)
		{
			sum_sizes += sizes[i];
			
			ChatColor color = color_default;
			if (colors != null && i < colors.length && colors[i] != null)
				color = colors[i];
			
			bar = bar + color;
			
			for (int j=0; j<sizes[i]; j++)
				bar = bar + pattern_full;
		}

		bar = bar + color_empty;
		for (int j=0; j<(max_size-sum_sizes); j++)
			bar = bar + pattern_empty;
		
		bar = bar + color_decoration + pattern_end;
		return bar;
	}
	
	
	public static String progressBar(double value, ChatColor color, double max, int nbCar)
	{
		double[] d = new double[1]; 		d[0] = value;
		ChatColor[] c = new ChatColor[1]; 	c[0] = color;
		return progressBar(d, c, max, nbCar);
	}
	
	
}
