var browserUI = require('browserUI.js')
var searchbarUtils = require('searchbar/searchbarUtils.js')
var urlParser = require('util/urlParser.js')

var readerDecision = require('readerDecision.js')

var readerView = {
  readerURL: urlParser.getFileURL(__dirname + '/reader/index.html'),
  getReaderURL: function (url) {
    return readerView.readerURL + '?url=' + url
  },
  isReader: function (tabId) {
    return tabs.get(tabId).url.indexOf(readerView.readerURL) === 0
  },
  getButton: function (tabId) {
    // TODO better icon
    var item = document.createElement('i')
    item.className = 'fa fa-align-left reader-button'

    item.setAttribute('data-tab', tabId)
    item.setAttribute('title', l('enterReaderView'))

    item.addEventListener('click', function (e) {
      var tabId = this.getAttribute('data-tab')

      e.stopPropagation()

      if (readerView.isReader(tabId)) {
        readerView.exit(tabId)
      } else {
        readerView.enter(tabId)
      }
    })

    return item
  },
  updateButton: function (tabId) {
    var button = document.querySelector('.reader-button[data-tab="{id}"]'.replace('{id}', tabId))
    var tab = tabs.get(tabId)

    if (readerView.isReader(tabId)) {
      button.classList.add('is-reader')
      button.setAttribute('title', l('exitReaderView'))
      return
    } else {
      button.classList.remove('is-reader')
      button.setAttribute('title', l('enterReaderView'))
    }

    if (tab.readerable) {
      button.classList.add('can-reader')
    } else {
      button.classList.remove('can-reader')
    }
  },
  enter: function (tabId, url) {
    browserUI.navigate(tabId, readerView.readerURL + '?url=' + encodeURIComponent(url || tabs.get(tabId).url))
  },
  exit: function (tabId) {
    browserUI.navigate(tabId, decodeURIComponent(tabs.get(tabId).url.split('?url=')[1]))
  },
  printArticle: function (tabId) {
    if (!readerView.isReader(tabId)) {
      throw new Error("attempting to print in a tab that isn't a reader page")
    }

    webviews.get(tabId).executeJavaScript('parentProcessActions.printArticle()', false)
  },
  showReadingList: function (container, filterText) {
    db.readingList.orderBy('time').reverse().toArray().then(function (articles) {
      empty(container)

      if (articles.length === 0) {
        var item = searchbarUtils.createItem({
          title: l('emptyReadingListTitle'),
          descriptionBlock: l('emptyReadingListSubtitle')
        })

        container.appendChild(item)
        return
      }

      articles.forEach(function (article) {
        if (!article.article) {
          return
        }

        // TODO integrate this with the regular history search system

        if (filterText) {
          var normalizedFilterText = filterText.trim().toLowerCase().replace(/\s+/g, ' ').replace(/\W+/g, '')
          var normalizedArticleText = (article.url + article.article.title + article.article.excerpt).trim().toLowerCase().replace(/\s+/g, ' ').replace(/\W+/g, '')
          if (normalizedArticleText.indexOf(normalizedFilterText) === -1) {
            return
          }
        }

        var item = searchbarUtils.createItem({
          title: article.article.title,
          descriptionBlock: article.article.excerpt,
          url: readerView.getReaderURL(article.url),
          delete: function (el) {
            db.readingList.where('url').equals(el.getAttribute('data-url')).delete()
          }
        })

        if (article.visitCount > 5 || (article.extraData.scrollPosition > 0 && article.extraData.articleScrollLength - article.extraData.scrollPosition < 1000)) { // the article has been visited frequently, or the scroll position is at the bottom
          item.style.opacity = 0.65
        }

        container.appendChild(item)
      })
    })
  }
}

window.readerView = readerView

/* typing !readinglist in the searchbar shows the list */

registerCustomBang({
  phrase: '!readinglist',
  snippet: l('viewReadingList'),
  isAction: false,
  showSuggestions: function (text, input, event, container) {
    readerView.showReadingList(container, text)
  }
})

// update the reader button on page load

webviews.bindEvent('did-start-navigation', function (e, url, isInPlace, isMainFrame, frameProcessId, frameRoutingId) {
  var tab = webviews.getTabFromContents(this)
  if (readerDecision.shouldRedirect(url) === 1) {
    // if this URL has previously been marked as readerable, load reader view without waiting for the page to load
    readerView.enter(tab, url)
  } else if (isMainFrame && !isInPlace) {
    tabs.update(tab, {
      readerable: false // assume the new page can't be readered, we'll get another message if it can
    })

    readerView.updateButton(tab)
  }
})

webviews.bindIPC('canReader', function (webview, tab) {
  if (readerDecision.shouldRedirect(tabs.get(tab).url) >= 0) {
    // if automatic reader mode has been enabled for this domain, and the page is readerable, enter reader mode
    readerView.enter(tab)
  }

  tabs.update(tab, {
    readerable: true
  })
  readerView.updateButton(tab)
})

@@ -531,20 +531,22 @@
     var 
-item
+button
  = docum
@@ -572,20 +572,22 @@
 i')%0A    
-item
+button
 .classNa
@@ -625,28 +625,30 @@
 utton'%0A%0A    
-item
+button
 .setAttribut
@@ -672,160 +672,60 @@
 Id)%0A
+%0A
     
-item.setAttribute('title', l('enterReaderView'))%0A%0A    item.addEventListener('click', function (e) %7B%0A      var tabId = this.getAttribute('data-tab')%0A
+button.addEventListener('click', function (e) %7B
 %0A   
@@ -892,17 +892,63 @@
   re
-turn item
+aderView.updateButton(tabId, button)%0A%0A    return button
 %0A  %7D
@@ -972,32 +972,40 @@
  function (tabId
+, button
 ) %7B%0A    var butt
@@ -1000,32 +1000,42 @@
     var button =
+ button %7C%7C
  document.queryS
@@ -1265,29 +1265,16 @@
 View'))%0A
-      return%0A
     %7D el
@@ -1378,23 +1378,19 @@
 View'))%0A
+%0A
   
-  %7D%0A%0A
     if (
@@ -1403,24 +1403,26 @@
 aderable) %7B%0A
+  
       button
@@ -1446,24 +1446,26 @@
 an-reader')%0A
+  
     %7D else %7B
@@ -1463,32 +1463,34 @@
  %7D else %7B%0A      
+  
 button.classList
@@ -1507,24 +1507,32 @@
 an-reader')%0A
+      %7D%0A
     %7D%0A  %7D,%0A 
@@ -1702,24 +1702,178 @@
 n (tabId) %7B%0A
+    // this page should not be automatically readerable in the future%0A    readerDecision.setURLStatus(urlParser.getSourceURL(tabs.get(tabId).url), false)%0A
     browserU

fix reader view bugs