package it.mice.voila.runtime.util;

public class NumberUtil {
	String[] unitdo = { "", "uno", "due", "tre", "quattro", "cinque", "sei",
			"sette", "otto", "nove", "dieci", "undici", "dodici", "tredici",
			"quattordici", "quindici", "sedici", "diciassette", "diciotto",
			"diciannove" };
	String[] tens = { "", "dieci", "venti", "trenta", "quaranta", "cinquanta",
			"sessanta", "settanta", "ottanta", "novanta" };
	String[] digit = { "", "cento", "mille", "milione", "miliardo" };
	int r;

	public static void main(String[] args) throws Exception {
		NumberUtil n = new NumberUtil();
		if (args.length > 0) {
			int num = Integer.parseInt(args[0]);
			System.out.print(num + "=" + n.numberToLetter(num));
		} else {
			for (int i=12199999; i<12201100; i=i+1) {
				System.out.print(i + "=" + n.numberToLetter(i) + "\n");
			}
		}
	}

	// Count the number of digits in the input number
	int numberCount(int num) {
		int cnt = 0;

		while (num > 0) {
			r = num % 10;
			cnt++;
			num = num / 10;
		}

		return cnt;
	}

	// Function for Conversion of two digit

	String twonum(int numq) {
		int numr, nq;
		String ltr = "";

		nq = numq / 10;
		numr = numq % 10;

		if (numq > 19) {
			String aTens = tens[nq];
			if (numr == 1 || numr == 8) {
				aTens = aTens.substring(0, aTens.length() - 1);
			}
			ltr = ltr + aTens + unitdo[numr];
		} else {
			ltr = ltr + unitdo[numq];
		}

		return ltr;
	}

	// Function for Conversion of three digit

	String threenum(int numq) {
		int numr, nq;
		String ltr = "";

		nq = numq / 100;
		numr = numq % 100;

		if (numr == 0) {
			ltr = ltr + (nq > 1 ? unitdo[nq] : "") + digit[1];
		} else {
			ltr = ltr + (nq > 1 ? unitdo[nq] : "") + digit[1] + twonum(numr);
		}
		return ltr;

	}

	public String numberToLetter(Object numStr) {
		// Defining variables q is quotient, r is remainder
		int num = Integer.parseInt(numStr.toString());
		
		int len, q = 0, r = 0;
		String ltr = " ";
		String Str = "";

		if (num <= 0)
			throw new RuntimeException("Zero or Negative number not for conversion");

		while (num > 0) {

			len = numberCount(num);

			// Take the length of the number and do letter conversion

			switch (len)

			{
			case 8:
			case 7:
				q = num / 1000000;
				r = num % 1000000;
				if (q == 1) {
					Str = Str + "unmilione";
				} else {
					ltr = twonum(q);
					Str = Str + ltr + "milioni";
				}
				num = r;
				break;
			case 6:
				q = num / 1000;
				r = num % 1000;
				ltr = threenum(q);
				Str = Str + ltr + "mila";
				num = r;
				break;

			case 5:
			case 4:

				q = num / 1000;
				r = num % 1000;
				if (q > 1) {
					ltr = twonum(q) + "mila";
				} else {
					ltr = "mille";
				}
				Str = Str + ltr;
				num = r;
				break;

			case 3:

				if (len == 3)
					r = num;
				ltr = threenum(r);
				Str = Str + ltr;
				num = 0;
				break;

			case 2:

				ltr = twonum(num);
				Str = Str + ltr;
				num = 0;
				break;

			case 1:
				Str = Str + unitdo[num];
				num = 0;
				break;
			default:
				throw new RuntimeException("Exceeding Crore....No conversion");
			}
			if (num == 0)
				return Str;
		}
		throw new RuntimeException("Problem converting number " + num);
	}
}
