/**
 * Copyright (C) 2015 yanni4night.com
 * back.js
 *
 * changelog
 * 2015-09-14[15:49:25]:revised
 *
 * @author yanni4night@gmail.com
 * @version 0.1.0
 * @since 0.1.0
 */

require(['./scripts/meetings'], function(Meetings) {

    var MeetingsChecker = function() {
        var started = false;
        var errTimes = 0;
        var lastNotification;

        var MAX_ERR_TIMES = 5;

        this.start = function() {
            var self = this;

            if (started) {
                return self;
            }

            chrome.alarms.create('checkMeetings', {
                when: Date.now() + 2e3,
                periodInMinutes: 1
            });

            chrome.alarms.onAlarm.addListener(function(alarm) {
                Meetings.list(function(err, scheduleList) {

                    if (err) {
                        // Do not report error duplicately.
                        if (++errTimes > MAX_ERR_TIMES) {
                            self.reportError(err.message || '获取会议室信息失败');

                            chrome.browserAction.setBadgeText({
                                text: 'error'
                            });

                            errTimes = 0;
                        }

                        return;
                    }


                    errTimes = 0;

                    var schedules = scheduleList.schedules;

                    var onSchedulingSize = schedules.filter(function(item) {
                        return '未开始' === item['当前状态']
                    }).length;

                    var onCheckingin = schedules.filter(function(item) {
                        return '可签入' === item['当前状态']
                    });

                    chrome.browserAction.setBadgeText({
                        text: onSchedulingSize + '(' + onCheckingin.length +
                            ')'
                    });

                    if (onCheckingin.length) {
                        if (lastNotification) {
                            lastNotification.close();
                        }

                        var meetingRoomName = onCheckingin[0]['会议室名称'];
                        var tipStr = '您有需要签到的会议室：' + meetingRoomName;

                        var meetingRoomInfo = '\n开始时间：' + onCheckingin[0]['开始时间'] +
                            '\n位置：' + onCheckingin[0]['地域'] + '-' +
                            onCheckingin[0]['楼层'] + '-' + onCheckingin[0]['会议室描述'];

                        var id = (onCheckingin[0]['操作'].match(/\b\d+\b/) || [])[0];

                        if (id) {
                            Meetings.checkin(id, function(err, data) {
                                if (err) {
                                    lastNotification = new Notification('会议室签到', {
                                        icon: 'img/ask.png',
                                        body: '会议室【' + meetingRoomName + '】自动签到失败，请手动签到' + meetingRoomInfo
                                    });
                                } else {
                                    lastNotification = new Notification('会议室签到', {
                                        icon: 'img/success.png',
                                        body: '会议室【' + meetingRoomName + '】已自动签到成功' + meetingRoomInfo
                                    });
                                }
                            });
                        } else {
                            lastNotification = new Notification('会议室签到', {
                                icon: 'img/ask.png',
                                body: tipStr + meetingRoomInfo
                            });
                            // Speak out
                            chrome.tts.speak(tipStr, {
                                lang: 'zh-CN',
                                rate: 1.0,
                                enqueue: true
                            }, function() {});
                        }
                    }
                });
            });

            return self;
        };


        this.reportError = function(errmsg) {
            //Prevent from mutiple notifications
            if (lastNotification) {
                lastNotification.close();
            }

            lastNotification = new Notification('出错', {
                icon: 'img/warn.png',
                body: errmsg
            });
        };
    };

    console.debug('Start checking meetings');
    new MeetingsChecker().start();

});
@@ -94,16 +94,62 @@
 revised%0A
+ * 2015-09-17%5B17:19:40%5D:refresh after checkin%0A
  *%0A * @a
@@ -192,17 +192,17 @@
 ion 0.1.
-0
+1
 %0A * @sin
@@ -255,16 +255,17 @@
 function
+ 
 (Meeting
@@ -296,32 +296,33 @@
 ecker = function
+ 
 () %7B%0A        var
@@ -453,24 +453,25 @@
 t = function
+ 
 () %7B%0A       
@@ -724,50 +724,35 @@
     
-chrome.alarms.onAlarm.addListener(
+var listMeetings = 
 function
 (ala
@@ -751,14 +751,10 @@
 tion
+ 
 (
-alarm
 ) %7B%0A
@@ -787,24 +787,25 @@
 ist(function
+ 
 (err, schedu
@@ -1463,32 +1463,33 @@
 .filter(function
+ 
 (item) %7B%0A       
@@ -1631,16 +1631,17 @@
 function
+ 
 (item) %7B
@@ -2638,24 +2638,25 @@
 id, function
+ 
 (err, data) 
@@ -2920,26 +2920,114 @@
 me +
- '%E3%80%91%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0%E5%A4%B1%E8%B4%A5%EF%BC%8C%E8%AF%B7%E6%89%8B%E5%8A%A8%E7%AD%BE%E5%88%B0' +
+%0A                                            '%E3%80%91%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0%E5%A4%B1%E8%B4%A5%EF%BC%8C%E8%AF%B7%E6%89%8B%E5%8A%A8%E7%AD%BE%E5%88%B0' +%0A                                           
  mee
@@ -3340,16 +3340,60 @@
 omName +
+%0A                                           
  '%E3%80%91%E5%B7%B2%E8%87%AA%E5%8A%A8%E7%AD%BE%E5%88%B0
@@ -3445,32 +3445,152 @@
              %7D);
+%0A%0A                                    //Refresh meetings immediately%0A                                    listMeetings();
 %0A               
@@ -4167,16 +4167,17 @@
 function
+ 
 () %7B%7D);%0A
@@ -4249,32 +4249,93 @@
 );%0A            %7D
+;%0A%0A            chrome.alarms.onAlarm.addListener(listMeetings
 );%0A%0A            
@@ -4395,16 +4395,17 @@
 function
+ 
 (errmsg)

refresh after checkin