// main.js

function valideEmail(Email){
    var filtre = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    var valid = filtre.test(Email);

    if (valid) {
        return true;
    }
    return false;
}

Template.main.onCreated( () => {
    let template = Template.instance();
    template.subscribe( 'events' );
    template.subscribe( 'currentUser' );
});

Template.subscriptionOk.onRendered(function(){
    $('#register').css('display','none');
    $('#editMailButton').css('display','none');
});

Template.main.helpers({
    currentEmail() {
        return Meteor.user().emails[0].address;
    }
});

Template.main.events({
    'click #editMail'(event) {
        $('#editMailButton').css('display','block');
    },
    'click #editMailButton'(event) {
        let newMail = $('#editMail').val();
        if(valideEmail(newMail)) {
            Meteor.call('updateMail',newMail,function(error){
                if(typeof(error) == 'undefined') {
                    Bert.alert('Email changed!', 'success');
                    $('#editMailButton').css('display','none');
                }
                else {
                    Bert.alert(error.reason, 'error');
                }
            });
        }
        else {
            Bert.alert('Incorrect Email', 'error');
        }
    },
    'click #Create'(event) {
        $('#register').css('display','block');
        $('#login').css('display','none');
    },
    'click #Auth'(event) {
        $('#register').css('display','none');
        $('#login').css('display','block');
    },
    'click #clickLogOut'(event) {
        Meteor.logout();
        Meteor.setTimeout(function(){
            $('#register').css('display','none');
            $('#login').css('display','block');
        }, 50);
    },
    'submit #login'(event) {
        event.preventDefault();

        var password = $('#Password').val();
        var pseudo = $('#Username').val();

        Meteor.loginWithPassword(pseudo, password, function(error){
            if(typeof(error) != 'undefined'){
                Bert.alert(error.reason,'error');
            }
        });

    },
    'submit #register'(event) {
        event.preventDefault();

        var email = $('#rEmail').val();
        var password = $('#rPassword').val();
        var confirm = $('#rConfirm').val();
        var pseudo = $('#rUsername').val();

        if( pseudo != '' && valideEmail(email) && password == confirm) {
            //accounts.createUser vérifie déjà si l'email ou le username sont pris
            Accounts.createUser({
                email : email,
                password : password,
                username : pseudo
            },function(error) {
                // callback createUser : appelé sans argument si ok, appelé avec error sinon
                if(typeof(error) != 'undefined') {
                    Bert.alert(error.reason,'error');
                }
            });
        }
        else {
            // on explique à l'utilisateur ce qu'il a mal rempli dans le formulaire
            var txt = '<ul>';
            if(pseudo == '')
                txt += 'Pseudo vide!';
            if(!valideEmail(email) || email == '')
                txt += 'Email non valide!';
            if(password != confirm)
                txt += 'mot de passe et confirmation de matchent pas!'
            txt += '</ul>';
            $('#createError').html(txt);
        }
    }
});
