var actionUtil = require('../../node_modules/sails/lib/hooks/blueprints/actionUtil');
/**
 * UserController
 *
 * @description :: Server-side logic for managing users
 * @help        :: See http://links.sailsjs.org/docs/controllers
 */

module.exports = {


  /**
   * `UserController.login()`
   */
  login: function (req, res) {

    // See `api/responses/login.js`
    return res.login({
      username: req.param('username'),
      //password: req.param('password'),
      successRedirect: '/',
      invalidRedirect: '/login'
    });
  },


  /**
   * `UserController.logout()`
   */
  logout: function (req, res) {

    // "Forget" the user from the session.
    // Subsequent requests from this user agent will NOT have `req.session.me`.
    req.session.me = null;

    // If this is not an HTML-wanting browser, e.g. AJAX/sockets/cURL/etc.,
    // send a simple response letting the user agent know they were logged out
    // successfully.
    if (req.wantsJSON) {
      return res.ok('Logged out successfully!');
    }

    // Otherwise if this is an HTML-wanting browser, do a redirect.
    return res.redirect('/');
  },


  /**
   * `UserController.signup()`
   */
  register: function (req, res) {

    // Attempt to signup a user using the provided parameters
    User.findOrCreate({
      username: req.param('username'),
      //password: req.param('password'),
      token: req.param('token')
    },{
      username: req.param('username'),
      //password: req.param('password'),
      token: req.param('token')
    }, function (err, user) {
      // res.negotiate() will determine if this is a validation error
      // or some kind of unexpected server error, then call `res.badRequest()`
      // or `res.serverError()` accordingly.
      if (err) return res.negotiate(err);

      // Go ahead and log this user in as well.
      // We do this by "remembering" the user in the session.
      // Subsequent requests from this user agent will have `req.session.me` set.
      req.session.me = user.id;

      // If this is not an HTML-wanting browser, e.g. AJAX/sockets/cURL/etc.,
      // send a 200 response letting the user agent know the signup was successful.
      if (req.wantsJSON) {
        return res.redirect('/api/user/list?token=' + user.token);
      }

      // Otherwise if this is an HTML-wanting browser, redirect to /welcome.
      return res.redirect('/welcome');
    });
  },


  // poke: function poke(req, res) {
  //   var useri = "meitsi";
  //   User.findOne({
  //     username: useri,
  //   }, function(err, user) {
  //     if (err) return res.negotiate(err);
  //     if(!user) return res.send(404,"User Not Found");
  //     sails.log("Pushing...");
  //     sails.log("Token: [" + user.token + "]");
  //     sails.log("Server API key: " + sails.config.GCMkey);
  //     PusherService
  //       .send([user.token], {
  //         body: "Hello world!"
  //       })
  //       /*
  //       .then(function(response) {sails.log("Push OK: " + JSON.stringify(response)); res.ok("Push sent!");})
  //       .catch(function(err) {sails.log("Push FAILED: " + err); return res.negotiate();});
  //       */
  //      .then(console.log.bind(console))
  //      .catch(console.error.bind(console));
  //   })
  // },

  poke: function poke(req, res) {
    var useri = req.param("username");
    if(!useri) return res.badRequest({error: "Request must have username."});
    User.findOne({username: useri}, function(err, user) {
      if(err) return res.negotiate(err);
      if(!user) return res.notFound() && sails.log("404 User Not Found");

      sails.hooks["sp-gcm"].sendToUser(useri, "Testing", "Testing");
      res.ok();
    })
  },

  list: function list(req, res) {
    User.authUser(req, function(err, user) {
      if(err) return res.negotiate(err);
      if(!user) return res.notFound();
      // Find all users except the one asking the list
      User.find({
        where: {username: {"!": req.param("username")} },
        select: ['username']

      }, function(err, users) {
        if(err) return res.negotiate(err);
        // Make array of usernames
        var userArray = users.map(function(a) {return a.username;});
        res.ok(JSON.stringify(userArray));
      });
    });
  },
  channels: function channels(req, res) {
    User.authUser(req, function(err, user) {
      User.findOne({username: user.username}).populate('channels')
        .exec(function (err, channels) {
          if(err) return res.negotiate(err);
          return res.json(channels.channels);
        });
    })

  },
  find: function find(req, res ) {

    var query = User.find()
      .where( actionUtil.parseCriteria(req))
      .limit( actionUtil.parseLimit(req) )
      .skip( actionUtil.parseSkip(req) )
      .sort( actionUtil.parseSort(req) );
      query = actionUtil.populateRequest(query, req);
      query.exec(function found(err, matchingRecords) {
        if (err) return res.serverError(err);

        var userArray = matchingRecords.map(function(a) {return a.username;});
        matchingRecords = userArray;
        // Only `.watch()` for new instances of the model if
        // `autoWatch` is enabled.
        if (req._sails.hooks.pubsub && req.isSocket) {
          User.subscribe(req, matchingRecords);
          if (req.options.autoWatch) { User.watch(req); }
          // Also subscribe to instances of all associated models
          _.each(matchingRecords, function (record) {
            actionUtil.subscribeDeep(req, record);
          });
        }

        res.ok(matchingRecords);
      });
    },
    // Show the users info. Only accessible for the same user.
    // Doesn't show the password
    me: function(req, res) {
      User.authUser(req, function(err, user) {
        if(err) return res.negotiate(err);

        return (function(){
          var obj = user;
          return res.ok(user.me());
        })();
      })
    },
    // Send a message to user via GCM XMPP
    message: function(req, res) {
      User.authUser(req, function(err, user) {
        if(err) return res.negotiate(err);
        var receiverName = req.param('name');
        var message = req.param('message');
        if(!receiverName || !message) {
          return res.badRequest({error: "'name' and 'message' required."});
        }

        // Tell a white lie to the client
        res.ok();
        sails.hooks["sp-gcm"].sendToUser(receiverName, user.username, message);
      })
    }
};

@@ -3942,35 +3942,29 @@
 : %7B%22!%22: 
-req.param(%22
+user.
 username
 %22)%7D %7D,%0A 
@@ -3955,18 +3955,16 @@
 username
-%22)
 %7D %7D,%0A   

Fix user/list query criteria