package lecho.lib.hellocharts.formatter;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import lecho.lib.hellocharts.util.FloatUtils;

public class ValueFormatterHelper {
	public static final int DEFAULT_DIGITS_NUMBER = 0;

	private int decimalDigitsNumber = Integer.MIN_VALUE;
	private char[] appendedText = new char[0];
	private char[] prependedText = new char[0];
	private char decimalSeparator = '.';

	public void determineDecimalSeparator() {
		NumberFormat numberFormat = NumberFormat.getInstance();
		if (numberFormat instanceof DecimalFormat) {
			decimalSeparator = ((DecimalFormat) numberFormat).getDecimalFormatSymbols().getDecimalSeparator();
		}
	}

	public int getDecimalDigitsNumber() {
		return decimalDigitsNumber;
	}

	/**
	 * Sets number of digits after comma, used only for manual axes, this value will not be used for auto-generated axes.
	 */
	public ValueFormatterHelper setDecimalDigitsNumber(int decimalDigitsNumber) {
		this.decimalDigitsNumber = decimalDigitsNumber;
		return this;
	}

	public char[] getAppendedText() {
		return appendedText;
	}

	public ValueFormatterHelper setAppendedText(char[] appendedText) {
		if (null != appendedText) {
			this.appendedText = appendedText;
		}
		return this;
	}

	public char[] getPrependedText() {
		return prependedText;
	}

	public ValueFormatterHelper setPrependedText(char[] prependedText) {
		if (null != prependedText) {
			this.prependedText = prependedText;
		}
		return this;
	}

	public char getDecimalSeparator() {
		return decimalSeparator;
	}

	public ValueFormatterHelper setDecimalSeparator(char decimalSeparator) {
		char nullChar = '\0';
		if (nullChar != decimalSeparator) {
			this.decimalSeparator = decimalSeparator;
		}
		return this;
	}

	/**
	 * Formats float value. Result is stored in (output) formattedValue array. Method
	 * returns number of chars of formatted value. The formatted value starts at index [formattedValue.length -
	 * charsNumber] and ends at index [formattedValue.length-1].
	 * <p/>
	 * If label is not null it will be used as formattedValue instead of float value.
	 */
	public int formatFloatValueWithPrependedAndAppendedText(char[] formattedValue, float value, int defaultDigitsNumber, char[] label) {
		if (null != label) {
			// If custom label is not null use only name characters as formatted value.
			// Copy label into formatted value array.
			System.arraycopy(label, 0, formattedValue, formattedValue.length - label.length, label.length);
			return label.length;
		}

		final int appliedDigitsNumber = getAppliedDecimalDigitsNumber(defaultDigitsNumber);
		final int charsNumber = formatFloatValue(formattedValue, value, appliedDigitsNumber);
		appendText(formattedValue);
		prependText(formattedValue, charsNumber);
		return charsNumber + getPrependedText().length + getAppendedText().length;
	}

	/**
	 * Formats float value. Result is stored in (output) formattedValue array. Method
	 * returns number of chars of formatted value. The formatted value starts at index [formattedValue.length -
	 * charsNumber] and ends at index [formattedValue.length-1].
	 * <p/>
	 * If label is not null it will be used as formattedValue instead of float value.
	 */
	public int formatFloatValueWithPrependedAndAppendedText(char[] formattedValue, float value, char[] label) {
		return formatFloatValueWithPrependedAndAppendedText(formattedValue, value, DEFAULT_DIGITS_NUMBER, label);
	}

	/**
	 * Formats float value. Result is stored in (output) formattedValue array. Method
	 * returns number of chars of formatted value. The formatted value starts at index [formattedValue.length -
	 * charsNumber] and ends at index [formattedValue.length-1].
	 */
	public int formatFloatValueWithPrependedAndAppendedText(char[] formattedValue, float value, int defaultDigitsNumber) {
		return formatFloatValueWithPrependedAndAppendedText(formattedValue, value, defaultDigitsNumber, null);
	}

	public int formatFloatValue(char[] formattedValue, float value, int decimalDigitsNumber) {
		return FloatUtils.formatFloat(formattedValue, value, formattedValue.length - appendedText.length, decimalDigitsNumber,
				decimalSeparator);
	}

	public void appendText(char[] formattedValue) {
		if (appendedText.length > 0) {
			System.arraycopy(appendedText, 0, formattedValue, formattedValue.length - appendedText.length,
					appendedText.length);
		}
	}

	public void prependText(char[] formattedValue, int charsNumber) {
		if (prependedText.length > 0) {
			System.arraycopy(prependedText, 0, formattedValue, formattedValue.length - charsNumber - appendedText.length
					- prependedText.length, prependedText.length);
		}
	}

	public int getAppliedDecimalDigitsNumber(int defaultDigitsNumber) {
		final int appliedDecimalDigitsNumber;
		if (decimalDigitsNumber < 0) {
			//When decimalDigitsNumber < 0 that means that user didn't set that value and defaultDigitsNumber should be used.
			appliedDecimalDigitsNumber = defaultDigitsNumber;
		} else {
			appliedDecimalDigitsNumber = decimalDigitsNumber;
		}
		return appliedDecimalDigitsNumber;
	}

}

@@ -752,137 +752,8 @@
 %09%7D%0A%0A
-%09/**%0A%09 * Sets number of digits after comma, used only for manual axes, this value will not be used for auto-generated axes.%0A%09 */%0A
 %09pub
@@ -1878,32 +1878,38 @@
 1%5D.%0A%09 * %3Cp/%3E%0A%09 *
+ Note:
  If label is not
@@ -1964,32 +1964,188 @@
 of float value.%0A
+%09 * Note: Parameter defaultDigitsNumber is used only if you didn't change decimalDigintsNumber value using%0A%09 * method %7B@link #setDecimalDigitsNumber(int)%7D.%0A
 %09 */%0A%09public int
@@ -2884,349 +2884,86 @@
 %09 * 
-F
+@see #f
 ormat
-s f
+F
 loat
- v
+V
 alue
-. Result is stored in (output) formattedValue array. Method%0A%09 * returns number of chars of formatted value. The formatted value starts at index %5BformattedValue.length -%0A%09 * charsNumber%5D and ends at index %5BformattedValue.length-1%5D.%0A%09 * %3Cp/%3E%0A%09 * If label is not null it will be used as formattedValue instead of float value.
+WithPrependedAndAppendedText(char%5B%5D, float, int, char%5B%5D)
 %0A%09 *
@@ -3198,257 +3198,86 @@
 %09 * 
-F
+@see #f
 ormat
-s f
+F
 loat
- v
+V
 alue
-. Result is stored in (output) formattedValue array. Method%0A%09 * returns number of chars of formatted value. The formatted value starts at index %5BformattedValue.length -%0A%09 * charsNumber%5D and ends at index %5BformattedValue.length-1%5D.
+WithPrependedAndAppendedText(char%5B%5D, float, int, char%5B%5D)
 %0A%09 *

Update java docs for ValueFormatterHelper