/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.triz.util;

import java.text.DecimalFormat;

/**
 *
 * @author abder_000
 */
public class UtilString {
    
    public static String verificationCaractaire(String mot){
        return mot.replace('\'', '`');
    }
    
    private static String[] uniteEnLetters = new String[]{"", "un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf", "dix", "onze", "douze", "treize", "quatorze", "quinze", "seize", "dix-sept", "dix-huit", "dix-neuf"};

    private static String[] dixEnLetters = new String[]{"", "dix", "vingt", "trente", "quarante", "cinquante", "soixante", "soixante", "quatre-vingt", "quatre-vingt"};

    private static String[] milleEnLetters = new String[]{"", "mille", "million", "milliard"};

    private static String centEnLettre(int numbre) {
        if (numbre == 0) {
            return "";
        }

        String lettre = "";

        if (numbre < 0) {
            lettre = " moin ";
            numbre = numbre * (-1);
        }

        int cent = numbre / 100;
        lettre = " " + uniteEnLetters[cent] + "";
        if (cent > 0) {
            if (cent == 1) {
                lettre = " cent";
            }
            else
                lettre = lettre + " cent";
        }
        numbre = numbre - cent * 100;

        if (numbre < 20) {
            lettre = lettre + " " + uniteEnLetters[numbre];
            return lettre;
        }
        int dix = numbre / 10;
        lettre = lettre + " " + dixEnLetters[dix];
        numbre = numbre - dix * 10;

        if (dix == 7 || dix == 9) {
            lettre = lettre + " " + uniteEnLetters[numbre + 10];
        } else {
            lettre = lettre + " " + uniteEnLetters[numbre];
        }
        return lettre;
    }

    public  static String num2Letters(double number, String devise) {

        if (number == 0) {
            return "zéro " + devise;
        }

        String lettre = "";

        int palier = 1000000000;
        int indice = 3;
        while (palier > 0) {
            Double valD = ((Double) number / palier);
            int val= valD.intValue();
            if (val != 0) {
                if(val>=1000 & indice==3){
                    lettre = lettre + num2Letters(val,  milleEnLetters[indice]);
                }
                else
                    lettre = lettre + centEnLettre(val) + " " + milleEnLetters[indice];
            }

            number = number - ((double) val * palier);
            palier = palier / 1000;
            indice = indice - 1;

        }

        if (!lettre.isEmpty()) {
            lettre = lettre + " " + devise;
        }
        if (number > 0) {
            DecimalFormat franc = new DecimalFormat("#,##0.00 ");
            String numbreMontant = franc.format(number);
            
            String[] virg2 = numbreMontant.split(",");
            //number = number * 100;
            String virgule = "";
            if(virg2.length != 0){
                virgule =virg2[virg2.length-1];
            }
            if (!lettre.isEmpty()) {
                lettre = lettre + " et";
            }
            lettre = lettre + " " + virgule + " cts";
        }

        return lettre;
    }

}
