﻿import Numeral from 'numeral';
import moment from 'moment';
import Config from '../Util/constantes.js'
import { Round } from './Util.jsx';

let _Masculino = true;
let _Numero = 0;
let _Moneda = "";
let _Centavos = "";
let _Separador = ".";

function Cambiar_na_a_masculino(Texto) {
    return Texto.replace("na", "");
}

function Cambiar_as_a_masculino(Texto) {
    return Texto.replace("as", "o");
}

function Unidades(numero) {
    switch (numero) {
        case 0:
            {
                return "";
            }

        case 1:
            {
                if (_Masculino)
                    return "un";
                else
                    return "una";
            }

        case 2:
            {
                return "dos";
            }

        case 3:
            {
                return "tres";
            }

        case 4:
            {
                return "cuatro";
            }

        case 5:
            {
                return "cinco";
            }

        case 6:
            {
                return "seis";
            }

        case 7:
            {
                return "siete";
            }

        case 8:
            {
                return "ocho";
            }

        case 9:
            {
                return "nueve";
            }

        default:
            {
                return "";
            }
    }
}

function Decenas(numero) {
    switch (numero) {
        case 0:
            {
                return "";
            }

        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
            {
                return Unidades(numero);
            }

        case 10:
            {
                return "diez";
            }

        case 11:
            {
                return "once";
            }

        case 12:
            {
                return "doce";
            }

        case 13:
            {
                return "trece";
            }

        case 14:
            {
                return "catorce";
            }

        case 15:
            {
                return "quince";
            }

        case 16:
            {
                return "dieciséis";
            }

        case 17:
            {
                return "diecisiete";
            }

        case 18:
            {
                return "dieciocho";
            }

        case 19:
            {
                return "diecinueve";
            }

        case 20:
            {
                return "veinte";
            }

        case 21:
        case 24:
        case 25:
        case 27:
        case 28:
        case 29:
            {
                return "veinti" + Unidades(numero % 10);
            }

        case 22:
            {
                return "veintidós";
            }

        case 23:
            {
                return "veintitrés";
            }

        case 26:
            {
                return "veintiséis";
            }

        case 30:
            {
                return "treinta";
            }

        case 40:
            {
                return "cuarenta";
            }

        case 50:
            {
                return "cincuenta";
            }

        case 60:
            {
                return "sesenta";
            }

        case 70:
            {
                return "setenta";
            }

        case 80:
            {
                return "ochenta";
            }

        case 90:
            {
                return "noventa";
            }

        default:
            {
                return Decenas(numero - numero % 10) + " y " + Unidades(numero % 10);
            }
    }
}

function Centenas(numero)
{
    if (numero == 0)
        return "";
    if (numero >= 1 & numero <= 99)
        return Decenas(numero);
    if (numero == 100)
        return "cien";
    if (numero >= 101 & numero <= 199)
        return "ciento " + Decenas(numero % 100);
    if (numero == 500)
    {
        if (_Masculino)
            return "quinientos ";
        else
            return "quinientas ";
    }
    if (numero == 700)
    {
        if (_Masculino)
            return "setecientos ";
        else
            return "setecientas ";
    }
    if (numero == 900)
    {
        if (_Masculino)
            return "novecientos ";
        else
            return "novecientas ";
    }
    if (numero >= 501 & numero < 599)
        return Centenas(numero - numero % 100) + Decenas(numero % 100);
    if (numero >= 701 & numero < 799)
        return Centenas(numero - numero % 100) + Decenas(numero % 100);
    if (numero >= 901 & numero < 999)
        return Centenas(numero - numero % 100) + Decenas(numero % 100);
    if (_Masculino)
        return Unidades(Math.floor(numero / 100)) + "cientos " + Decenas(numero % 100);
    else
        return Unidades(Math.floor(numero / 100)) + "cientas " + Decenas(numero % 100);
}

function Millares(numero) {
    if (numero > 999) {
        if (numero > 1999)
            return Centenas(Math.floor(numero / 1000)) + " mil " + Centenas(numero % 1000);
        else
            return " mil " + Centenas(numero % 1000);
    }
    else
        return Centenas(numero);
}

function Millones(numero) {
    let tmp = '';
    let A = 0, B = 0;
    A = Math.trunc(numero * 0.000001);
    B = Math.trunc(numero - (A / 0.000001));
    if (A == 1)
        return "un millón " + Millares(B);
    else {
        tmp = Millares(A);
        if (tmp.trim() != "") {
            tmp = Cambiar_as_a_masculino(tmp);
            tmp = Cambiar_na_a_masculino(tmp);
            return tmp + " millones " + Millares(B);
        }
        else
            return Millares(B);
    }
}

function Billones(numero) {
    let tmp = '';
    let A = 0;
    let B = 0;
    A = Math.trunc(numero * 0.000000000001);
    B = numero - (A / 0.000000000001);
    if (A == 1)
        return " un billón " + Millones(B);
    else {
        tmp = Millares(A);
        if (tmp.trim() != "") {
            tmp = Cambiar_as_a_masculino(tmp);
            tmp = Cambiar_na_a_masculino(tmp);
            return tmp + " billones " + Millones(B);
        }
        else
            return Millones(B);
    }
}

function Trillones(numero) {
    let tmp = '';
    let A = 0;
    let B = 0;
    A = Math.trunc(numero * 1.0E-18);
    B = numero - (A / 1.0E-18);
    if (A == 1)
        return "un trillón " + Billones(B);
    else if (A <= 9) {
        tmp = Millares(A);
        if (tmp.trim() != "") {
            tmp = Cambiar_as_a_masculino(tmp);
            tmp = Cambiar_na_a_masculino(tmp);
            return tmp + " trillones " + Billones(B);
        }
        else
            return Billones(B);
    }
    else
        return "# # # # # # # # #";
}

export function Convert(pNumero, Moneda, Centavos) {

    _Moneda = Moneda;
    _Centavos = Centavos;

    let S = '';
    let Num_Ctvs = 0;
    let Num_Largo = 0;
    let Result = '';
    _Numero = Math.abs(pNumero);
    Num_Largo = parseInt(_Numero);

    // "si es menor que mil billones... tomamos en cuenta los decimales,
    // "de lo contrario no podemos tomarlos en cuenta... por aquello de
    // "las cifras significativas... uds. saben...
    if (Num_Largo < 1.0E+15) {
        S = Round(_Numero, 2).toString();
        Num_Ctvs = parseInt(S.substring(S.indexOf('.') + 1, S.length));
    }
    else
        Num_Ctvs = 0;
    // "Se traduce la cifra sin decimales
    S = Trillones(Num_Largo);

    // "Se traducen los decimales
    if ((Num_Ctvs > 0))
        Result = Decenas(Num_Ctvs) + " " + _Centavos;
    else
        Result = "";

    // "Compactamos en un solo texto
    if (S.trim() != "") {
        if (Result.trim() != "")
            Result = S.trim() + " " + _Moneda + " con " + Result;
        else
            Result = Strings.trim(S) + " " + _Moneda;
    }

    return Result;
}
