var Clay = require('pebble-clay');
var messageKeys = require('message_keys');
var clayConfig = require('./clay-config.json');

//var clay = new Clay(clayConfig);
var clay = new Clay(clayConfig, null, { autoHandleEvents: false });

Pebble.addEventListener('showConfiguration', function(e) {
  Pebble.openURL(clay.generateUrl());
});

Pebble.addEventListener('webviewclosed', function(e) {
  if (e && !e.response) {
    return;
  }

  // Get the keys and values from each config item
  var dict = clay.getSettings(e.response);

  // Force "select" item value into a number
  dict[messageKeys.DateFormat] = parseInt(dict[messageKeys.DateFormat], 10 /* radix */);

  // Send settings values to watch side
  Pebble.sendAppMessage(dict, function(e) {
    console.log('Sent config data to Pebble');
  }, function(e) {
    console.log('Failed to send config data!');
    console.log(JSON.stringify(e));
  });
});
