 
   constructor : (@storageManager)->
     @currentCount = 0
+    @initLastDump()
+    @dumpServerURL = "http://127.0.0.1:8080/"
     @checkIfNeedNewContext()
+    @dumpDailyData()
     console.log "Toolbox for the user study is ready"
+  
+  initLastDump : () ->
+    lastDump = localStorage.getItem 'lastDump'    
+    if not lastDump?
+      localStorage.setItem 'lastDump', 0
   
   onMessage : (message, sender, sendResponse) ->
     switch(message.title)

    if (new Date().getTime() - @getLastActivityTime())>10*1000*60
      chrome.tabs.create {'url': chrome.extension.getURL('html/sessionInfo.html'), pinned:true}
    @updateLastActivityTime()
    
  getUserStudyWebsites : (sendResponse) ->
    query = new mem0r1es.Query().from("parameters").where("parameterId", "equals", "userStudyWebsites")
    @storageManager.get query, (results) ->
      if results.length is 1
        sendResponse results[0].value
        return
      else
        sendResponse {}
        return
      return
    return

  deleteUserStudyWebsite : (websiteId, sendResponse) =>
    @getUserStudyWebsites (websites) =>
      delete websites[websiteId]
      @storageManager.store "parameters", {parameterId:"userStudyWebsites", value:websites}, sendResponse
    
  storeUserStudyWebsite : (website, sendResponse) =>
    @getUserStudyWebsites (websites) =>
      websites[website.websiteId] = website
      @storageManager.store "parameters", {parameterId:"userStudyWebsites", value:websites}, sendResponse
  
  countDumpedData : (sendResponse) ->
    sendResponse @currentCount
    
  countDumpData : (sendResponse) ->
    @currentCount = 0
    @getUserStudyWebsites (websites) =>
      count = Object.keys(websites).length
      if count is 0
        sendResponse 1
        return
      total = 0
      for websiteId, website of websites
        @currentCount++
        do (website) =>
          query = new mem0r1es.Query().from("temporary").where("URL", "between", "#{website.pattern}", false , "#{website.pattern.slice 0,-1}#{String.fromCharCode(website.pattern.charCodeAt(website.pattern.length-1)+1) }", true)
          @storageManager.count query, (results) =>
            total += results
            if count is 1
              sendResponse total
            else
              count--
          
  dumpData : (sendResponse) ->
    console.log "dumping data for user study"
    dump = {}
    lastDump = localStorage.getItem 'lastActivityTime'
    if lastDump is null
      lastDump = 0
    
    @getUserStudyWebsites (websites) =>
      count = Object.keys(websites).length
      if count is 0
        @currentCount = 1
        sendResponse dump
        return
      for websiteId, website of websites
        do (website) =>
          query = new mem0r1es.Query().from("temporary").where("URL", "between", "#{website.pattern}", false , "#{website.pattern.slice 0,-1}#{String.fromCharCode(website.pattern.charCodeAt(website.pattern.length-1)+1) }", true).getChildren [{name:"userAction", objectStore:"userActions"},{name:"screenshot", objectStore:"screenshots"}]
          
          @storageManager.get query, (results) =>
            subcount = results.length
            for result in results
              do(result) =>
                query = new mem0r1es.Query().from("userStudySessions").where("userStudySessionId", "equals", parseInt(result._userStudySessionId, 10))
                @storageManager.get query, (subResults) =>
                  result.userStudySession = subResults[0]
                  if subcount is 1
                    dump[website.title] = results
                    if count is 1
                      sendResponse dump
                    else
                      count--
                  else
                    subcount--
                    @currentCount++
                    
  storeMem0r1esFile : (messageContent, sendResponse) ->
    @storageManager.clearStore "temporary"

    @storageManager.clearStore "screenshots"
    try
      mem0ries = JSON.parse messageContent     
    catch error 
      sendResponse "Invalid json"

    count=0
    
    for key, session of mem0ries
      @storageManager.store "userStudySessions", session.userStudySession
      for page in session.pages
        count++
        @storageManager.store "temporary", page, () =>
          count--
          if count is 0
            sendResponse "Mem0r1es Loaded"
  
  countMem0r1es : (sendResponse) =>
    query = new mem0r1es.Query().from("temporary")
