var moment = require('moment')
var request = require('request')

var mock = {
	recentMeetings: [{
		attendeeOpenIds: ['o1__FsxGOvh12CjS8ZtZqcwYtZXA'],
		meetingName: '我的会议1',
		meetingRoom: '九州岛',
		from: '1460170070059',
		to: '1460170080059',
		bookerName: 'yuhao.ju'
	}],
	newMeetings: [{
		attendeeOpenIds: ['o1__FsxGOvh12CjS8ZtZqcwYtZXA'],
		meetingName: '我的会议2',
		meetingRoom: '格林威治',
		from: '1460170070059',
		to: '1460170080059',
		bookerName: 'yuhao.ju'
	}]
}

var MEETING_TYPE = {
    'NEW': 'new',
    'RECENT': 'recnet'
}

var initSchedule = function() {
	var schedule = require('node-schedule')

	var j = schedule.scheduleJob('*/5 * * * * *', function() {
		console.log('schedule processing')
		getRemindRList(function(resData) {
			sendRemind(resData.recentMeetings, MEETING_TYPE.RECENT)
			sendRemind(resData.newMeetings,  MEETING_TYPE.NEW)
		})
	})
}

function sendRemind(meetings, type) {
    meetings.forEach(function(meeting) {
        var msg = ''

        if(type === MEETING_TYPE.RECENT){
            msg = '哈喽：您近期有一个会议\n'
                + '会议室：' + meeting.meetingRoom + '\n'
                + '时间：' + getFormedDateRange(meeting.from, meeting.to) + '\n'
                + '发起人：' + meeting.bookerName + '\n'
                + '请您准时到场 谢谢'
        } else if(type === MEETING_TYPE.NEW){
            msg = '哈喽：您有一个会议邀请\n'
                + '会议室：' + meeting.meetingRoom + '\n'
                + '时间：' + getFormedDateRange(meeting.from, meeting.to) + '\n'
                + '发起人：' + meeting.bookerName + '\n'
                + '如有特殊情况请联系发起人'
        }

        if (meeting.attendeeOpenIds) {
            meeting.attendeeOpenIds.forEach(function(openid) {
                // FIXME 发送消息
                // api.sendText(openid, 'Hello world2', function(err) {
                // 	if(err){
                //         console.log(err);
                //     }
                // })
                // console.log(msg);
            })
        }
    })
}

function getFormedDateRange(from, to){
    var fromStr = moment(Number(from)).format("M月D日 hh:mm"),
        toStr = moment(Number(to)).format("hh:mm")

    return fromStr + '-' + toStr
}

function getRemindRList(callback) {
	request('http://115.159.119.199:8080/getMessages', function(error, response, body) {
		if (!error && response.statusCode == 200) {
			console.log(body);
			// res.json(JSON.parse(body))
		} else{
			console.log('hehe', response.statusCode);
			console.log(error);
			// res.json(JSON.parse(error))
		}
	})

	callback(mock)
}

module.exports = {
	init: initSchedule
}

@@ -641,11 +641,9 @@
 ('*/
-5 *
+1
  * *
@@ -895,24 +895,78 @@
 gs, type) %7B%0A
+%09if(!meetings %7C%7C meetings.length === 0)%7B%0A%09%09return%0A%09%7D%0A%0A
     meetings
@@ -1763,19 +1763,16 @@
         
- //
  api.sen
@@ -1789,22 +1789,11 @@
 id, 
-'Hello world2'
+msg
 , fu
@@ -1821,19 +1821,16 @@
         
- //
  %09if(err
@@ -1843,27 +1843,24 @@
             
- //
          con
@@ -1889,19 +1889,16 @@
         
- //
      %7D%0A 
@@ -1916,50 +1916,10 @@
     
-// %7D)%0A                // console.log(msg);
+%7D)
 %0A   
@@ -2314,40 +2314,15 @@
 %09%09%09c
-onsole.log(body);%0A%09%09%09// res.json
+allback
 (JSO
@@ -2354,86 +2354,15 @@
 %09%09%09c
-onsole.log('hehe', response.statusCode);%0A%09%09%09console.log(error);%0A%09%09%09// res.json
+allback
 (JSO
@@ -2390,24 +2390,8 @@
 %7D)%0A%0A
-%09callback(mock)%0A
 %7D%0A%0Am

test schedule