package cnp.ew.converter;

import java.util.*;
import cnp.ew.util.*;

class CpDecimalToString implements CpToStringConverter
{

    // the number of digits to pad the left side with zeros.
    int integerZeros;

    // the number of digits to pad the right side with zeros.
    int fractionZeros;

    // are we displaying the left side at all?
    boolean showingIntegerPortion;

    // should we show the thousands separator?
    boolean showThousandsSeparator;

    // should we display a decimal separator?
    boolean showingDecimalSeparator;

    // were should we round the fraction portion?
    int significantDigits;

    // Is this in scientific notation? If so, we'll be scaling the number
    // before displaying it.
    public boolean isScientificNotation = false;

    boolean isPercentage=false;



    String thousandsSeparatorString=",";
    String decimalSeparatorString=".";

    static final int BEGIN=0;
    static final int END=1;

    public CpDecimalToString(String formatString)
    {
        int decimalIndex;
        String integerString, fractionString;

        showThousandsSeparator = formatString.indexOf(',') >= 0;

        decimalIndex = formatString.indexOf('.');
        showingDecimalSeparator = decimalIndex >= 0;
        isPercentage = formatString.indexOf('%') >= 0;
        if (showingDecimalSeparator) {
            integerString = formatString.substring(0, decimalIndex);
            fractionString = formatString.substring(decimalIndex + 1, formatString.length());

            showingIntegerPortion = integerString.length() > 0;

            integerZeros = zerosFor(integerString);
            fractionZeros = zerosFor(fractionString);
            significantDigits = fractionString.length();
        } else {
            showingIntegerPortion = true;
            integerZeros = zerosFor(formatString);
        }
    }

    public int getIntegerZeros()
    {
        return integerZeros;
    }

    int zerosFor(String string)
    {
        int zeroCount = CpTextManipulator.occurrencesOf(string, '0');
        if (zeroCount > 0) {
            return zeroCount + CpTextManipulator.occurrencesOf(string, '#');
        } else {
            return 0;
        }
    }

    public String convert(Object o)
    {
        double number = ((Number)o).doubleValue();

        if (isPercentage) {
            number *= 100;
        }


        if (isScientificNotation) {
            while (number >= 10) {
                number /= 10;
            }
            while (number < 1) {
                number *= 10;
            }
        }

        int integerPortion = (int)Math.floor(number);
        int fractionPortion;
        String tempString, convertedString;

        if (showingIntegerPortion) {
            tempString = convertIntegerToString(integerPortion, showThousandsSeparator);
            convertedString = padStringWithZeros(tempString, integerZeros, BEGIN);
        } else {
            convertedString = "";
        }

        if (showingDecimalSeparator) {
            convertedString = convertedString + decimalSeparatorString;
        }

        if (significantDigits > 0) {
            fractionPortion = (int)(Math.round((number - integerPortion) * Math.pow(10, significantDigits)));
            tempString = convertIntegerToString(fractionPortion, false);
            tempString = padStringWithZeros(tempString, fractionZeros, END);
            convertedString = convertedString + tempString;
        }

        if (isPercentage) {
            convertedString += "%";
        }

        return convertedString;
    }

    String convertIntegerToString(int number, boolean shouldShowThousandsSeparator)
    {

        String remainderString = String.valueOf(number);
        String separatedString;
        int length;

        if (!shouldShowThousandsSeparator) {
            return remainderString;
        }

        separatedString = "";
        length = remainderString.length();
        while (length > 3) {
            separatedString = thousandsSeparatorString +
                remainderString.substring(length - 3, length) +
                separatedString;
            remainderString = remainderString.substring(0, length - 3);
            length = remainderString.length();
        }
        separatedString = remainderString + separatedString;

        return separatedString;

    }

    String padStringWithZeros(String string, int zeroCount, int position)
    {
        String zeroString;
        int zerosToPad = zeroCount - string.length();

        if (zerosToPad < 0) {
            return string;
        }

        char charArray[] = new char[zerosToPad];
        for (int i=0; i < zerosToPad; i++) {
            charArray[i] = '0';
        }
        zeroString = new String(charArray);

        if (position == BEGIN) {
            return zeroString + string;
        } else {
            return string + zeroString;
        }
    }

    public void setFormatString(String s)
    {
    }
}

