var crypto = require('crypto');
var config = require('../../config');
var router = require('express').Router();
var activity = require('../../modules/activity')();
var async = require('async');
var _ = require('lodash-node');
var errorProxy = require(process.cwd() + '/error');



/**
 *  add activity
 * 
 */
router
  .post('/add', function(req, res) {
    var _activity = req.body;

    async.waterfall(
      [
        function(callback) {
          var err = null;

          if (_activity.user_id == '' || !_.isString(_activity.user_id)) {
            err = errorProxy(1000);
          } else if (_activity.name == '' || !_.isString(_activity.name)){
            err = errorProxy(1200);
          } else if ( _activity.name.length >= 200){
            err = errorProxy(1208);
          } else if (!/^\d+$/.test(_activity.deadline)) {
            err = errorProxy(1201);
          } else if (!/^\d+$/.test(_activity.toplimit)) {
            err = errorProxy(1202);
          } else if (!/^\d+(\.\d+)?$/.test(_activity.cost)) {
            err = errorProxy(1203);
          } else if ( _activity.detail == '' || !_.isString(_activity.detail)) {
            err = errorProxy(1205);
          } else if (_activity.tags == '' || !_.isString(_activity.tags)) {
            err = errorProxy(1206);
          } else if (!_.isArray(_activity.bdloc) || !/[\d.]+\,[\d\s.]+$/.test(_activity.bdloc.join())) {
            err = errorProxy(1207);
          }

          callback(err)
        },
        function(callback){
          activity.add(_activity, function(err, result){
            if (err) {
              callback(errorProxy(2000, err))
            } else {
              callback(null, result)
            }
          })
        }
      ], 
      function(err, data){
            // connection: Object
            // insertedCount: 1
            // insertedId: ObjectID
            // ops: Array[1]
            // result: Object
            // __proto__: Object
          var rslt = err || errorProxy();

          if (!err) {
            rs = rslt.result = {};
            rs._id = data.insertedId.toString();
          } 

          res.json(rslt)
      })
    
  })
  //查询活动列表

  .get('/activities', function(req, res) {
    var params = req.params;

    //qeury
    var query = {};

    //options
    var options = {};
    options.skip = params.page_index || 0;

    async.waterfall(
      [
        function(callback) {
          activity.list(query, options, function(err, activities){
            if (err) {
              callback(errorProxy(2000, err));
            } else {
              callback(null, activities);
            }
          })
        }
      ], 
      function(err, activities) {
        var rslt = null;
        if (err) {
          rslt = err;
        } else {
          
.json(rslt)
      })

  })
  //字符串搜索
  .get('/search', function(req, res) {
      //qeury
      var query = {};

      var word = req.query.word;

      if (word && word.length > 1) {
        word = new RegExp(word, 'i');
        query['$or'] = [{tags: word}, {name: word}];
      } else {
        res.json(errorProxy(2000, '搜索字符串不能少于2个字符'));
        return;
      }

      if (req.query.city) {
        query.city = req.query.city;
      }
      

      //options
      var options = {};
      options.skip = req.query.page_index || 0;

      async.waterfall(
        [
          function(callback) {
            activity.search(query, options, function(err, activities){
              if (err) {
                callback(errorProxy(2000, err));
              } else {
                callback(null, activities);
              }
            })
          }
        ], 
        function(err, activities) {
          var rslt = null;
          if (err) {
            rslt = err;
          } else {
            rslt = errorProxy();
            rslt.result = {};
            rslt.result.activities = activities;
            rslt.result.page_index = options.skip;
          }
          res.json(rslt)
        })

    });



module.exports = router;  
rslt = errorProxy();
          rslt.result = {};
          rslt.result.activities = activities;
          rslt.result.page_index = options.skip;
        }
        res