package com.fitbank.common;

import com.fitbank.common.properties.LetterProperties;

//clase utilizada para cambiar de numeros a letras
public class ConvertNumberToLetter {
    private String vLenguaje = "";

    private static final String LETTER_ES = "ES";

    private final String Unidad[] = { "CERO", "UNO", "DOS", "TRES", "CUATRO", "CINCO", "SEIS", "SIETE", "OCHO",
            "NUEVE", "DIEZ", "ONCE", "DOCE", "TRECE", "CATORCE", "QUINCE", "DIECISEIS", "DIECISIETE", "DIECIOCHO",
            "DIECINUEVE", "VEINTE" };

    private final String UnidadEN[] = { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE",
            "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN",
            "NINETEEN", "TWENTY" };

    private final String Decena[] = { "VEINTI", "TREINTA", "CUARENTA", "CINCUENTA", "SESENTA", "SETENTA", "OCHENTA",
            "NOVENTA" };

    private final String DecenaEN[] = { "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY" };

    private final String Centena[] = { "CIEN", "DOSCIENTOS", "TRESCIENTOS", "CUATROCIENTOS", "QUINIENTOS",
            "SEISCIENTOS", "SETECIENTOS", "OCHOCIENTOS", "NOVECIENTOS", "MIL", "UN MILLON", "MILLONES", "UN MIL",
            "MIL" };

    private final String CentenaEN[] = { "ONE HUNDRED", "TWO HUNDRED", "TREE HUNDRED", "FOUR HUNDRED", "FIVE HUNDRED",
            "SIX HUNDRED", "SEVEN HUNDRED", "EIGHT HUNDRED", "NINE HUNDRED", "THOUSAND", "ONE MILLION", "MILLIONS",
            "ONE BILLION", "BILLIONS" };

    /**
     * Obtenemos el valor del Properties Letter.properties que se encuentra en el mismo proyeto
     * 
     * @throws Exception
     */
    public ConvertNumberToLetter() throws Exception {
        // constructoir vacio
        this.vLenguaje = LetterProperties.getInstance().getValue("localNumberToLetter");
    }

    private String getBillon(long Numero) {
        String aux = "";
        long pf = Numero % 1000000000;
        long pi = (Numero / 1000000000);
        if ((Numero > 1000000000) & (Numero < 1999999999)) {
            if (this.vLenguaje.compareTo(LETTER_ES) == 0) {
                aux = this.Centena[12] + " ";
            } else {
                aux = this.CentenaEN[12] + " ";
            }
        } else {
            String value = "";
            if (this.vLenguaje.compareTo(LETTER_ES) == 0) {
                value = this.Centena[13] + " ";
            } else {
                value = this.CentenaEN[13] + " ";
            }
            aux = this.resolverIntervalo(pi) + value + " ";
        }
        if (pf != 0) {
            return (aux + this.resolverIntervalo(pf) + " ");
        }
        return aux;
    }

    private String getCentena(long Numero) {
        String aux = "", aux2 = "";
        long pf = Numero % 100;
        long pi = (Numero / 100);
        int p = 0;
        boolean sal = false;
        while ((p <= 10) & (!sal)) {
            if (pi == p + 1) {
                if (this.vLenguaje.compareTo(LETTER_ES) == 0) {
                    aux = this.Centena[p];
                } else {
                    aux = this.CentenaEN[p];
                }
                sal = true;
            }
            p++;
        }
        if (pf == 0) {
            return aux;
        }
        if (pf < 21) {
            aux2 = this.getUnidad(pf);
        } else {
            aux2 = this.getDecena(pf);
        }
        if (Numero < 200) {
            return (aux + "TO " + aux2 + " ");
        } else {
            return (aux + " " + aux2 + " ");
        }
    }

