var when = require('when');
var _ = require('lodash');
var skynet = require('skynet');

var subdevices = require('./subdevices');
var remoteConfig = require('./remoteConfig');

var registerRetries = 0;
var retriesMax = 10;
var skynetConfig;

function connect(gatewayId, token){
  var defer = when.defer();

if(process.env.GW_UUID){
  
    skynetConfig = {
    uuid: process.env.GW_UUID,
    token: process.env.GW_TOKEN,
    server: process.env.SKYNET_SERVER,
    port: process.env.SKYNET_PORT
  };
  
    }else {
  skynetConfig = {
    uuid: gatewayId,
    token: token,
    server: process.env.SKYNET_SERVER,
    port: process.env.SKYNET_PORT
  };

    }
    
    
  console.log('connecting to skynet with', skynetConfig);

  var conn = skynet.createConnection(skynetConfig);
  conn.uuid = gatewayId;
  conn.token = token;

  var handlersRegistered = false;



  conn.on('notReady', function(data){
    console.log('UUID FAILED AUTHENTICATION!', data);
    // Register device
    conn.register({
      uuid: gatewayId,
      token: token,
      type: 'gateway'
    }, function (data) {
      console.log('registered', data);
      conn.emit('ready', data);
      defer.resolve(conn);
    });
  });

  conn.on('ready', function(data){
    defer.resolve(conn);
    console.log('UUID AUTHENTICATED!', data);

    if(!handlersRegistered){
      conn.on('message', function(data, fn){
        console.log('\nmessage received from:', data.fromUuid, data);
        if(data.devices){

          var subdevice;
          if(data.devices === gatewayId){
            subdevice = data.subdevice;
          }else if(data.devices !== '*' && data.devices !== 'all'){
            //was messaged as hub_uuid/subdevice_id
            subdevice = data.devices;
          }


          try{
            //console.log(data);
            if(typeof data == "string"){
              data = JSON.parse(data);
            }

            if(subdevice){

              var instance = subdevices.instances[subdevice];
              if(!instance){
                _.forEach(subdevice.instances, function(sub){
                  if(sub.uuid === subdevice){
                    instance = sub;
                  }
                });
              }

              if(instance && instance.onMessage){
                console.log('matching subdevice found:', subdevice);
                instance.onMessage(data, fn);
              }else{
                console.log('no matching subdevice:', subdevice);
              }
            }else{
              if(fn){
                console.log('responding');
                fn('hello back at you');
              }
            }

          }catch(exp){
            console.log('err dispatching message', exp);
          }

        }

      });

      // handle gateway configuration requests
      conn.on('config', function(data, cb){
        console.log('config api call received:', data, cb);
        remoteConfig(token, data, cb);
      });

      handlersRegistered = true;

    }else{
      console.log('hanlders already registered, just reconnected to skynet');
    }



    // Event triggered when device loses connection to skynet
    conn.on('disconnect', function(data){
      console.log('disconnected from skynet');
    });



    //WhoAmI?
    conn.whoami({uuid:gatewayId}, function (data) {
      console.log('whoami', data);
    });



  });

  return defer.promise;


}

module.exports = connect;

@@ -220,25 +220,8 @@
 10;%0A
-var skynetConfig;
 %0A%0Afu
@@ -287,38 +287,17 @@
 );%0A%0A
-if(process.env.GW_UUID)%7B
 %0A  %0A  
+var
   sk
@@ -340,16 +340,29 @@
 .GW_UUID
+ %7C%7C gatewayId
 ,%0A    to
@@ -390,153 +390,11 @@
 OKEN
-,%0A    server: process.env.SKYNET_SERVER,%0A    port: process.env.SKYNET_PORT%0A  %7D;%0A  %0A    %7Delse %7B%0A  skynetConfig = %7B%0A    uuid: gatewayId,%0A    token:
+ %7C%7C
  tok
@@ -475,22 +475,22 @@
 RT%0A  %7D;%0A
+  
 %0A   
- %7D
 %0A    %0A  

Update connectSkynet.js