'use strict';

chrome.runtime.onInstalled.addListener(function (details){
  console.log('previousVersion', details.previousVersion);
});

chrome.tabs.onUpdated.addListener(function (tabId){
  chrome.pageAction.show(tabId);
});

(function() {
  // var MSG_KEY_SYNCBOOKMARK = 'eureka_bookmark_sync';

  function collectBookmarkUrl(urlMap, node) {
    if (node) {
      if (node.url) {
        urlMap[node.url] = node.id;
      }
      var children = node.children;
      if (children) {
        for (var i = 0; i < children.length; i++) {
          collectBookmarkUrl(urlMap, children[i]);
        }
      }
    }
  }

  var reloadBookmarkAlarmStream =
  Rx.Observable.create(function (observer) { 
    var NAME_ALARM_RELOAD_BOOKMARK = 'NAME_ALARM_RELOAD_BOOKMARK';
    chrome.alarms.create(NAME_ALARM_RELOAD_BOOKMARK, {
      delayInMinutes: 1,
      periodInMinutes: 1
    });
    chrome.alarms.onAlarm.addListener(function (alarm){
      if (alarm.name === NAME_ALARM_RELOAD_BOOKMARK) {
        observer.onNext(Date.now());
      }
    });
  }).startWith(Date.now())
  .publish();

  var bookmarkUrlMapStream =
  reloadBookmarkAlarmStream.flatMap(function () {
    return Rx.Observable.create(function (observer) {
      chrome.bookmarks.getTree(function (nodes){
        var urlMap = {};
        collectBookmarkUrl(urlMap, nodes[0]);
        observer.onNext(urlMap);
        observer.onCompleted();
      });
    });
  });

  function syncBookmarkedUrls() {
    bookmarkUrlMapStream.subscribe(function (urlMap){
      console.log('Loaded Bookmarks loaded');

      chrome.runtime.onMessage.addListener(function (request, sender, sendResponse){
        var bookmarkedUrls = request.urls.filter(function (url){
          return urlMap[url];
        });
        sendResponse({bookmarkedUrls: bookmarkedUrls});
      });
    });
  }

  bookmarkUrlMapStream.subscribe(function (urlMap) {
    console.log('Bookmark url map updated');
    console.log(urlMap);
  });

  reloadBookmarkAlarmStream.connect();
})();

@@ -221,16 +221,44 @@
 );%0A%7D);%0A%0A
+var requestHandlingStream = 
 (functio
@@ -1444,21 +1444,20 @@
 %7D);%0A  %7D)
-;%0A%0A  
+.do(
 function
@@ -1461,34 +1461,71 @@
 ion 
-syncBookmarkedUrls() %7B%0A   
+() %7B%0A    console.log('Bookmark url map reloaded');%0A  %7D);%0A%0A  var
  boo
@@ -1533,99 +1533,75 @@
 mark
+ed
 Url
-MapStream.subscribe(function (urlMap)%7B%0A      console.log('Loaded Bookmarks loaded');%0A%0A  
+sRequestStream =%0A  Rx.Observable.create(function (observer) %7B%0A
     
@@ -1677,24 +1677,297 @@
 esponse)%7B%0A  
+    observer.onNext(%7B%0A        request: request,%0A        sender:sender,%0A        sendResponse:sendResponse%7D);%0A    %7D);%0A  %7D).publish();%0A%0A  var requestHandlingStream =%0A  bookmarkUrlMapStream.combineLatest(%0A    bookmarkedUrlsRequestStream,%0A    function (urlMap, requestBundle) %7B%0A
       var bo
@@ -1981,16 +1981,30 @@
 dUrls = 
+requestBundle.
 request.
@@ -2031,18 +2031,16 @@
  (url)%7B%0A
-  
         
@@ -2061,26 +2061,24 @@
 url%5D;%0A      
-  
 %7D);%0A        
@@ -2071,26 +2071,38 @@
   %7D);%0A      
-  
+requestBundle.
 sendResponse
@@ -2147,41 +2147,141 @@
     
-%7D);%0A    %7D);%0A
+var logging = 'Handling request from page %5B' + requestBundle.sender.tab.title + '%5D';%0A      return logging;%0A  
   %7D
+);
 %0A%0A  
-bookmarkUrlMap
+requestHandling
 Stre
@@ -2303,22 +2303,23 @@
 nction (
-urlMap
+logging
 ) %7B%0A    
@@ -2334,106 +2334,136 @@
 log(
-'Bookmark url map updated');%0A    console.log(urlMap);%0A  %7D);%0A%0A  reloadBookmarkAlarmStream.connect()
+logging);%0A  %7D);%0A%0A  reloadBookmarkAlarmStream.connect();%0A  bookmarkedUrlsRequestStream.connect();%0A%0A  return requestHandlingStream
 ;%0A%7D)

connect request and urlmap stream