var weConfig = require('../config')
var OAuth = require('wechat-oauth')
var WechatAPI = require('wechat-api')

var client = new OAuth(weConfig.appid, weConfig.secret)
var api = new WechatAPI(weConfig.appid, weConfig.secret)

module.exports = function(req, res, next){
    var msg = req.weixin

    if(msg.MsgType === 'event' && msg.Event === 'subscribe'){
        var bindingUrl = client.getAuthorizeURL('http://roscoe.cn/info', 'binding', 'snsapi_userinfo');
        res.reply('等你好久了！<a href="' + bindingUrl + '">点击这里</a>以完成绑定(ง •_•)ง')
        next()
    }

    if(msg.MsgType === 'text'){
        if (msg.Content === 'hehe') {
    		res.reply('...')
    	} else {
    		res.reply('这学期暂时不开啦，不好意思〒▽〒')
        }
        next()
    }

	if(msg.MsgType === 'voice'){
		var recognition = msg.Recognition

		// TODO remove this test
		if(recognition && recognition !== ""){
			api.semantic(msg.FromUserName, {
				query: recognition,
				category: 'remind',
				city: '上海'
			}, function(err, result){
                var semantic = result.semantic.details

				if(err){
					res.reply('服务器出了点小问题(´_ゝ`)')
				} else if(!semantic || !semantic.event || !semantic.datetime){
                    res.reply('我好像没理解(´_ゝ`)')
                } else{
                    var remindText = '已设置提醒：\n'
                                + semantic.datetime.date_ori
                                + semantic.datetime.time_ori + '：'
                                + semantic.event + '\n'
                                + '如需取消提醒请<a href="#">点击这里</a>'
					res.reply(remindText)
				}
				next()
			})
		} else{
			res.reply('不好意思没有听清(´_ゝ`)')
			next()
		}
	}
}

@@ -1246,24 +1246,83 @@
             
+ res.reply(JSON.stringify(semantic))%0A                    //
  var remindT
@@ -1349,32 +1349,35 @@
                 
+ //
              + s
@@ -1413,32 +1413,35 @@
                 
+ //
              + s
@@ -1484,32 +1484,34 @@
                 
+//
             + se
@@ -1498,32 +1498,33 @@
   //            
+ 
 + semantic.event
@@ -1529,16 +1529,18 @@
 nt + '%5Cn
+%5Cn
 '%0A      
@@ -1545,32 +1545,34 @@
                 
+//
             + '%E5%A6%82
@@ -1567,16 +1567,17 @@
         
+ 
 + '%E5%A6%82%E9%9C%80%E5%8F%96%E6%B6%88%E6%8F%90
@@ -1601,24 +1601,27 @@
 %E9%87%8C%3C/a%3E'%0A%09%09%09%09%09
+// 
 res.reply(re

test semantic