/* global $, Handlebars */

let hyperty;


function hypertyLoaded(result) {
  console.log('hypertyLoaded', result);

  let hypertyInfo = '<span class="white-text">' +
                    '<b>Name:</b> ' + result.name + '</br>' +
                    '<b>Status:</b> ' + result.status + '</br>' +
                    '<b>HypertyURL:</b> ' + result.runtimeHypertyURL + '</br>' +
                    '</span>';

  $('.card-panel').html(hypertyInfo);

  hyperty = result.instance;
  let userDirectory = [
      ['openidtest10@gmail.com', 'localhost'],
      ['openidtest20@gmail.com', 'localhost']
  ];

  let contacts = [];
  Handlebars.getTemplate('user-status/user-card').then(function(template) {
    $.each(userDirectory, function(i, v) {
      $('.user-list').append(template({email: v[0]}));
      contacts.push({email: v[0], domain: v[1]});
    });
    $('.btn-change-state').on('click', function() {
      hyperty.setStatus($(this).attr('rel'));
    });
    
    hyperty.create(contacts).then(function(res) {
      console.info(res);
    }).catch(function(reason) {
      console.error(reason);
    });
  });

  hyperty.addEventListener('statusChange', function(event) {
    console.debug('handle statusChange event for', event);
    let email = (typeof event !== 'undefined' && typeof event.identity !== 'undefined') ? event.identity.userProfile.username : 'none';
    $('#user-list').children('[rel="' + email + '"]').removeClass('state-available state-unavailable state-busy state-away').addClass('state-' + event.status);
  });
}

Handlebars.getTemplate = function(name) {

  return new Promise(function(resolve, reject) {

    if (Handlebars.templates === undefined || Handlebars.templates[name] === undefined) {
      Handlebars.templates = {};
    } else {
      resolve(Handlebars.templates[name]);
    }

    $.ajax({
      url: name + '.hbs',
      success: function(data) {
        Handlebars.templates[name] = Handlebars.compile(data);
        resolve(Handlebars.templates[name]);
      },

      fail: function(reason) {
        reject(reason);
      }
    });

  });

};

@@ -25,22 +25,27 @@
 */%0A%0Alet 
-hypert
+userStatusH
 y;%0A%0A%0Afun
@@ -447,22 +447,27 @@
 fo);%0A%0A  
-hypert
+userStatusH
 y = resu
@@ -913,22 +913,27 @@
 %7B%0A      
-hypert
+userStatusH
 y.setSta
@@ -975,22 +975,27 @@
    %0A    
-hypert
+userStatusH
 y.create
@@ -1129,22 +1129,27 @@
  %7D);%0A%0A  
-hypert
+userStatusH
 y.addEve

Update user-status.js