function handleSubmit () {
  document.location += document.getElementById('url').value
}

function shortestFirst(a, b) {
  if (a.length < b.length) {return -1}
  if (a.length > b.length) {return 1}
  return 0
}

function showURIs () {
  var ul = document.getElementById('uriList')
  if (ul.childNodes.length > 0) {
    return // Prevent multiple adds of the URI list to the DOM
  }

  var htmlPages = 0
  var uriKeys = Object.keys(uris).sort(shortestFirst)
  for (var uri of uriKeys) {
    for (var datetimesI = 0; datetimesI < uris[uri]['datetimes'].length; datetimesI++) {
      var datetime = uris[uri]['datetimes'][datetimesI]

      var li = document.createElement('li')
      var a = document.createElement('a')
      a.href = datetime + '/' + uri
      a.appendChild(document.createTextNode(uri))
      dt = document.createTextNode(' (' + datetime + ')')

      li.appendChild(a)
      li.appendChild(dt)
      li.setAttribute('data-mime', uris[uri]['mime'])
      ul.appendChild(li)
    }
    uris[uri]['mime'] === 'text/html' ? ++htmlPages : ''
  }
  document.getElementById('htmlPages').innerHTML = htmlPages
  document.getElementById('memCountListLink').classList = ['activated']
  document.getElementById('uris').classList.remove('hidden')
  setPlurality()
  setShowAllButtonStatus()
}

function addEventListeners () {
  var target = document.getElementById('memCountListLink')
  target.addEventListener('click', showURIs, false)

  var showAllInListingButton = document.getElementById('showEmbeddedURI')
  showAllInListingButton.onclick = function showAllURIs () {
    document.getElementById('uriList').classList.add('forceDisplay')
  }

  getIPFSWebUIAddress()
}

function setPlurality () {
  var urimCount = document.getElementById('memCountInt').innerHTML
  var htmlFilesPlurality = document.getElementById('htmlPages').innerHTML

  if (urimCount === '1') {
    document.getElementById('plural').classList.add('hidden')
  }
  if (htmlFilesPlurality === '1') {
    document.getElementById('htmlPagesPlurality').classList.add('hidden')
  }
}

function setShowAllButtonStatus () {
  var urimCount = document.getElementById('memCountInt').innerHTML
  var htmlFilesPlurality = document.getElementById('htmlPages').innerHTML
  if (urimCount === htmlFilesPlurality) {
    document.getElementById('showEmbeddedURI').setAttribute('disabled','disabled')
  }
}

function assignStatusButtonHandlers () {
  var button = document.getElementsByTagName('button')[0]
  if (button.innerHTML === 'Start') {
    button.addEventListener('click', startIPFSDaemon)
  } else {
    button.addEventListener('click', stopIPFSDaemon)
  }
}

function startIPFSDaemon () {
  sendCommandToIPFSDaemon('start')
  this.innerHTML = 'Starting...'
  this.setAttribute('disabled', 'disabled')
}

function stopIPFSDaemon () {
  sendCommandToIPFSDaemon('stop')
  this.innerHTML = 'Stopping...'
  this.setAttribute('disabled', 'disabled')
}

function getIPFSWebUIAddress () {
  var setIPFSWebUILink = function (resp) {
    document.getElementById('webui').setAttribute('href', 'http://' + resp)
    console.log(resp)
  }
  var fail = function () { console.log('fail') }
  var err = function () { console.log('err') }
  makeAnAJAXRequest('/config/openEndedPlaceHolder', setIPFSWebUILink, fail, err)
}

function updateIPFSDaemonButtonUI () {
  window.setTimeout(function () {
    document.location.reload(true)
  }, 4000)
}

function sendCommandToIPFSDaemon (cmd) {
  var failFunction = function () { console.log('Comm w/ ipfs daemon failed.') }
  var errFunction = function () { console.log('Error talking to ipfs daemon.') }
  makeAnAJAXRequest('/daemon/' + cmd, updateIPFSDaemonButtonUI,
    failFunction, errFunction)
}

function makeAnAJAXRequest (address, successFunction, failFunction, errorFunction) {
  var xmlhttp = new XMLHttpRequest()

  xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState === XMLHttpRequest.DONE) {
      if (xmlhttp.status === 200) {
        successFunction(xmlhttp.responseText)
      } else if (xmlhttp.status === 400) {
        failFunction()
      } else {
        errorFunction()
      }
    }
  }

  xmlhttp.open('GET', address, true)
  xmlhttp.send()
}

function injectIPWBJS () {
  registerServiceWorker()
  // TODO: Add ipwb replay banner
}



function registerServiceWorker () {
  if ('serviceWorker' in navigator) {
    var newInstallation = false

    if (navigator.serviceWorker.controller === null) { // Ideally we would use serviceWorker.getRegistration
      newInstallation = true
    }
    window.addEventListener('load', function() {
      navigator.serviceWorker.register('/serviceWorker.js').then(function(registration) {
        // Registration was successful
        console.log('ServiceWorker registration successful with scope: ', registration.scope)
      }).catch(function(err) {
        // registration failed :(
        console.log('ServiceWorker registration failed: ', err)
      }).then(function(rr){
        var dt = document.location.href.split('/')[3]
        var viewingMemento = dt.length === 14 && parseInt(dt, 10) + '' === dt

        // Reload the page with processing by the newly installed Service Worker
        if (newInstallation && viewingMemento) {
          document.location.reload()
        }
      })
    })
  } else {
    console.log('Browser does not support Service Worker.')
  }
}

function serviceWorkerUpToDate () {

}

function updateServiceWorker () {

}

function reloadPageFromServiceWorker () {
  console.log('reloading page from serviceWorker!')
}