(function () {

    ready(function () {
        Nonstop.default.initialize({
            environment: window.environment,
            platform: 'web',
            cdnUrl: '../../dist/'
        });

        var isConnectedOutput = document.querySelectorAll('.is-connected')[0];
        var currentUserOutput = document.querySelectorAll('.current-user')[0];

        var loginButton = document.querySelectorAll('.login')[0];
        loginButton.addEventListener('click', function () {
            var userInput = {
                firstName: chance.first(),
                externalId: chance.guid(),
                email: chance.email(),
                birthday: moment(chance.birthday({string: true, american: false}), 'DD/MM/YYYY').format('YYYY-MM-DD')
            };
            Nonstop.default.login(userInput).then(function() {
                isConnectedOutput.textContent = Nonstop.default.isConnected();
                currentUserOutput.textContent = JSON.stringify(Nonstop.default.currentUser());
            });
        });

        var logoutButton = document.querySelectorAll('.logout')[0];
        logoutButton.addEventListener('click', function () {
            Nonstop.default.logout();
            isConnectedOutput.textContent = Nonstop.default.isConnected();
            currentUserOutput.textContent = '{}';
        });
    });

    function ready(fn) {
        if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading") {
            fn();
        } else {
            document.addEventListener('DOMContentLoaded', fn);
        }
    }

})();