var openMessenger = function(tab) {
    if (window.celery_facebook_window == null) {
        return chrome.windows.create({
            url: 'https://www.messenger.com/t',
            type: 'detached_panel',
            focused: true,
            top: 100,
            width: 800,
            height: 560
        }, function(fb_window) {
            window.celery_facebook_window = fb_window;
        });
    } else {
        return chrome.windows.update(window.celery_facebook_window.id, {
            focused: true
        });
    }
};

var last_check = 0;
var re = /(.*)[ ][(](.*)[)]/;
var re_convo_id = /msg_id[.](.*)/;
var re_strip_script = /(<script.+?<\/script>)/g;

function getMinMaxTime(messages) {
    var times = [];
    for (var i = messages.length - 1; i >= 0; i--) {
        times.push(messages[i].time);
    };
    return {
        max: Math.max.apply(Math, times),
        min: Math.min.apply(Math, times)
    };
}

function concat(messages) {
    var r = 'Facebook Messenger';
    for (var i = messages.length - 1; i >= 0; i--) {
        r = r + "\n \u25CF " + messages[i].name
    };
    return r;
}

function parseHtml(body_text) {
    body_text = body_text.replace(re_strip_script, '');
    var messages_body = $(body_text);
    var all_items = $('.aclb.messages-flyout-item', messages_body);
    return all_items;
}

function parseMessage(all_items) {
    var allMessages = [];
    for (var i = all_items.length - 1; i >= 0; i--) {
        var item = all_items[i];
        var name = $('.lr .title strong', item).first().text();
        var preview = $('.preview span', item).first().text();
        var time_data = $('abbr[data-sigil="timestamp"]', item).first().attr('data-store');
        var profpic = $('.img', item).first().css('background-image').replace('url(', '').replace(')', '');
        allMessages.push({
            name: name,
            preview: preview,
            time: JSON.parse(time_data).time,
            profpic: profpic
        });
    };
    return allMessages;
}

function setBrowserAction(messages) {
    chrome.browserAction.setBadgeText({
        text: (messages.length > 0) ? ("" + messages.length) : ''
    });

    chrome.browserAction.setTitle({
        title: concat(messages)
    });
}

function createNotification(message) {
    var notification = new Notification(message.name, {
        icon: message.profpic,
        body: message.preview,
    });
    notification.onclick = openMessenger;
    setTimeout(function() {
        notification.close();
    }, 5000);
}

function sendNotyToContent(messages) {
    chrome.tabs.query({
        active: true,
        currentWindow: true
    }, function(tabs) {
        if (tabs.length == 0) return;
        chrome.tabs.sendMessage(tabs[0].id, {
            action: 'new_msg',
            messages: messages
        }, function(response) {});
    });
}

function sendNotification(messages) {
    if (messages.length == 0) {
        return;
    }    

    var time = getMinMaxTime(messages);

    if (time.min > last_check) {
        last_check = time.max;
        console.log('' + new Date() + ': set last_check = ' + last_check);
        sendNotyToContent(messages);
        // for (var i = messages.length - 1; i >= 0; i--) {
        //     var message = messages[i];
        //     createNotification(message);
        // };
    }
}

function checkFacebook() {
    return $.ajax({
        type: "GET",
        dataType: "text",
        url: "https://m.facebook.com/messages",
        cache: false,
        timeout: 20000,
        success: function(data) {            
            var all_items = parseHtml(data);
            var messages = parseMessage(all_items);

            setBrowserAction(messages);
            sendNotification(messages);
        }
    });
}

var schedule = function() {
    checkFacebook();
    return setTimeout((function() {
        return schedule();
    }), 5000);
};

// Execute script
if (Notification.permission !== "granted") {
    Notification.requestPermission();
}
chrome.browserAction.onClicked.addListener(openMessenger);
schedule();

chrome.windows.onRemoved.addListener(function(windowId) {
    if (window.celery_facebook_window != null && window.celery_facebook_window.id == windowId) {
        window.celery_facebook_window = null;
    }
});
@@ -787,19 +787,19 @@
     
-times.push(
+var time = 
 mess
@@ -810,16 +810,41 @@
 %5Bi%5D.time
+;%0A        times.push(time
 );%0A    %7D
@@ -2544,24 +2544,282 @@
 , 5000);%0A%7D%0A%0A
+function filterMessage(messages) %7B%0A    var filter = %5B%5D;%0A    for (var i = messages.length - 1; i %3E= 0; i--) %7B%0A        var message = messages%5Bi%5D;%0A        if (message.time %3E last_check) %7B%0A            filter.push(message);%0A        %7D%0A    %7D;%0A    return filter;%0A%7D%0A%0A
 function sen
@@ -2837,32 +2837,32 @@
 ent(messages) %7B%0A
-
     chrome.tabs.
@@ -3231,17 +3231,16 @@
   %7D    %0A
-%0A
     var 
@@ -3267,25 +3267,24 @@
 (messages);%0A
-%0A
     if (time
@@ -3451,168 +3451,8 @@
 s);%0A
-        // for (var i = messages.length - 1; i %3E= 0; i--) %7B%0A        //     var message = messages%5Bi%5D;%0A        //     createNotification(message);%0A        // %7D;%0A
     
@@ -3849,32 +3849,46 @@
 endNotification(
+filterMessage(
 messages);%0A     
@@ -3872,32 +3872,33 @@
 essage(messages)
+)
 ;%0A        %7D%0A    

filter new message by timestamp