    private String getDecena(long Numero) {
        String aux = "";
        long pf = Numero % 10;
        long pi = (Numero / 10);
        int p = 0;
        boolean sal = false;
        while ((p <= 8) & (!sal)) {
            if (pi == p + 2) {
                if (this.vLenguaje.compareTo(LETTER_ES) == 0) {
                    aux = this.Decena[p];
                } else {
                    aux = this.DecenaEN[p];
                }
                sal = true;
            }
            p++;
        }
        if (pf == 0) {
            return aux + " ";
        }
        if ((Numero > 20) & (Numero < 30)) {
            return (aux + this.getUnidad(pf) + " ");
        }
        String vMessage = "";
        if (this.vLenguaje.compareTo(LETTER_ES) == 0) {
            vMessage = aux + " Y " + this.getUnidad(pf) + " ";
        } else {
            vMessage = aux + " " + this.getUnidad(pf) + " ";
        }
        return vMessage;
    }

    private String getMil(long Numero) {
        String aux = "";
        long pf = Numero % 1000;
        long pi = (Numero / 1000);
        if (Numero == 1000) {
            String vText = "";
            if (this.vLenguaje.compareTo(LETTER_ES) == 0) {
                vText = "MIL";
            } else {
                vText = "THOUSAND";
            }
            return vText;
        }
        if ((Numero > 1000) & (Numero < 1999)) {
            if (this.vLenguaje.compareTo(LETTER_ES) == 0) {
                aux = this.Centena[9] + " ";
            } else {
                aux = this.CentenaEN[9] + " ";
            }
        } else {
            String value = "";
            if (this.vLenguaje.compareTo(LETTER_ES) == 0) {
                value = this.Centena[9] + " ";
            } else {
                value = this.CentenaEN[9] + " ";
            }
            aux = this.resolverIntervalo(pi) + " " + value + " ";
        }
        if (pf != 0) {
            return (aux + this.resolverIntervalo(pf) + " ");
        }
        return aux;
    }

    private String getMillon(long Numero) {
        String aux = "";
        long pf = Numero % 1000000;
        long pi = (Numero / 1000000);
        if ((Numero > 1000000) & (Numero < 1999999)) {
            if (this.vLenguaje.compareTo(LETTER_ES) == 0) {
                aux = this.Centena[10] + " ";
            } else {
                aux = this.CentenaEN[10] + " ";
            }
        } else {
            String value = "";
            if (this.vLenguaje.compareTo(LETTER_ES) == 0) {
                value = this.Centena[11] + " ";
            } else {
                value = this.CentenaEN[11] + " ";
            }
            aux = this.resolverIntervalo(pi) + value + " ";
        }
        if (pf != 0) {
            return (aux + this.resolverIntervalo(pf) + " ");
        }
        return aux;
    }

    private String getUnidad(long Numero) {
        String aux = "";
        for (int p = 0; p <= 20; p++) {
            if (Numero == p) {
                if (this.vLenguaje.compareTo(LETTER_ES) == 0) {
                    aux = this.Unidad[p] + " ";
                } else {
                    aux = this.UnidadEN[p] + " ";
                }
                /*
                 * if( Numero ==1 & Valororiginal > 1000 ) return("Un ");
                 */
                return aux;
            }
        }
        return " ";
    }

    public boolean pausa(long p) {
        try {
            Thread.sleep(p);
        } catch (Exception e) {

        }
        return true;
    }

    private String resolverIntervalo(long Numero) {
        if ((Numero >= 0) & (Numero <= 20)) {
            return this.getUnidad(Numero);
        }
        if ((Numero >= 21) & (Numero <= 99)) {
            return this.getDecena(Numero);
        }
        if ((Numero >= 100) & (Numero <= 999)) {
            return this.getCentena(Numero);
        }
        if ((Numero >= 1000) & (Numero <= 999999)) {
            return this.getMil(Numero);
        }
        if ((Numero >= 1000000) & (Numero <= 999999999)) {
            return this.getMillon(Numero);
        }
        if ((Numero >= 1000000000) & (Numero <= 2000000000)) {
            return this.getBillon(Numero);
        }
        return "<<El numero esta fuera del rango>>";
    }

    public String toLetras(long Numero) {
        if (Numero >= 0) {
            return (this.resolverIntervalo(Numero));
        } else {
            return (" Menos " + this.resolverIntervalo(Numero * -1));
        }
    }

    public String toLetters(String num) {
        long Numero = Long.valueOf(num).longValue();
        if (Numero >= 0) {
            return (this.resolverIntervalo(Numero));
        } else {
            return (" Menos " + this.resolverIntervalo(Numero * -1));
        }
    }
}
