var Database = require('../persistence/mongo');
var db = new Database();
var isJson = require('../utils/common').isJson;
var authCheck = require('../auth/basic');

module.exports = function (app) {
  'use strict';
  return function (req, res) {
    var deviceRex = /^\/device\/(.*)\?/;

    var other = function () {
      res.code = '4.00';
      res.end(JSON.stringify({method: 'not support'}));
    };

    if (!req.options) {
      return other();
    }
    var existBlock = false;
    var uriPathAuth = '';
    for (var i = 1; i < req.options.length; i++) {
      if (req.options[i].name === 'Uri-Query') {
        uriPathAuth = req.options[i].value.toString();
        existBlock = true;
      }
    }
    if (!existBlock) {
      return other();
    }
    var username = uriPathAuth.split(':')[0];
    var password = uriPathAuth.split(':')[1];
    var userInfo = {
      password: password,
      name: username
    };

    var errorCB = function () {
      res.code = '4.03';
      res.end({});
    };

    var handlerGet = function () {
      var successCB = function (user) {
        var payload = {name: userInfo.name, token: user.uid};
        if((deviceRex.test(req.url))) {
          payload.device = deviceRex.exec(req.url)[1];
        }
        db.query(payload, function (dbResult) {
          res.code = '2.05';
          res.end(JSON.stringify({result: dbResult}));
        });
      };
      authCheck(userInfo, errorCB, successCB, errorCB);
    };

    var handPut = function () {
      if(!(deviceRex.test(req.url))){
        res.code = '4.03';
        res.end(JSON.stringify({"topic": "no exist"}));
      }
      var successCB = function (user) {
        var payload = {name: user.name, token: user.uid, data: req.payload.toString()};
        payload.device = deviceRex.exec(req.url)[1];

        db.update(payload);
        res.code = '2.00';
        res.end(JSON.stringify({method: 'PUT'}));
      };

      authCheck(userInfo, errorCB, successCB, errorCB);
    };

    var handPost = function () {
      var successCB = function (user) {
        var payload = {name: user.name, token: user.uid, data: req.payload.toString()};
        if((deviceRex.test(req.url))) {
          payload.device = deviceRex.exec(req.url)[1];
        }

        db.insert(payload);
        res.code = '2.01';
        res.end(JSON.stringify({method: 'POST'}));
      };

      authCheck(userInfo, errorCB, successCB, errorCB);
    };

    switch (req.method) {
      case 'GET':
        handlerGet();
        break;
      case 'PUT':
        handPut();
        break;
      case 'POST':
        handPost();
        break;
      default:
        return other();
    }
  };
}
;

@@ -262,22 +262,20 @@
 x = /%5E%5C/
-device
+(.*)
 %5C/(.*)%5C?
@@ -1188,39 +1188,60 @@
          payload
-.
+%5B
 device
+Rex.exec(req.url)%5B1%5D%5D
  = deviceRex.exe
@@ -1243,33 +1243,33 @@
 x.exec(req.url)%5B
-1
+2
 %5D;%0A        %7D%0A   
@@ -1778,39 +1778,69 @@
 %0A        payload
-.device
+%5Bpayload%5BdeviceRex.exec(req.url)%5B1%5D%5D%5D
  = deviceRex.exe
@@ -1850,17 +1850,17 @@
 eq.url)%5B
-1
+2
 %5D;%0A%0A    
@@ -2256,15 +2256,36 @@
 load
-.
+%5B
 device
+Rex.exec(req.url)%5B1%5D%5D
  = d
@@ -2307,17 +2307,17 @@
 eq.url)%5B
-1
+2
 %5D;%0A     

make url custom