/* Error de minimización. Devolviendo el contenido no minimizado.
(2,5): run-time error CSS1062: Expected semicolon or closing curly-brace, found 'constructor('
 */
class TarjetaReader {
    constructor() {
        this.AccumulatedValue = '';
        this.regex = new RegExp('[!-~]');  // alfanuméricos o signos de puntuación más usuales
        this.TypingTimer = null;
        this.TiempoLimpiar = 100;
        this.MinChars = 4;
        this.UserNameTxt = $('#UserName');
        this.PassTxt = $('#Password');
    }

    init() {
        document.addEventListener('keypress', this.onKeyPress.bind(this));
    }

    async onKeyPress(e) {
        const self = this;

        clearTimeout(this.TypingTimer);
        this.TypingTimer = setTimeout(function () {
            self.clean();
        }, this.TiempoLimpiar);

        if (this.PassTxt.is(":focus") == false) {
            if (e.key == "Enter") {
                await this.check();
            } else {
                if (this.regex.test(event.key)) {
                    this.AccumulatedValue += event.key;
                }

            }
        }
    }

    async check() {
        if (this.AccumulatedValue.length >= this.MinChars) {
            const data = {
                numTarjeta: this.AccumulatedValue
            };

            const result = await $.getJSON('/Account/GetUserByTarjeta', data);

            if (result.success) {
                $("form").trigger("reset");
                this.UserNameTxt.val(result.data);
                $('#Password').focus();
            }
        }
    }

    clean() {
        this.AccumulatedValue = '';
    }
}  
