var common = require('../common.js').common;

// NOTE this test will fail if run standalone, because
// other tests send a message before this one is run,
// changing the unread counts. uncomment line 43 to run
// standalone
// Silence jslint errors about the "process_visible_unread_messages" global
/*global process_visible_unread_messages: true */

function send_with_content(content) {
    common.send_message('stream', {
            stream:  'Venice',
            subject: 'unread test',
            content: content
    });
}

// Iago
common.start_and_log_in(undefined, {width: 1280, height: 350});
casper.then(function () {
    send_with_content('Iago unread test 0');
});
common.then_log_out();

// Othello
casper.open("http://localhost:9981/accounts/login");
common.then_log_in({username: 'othello@humbughq.com', password: '52VeZqtHDCdAr5yM'});

casper.then(function () {
    // Force a pointer update so we no longer have a -1 pointer in the db
    var a = casper.evaluate(function () {
        $.post("/json/update_pointer", {pointer: 1});
    });
});

common.then_log_out();
common.then_log_in({username: 'iago@humbughq.com', password: 'FlokrWdZefyEWkfI'});

send_with_content('Iago unread test 1');
send_with_content('Iago unread test 2');
send_with_content('Iago unread test 3');
send_with_content('Iago unread test 4');
send_with_content('Iago unread test 5');
send_with_content('Iago unread test 6');
send_with_content('Iago unread test 7');
send_with_content('Iago unread test 8');
send_with_content('Iago unread test 9');
send_with_content('Iago unread test 10');
send_with_content('Iago unread test 11');
send_with_content('Iago unread test 12');
send_with_content('Iago unread test 13');
common.then_log_out();

common.then_log_in({username: 'othello@humbughq.com', password: '52VeZqtHDCdAr5yM'});
casper.then(function () {
    // Make sure we have 3 unread messages
    casper.test.assertSelectorHasText("a[href='#narrow/stream/Venice']", 'Venice', 'Unread count in sidebar is correct');
});

// Sending a message should not increase the count
send_with_content('Othello unread test 4');
send_with_content('Othello unread test 5');

casper.then(function () {
    function get_sidebar() { return casper.evaluate(function () { return $("a[href='#narrow/stream/Venice']").text(); }); }
    function get_sidebar_num() { return parseInt(get_sidebar().match(/\w+\((\d+)\)/)[1], 10); }
    function send_key(key) { casper.page.sendEvent('keypress', key); casper.wait(50); }
    function scroll_to(ypos) {
        // Changing the scroll position in phantomjs doesn't seem to trigger on-scroll
        // handlers, so unread messages are not handled
        casper.page.scrollPosition = {top: ypos, left: 0};
        casper.evaluate(function () { process_visible_unread_messages(); });
    }
    scroll_to(0);

    // Due to font size and height variance across platforms, we are restricted from checking specific
    // unread counts as they might not be consistent. However, we do know that after scrolling
    // down more messages should be marked as read
    var sidebar_initial = get_sidebar_num();
    scroll_to(100);
    scroll_to(200);
    scroll_to(300);
    scroll_to(400);
    scroll_to(500);
    scroll_to(600);
    scroll_to(700);
    scroll_to(800);
    var sidebar_end = get_sidebar_num();
    casper.test.assert(sidebar_end < sidebar_initial, "Unread count in sidebar decreases after scrolling");

});
common.then_log_out();
common.then_log_in({username: 'othello@humbughq.com', password: '52VeZqtHDCdAr5yM'});
casper.then(function () {
    casper.test.assertSelectorHasText("a[href='#narrow/stream/Venice']", 'Venice', 'Sidebar unread correct on login');
});

// Run the above queued actions.
casper.run(function () {
    casper.test.done(3);
});

@@ -343,16 +343,55 @@
  true */
+%0A/*global keep_pointer_in_view: true */
 %0A%0Afuncti
@@ -634,10 +634,10 @@
 ht: 
-35
+60
 0%7D);
@@ -2372,25 +2372,29 @@
 () %7B
- return parseInt(
+%0A        var match = 
 get_
@@ -2428,19 +2428,127 @@
 %5C)/)
-%5B1%5D, 10); %7D
+;%0A        if (match) %7B%0A           return parseInt(match%5B1%5D, 10);%0A        %7D else %7B%0A           return 0;%0A        %7D%0A    %7D%0A
 %0A   
@@ -2628,24 +2628,25 @@
 wait(50); %7D%0A
+%0A
     function
@@ -2903,16 +2903,78 @@
 ion () %7B
+ keep_pointer_in_view();%0A                                     
  process
@@ -3007,24 +3007,39 @@
 ; %7D);%0A    %7D%0A
+    var i = 0;%0A
     scroll_t
@@ -3347,124 +3347,45 @@
     
-scroll_to(100);%0A    scroll_to(200);%0A    scroll_to(300);%0A    scroll_to(400);%0A    scroll_to(500);
+for(i = 0; i %3C 1500; i += 100) %7B
 %0A    
-scroll_to(600);%0A
     
@@ -3398,33 +3398,18 @@
 _to(
-700
+i
 );%0A    
-scroll_to(800);
+%7D%0A
 %0A   

Fix unread count test by being slightly more robust