var fs = require('fs')
var get = require('simple-get')
var moment = require('moment')
var P2PGraph = require('p2p-graph')
var path = require('path')
var prettierBytes = require('prettier-bytes')
var throttle = require('throttleit')
var WebTorrent = require('webtorrent')

var TORRENT = fs.readFileSync(
  path.join(__dirname, '../../static/torrents/sintel.torrent')
)

module.exports = function () {
  var graph
  var hero = document.querySelector('#hero')

  // Don't start the demo automatically on mobile.
  if (window.innerWidth <= 899) {
    var beginButton = document.createElement('a')
    beginButton.href = '#'
    beginButton.id = 'begin'
    beginButton.className = 'btn large'
    beginButton.textContent = 'Begin Demo'

    beginButton.addEventListener('click', function onClick () {
      beginButton.removeEventListener('click', onClick, false)
      beginButton.parentNode.removeChild(beginButton)
      beginButton = null

      init()
    })
    hero.appendChild(beginButton)
  } else {
    init()
  }

  function init () {
    // Display video and related information.
    hero.className = 'loading'
    hero = null

    graph = window.graph = new P2PGraph('.torrent-graph')

    getRtcConfig(function (err, rtcConfig) {
      if (err) console.error(err)
      createClient(rtcConfig)
    })
  }

  var torrent
  function createClient (rtcConfig) {
    var client = window.client = new WebTorrent({
      tracker: {
        rtcConfig: rtcConfig
      }
    })
    client.on('warning', onWarning)
    client.on('error', onError)

    torrent = client.add(TORRENT, onTorrent)
    graph.add({ id: 'You', name: 'You', me: true })
  }

  var $body = document.body
  var $progressBar = document.querySelector('#progressBar')
  var $numPeers = document.querySelector('#numPeers')
  var $downloaded = document.querySelector('#downloaded')
  var $total = document.querySelector('#total')
  var $remaining = document.querySelector('#remaining')

  function onTorrent () {
    var file = torrent.files.find(function (file) {
      return file.name.endsWith('.mp4')
    })

    var opts = {
      autoplay: true,
      muted: true
    }

    var videoOverlay = document.querySelector('.videoOverlay')

    file.appendTo('#videoWrap .video', opts, function (err, elem) {
      if (err) return onError(err)
      elem.parentElement.classList.add('canplay')
      elem.parentElement.classList.add('muted')

      videoOverlay.addEventListener('click', onClick1)

      // First click unmutes the video!
      function onClick1 () {
        videoOverlay.removeEventListener('click', onClick1)

        elem.muted = false
        elem.parentElement.classList.remove('muted')
      }
    })

    torrent.on('wire', onWire)
    torrent.on('done', onDone)

    torrent.on('download', throttle(onProgress, 250))
    torrent.on('upload', throttle(onProgress, 250))
    setInterval(onProgress, 5000)
    onProgress()
  }

  function onWire (wire) {
    var id = wire.peerId.toString()
    graph.add({ id: id, name: wire.remoteAddress || 'Unknown' })
    graph.connect('You', id)
    wire.once('close', function () {
      graph.disconnect('You', id)
      graph.remove(id)
    })
  }

  function onProgress () {
    var percent = Math.round(torrent.progress * 100 * 100) / 100
    $progressBar.style.width = percent + '%'
    $numPeers.innerHTML = torrent.numPeers + (torrent.numPeers === 1 ? ' peer' : ' peers')

    $downloaded.innerHTML = prettierBytes(torrent.downloaded)
    $total.innerHTML = prettierBytes(torrent.length)

    var remaining
    if (torrent.done) {
      remaining = 'Done.'
    } else {
      remaining = moment.duration(torrent.timeRemaining / 1000, 'seconds').humanize()
      remaining = remaining[0].toUpperCase() + remaining.substring(1) + ' remaining.'
    }
    $remaining.innerHTML = remaining
  }

  function onDone () {
    $body.className += ' is-seed'
    onProgress()
  }

  function onError (err) {
    if (err) {
      window.alert(err)
      console.error(err)
    }
  }

  function onWarning (err) {
    if (err) {
      console.error(err)
    }
  }

  function getRtcConfig (cb) {
    // WARNING: This is *NOT* a public endpoint. Do not depend on it in your app.
    get.concat({
      url: 'https://instant.io/__rtcConfig__',
      timeout: 5000
    }, function (err, res, data) {
      if (err || res.statusCode !== 200) {
        cb(new Error('Could not get WebRTC config from server. Using default (without TURN).'))
      } else {
        var rtcConfig
        try {
          rtcConfig = JSON.parse(data)
        } catch (err) {
          return cb(new Error('Got invalid WebRTC config from server: ' + data))
        }
        cb(null, rtcConfig)
      }
    })
  }
}

@@ -1006,32 +1006,46 @@
     init()%0A  %7D%0A%0A
+  var torrent%0A
   function init 
@@ -1205,181 +1205,81 @@
 h')%0A
-%0A
     g
-etRtcConfig(function (err, rtcConfig) %7B%0A      if (err) console.error(err)%0A      createClient(rtcConfig)%0A    %7D)%0A  %7D%0A%0A  var torrent%0A  function c
+raph.add(%7B id: 'You', name: 'You', me: true %7D)%0A%0A    // C
 reate
-C
+ c
 lient
- (rtcConfig) %7B
 %0A   
@@ -1327,69 +1327,8 @@
 ent(
-%7B%0A      tracker: %7B%0A        rtcConfig: rtcConfig%0A      %7D%0A    %7D
 )%0A  
@@ -1394,16 +1394,38 @@
 Error)%0A%0A
+    // Create torrent%0A
     torr
@@ -1465,60 +1465,8 @@
 nt)%0A
-    graph.add(%7B id: 'You', name: 'You', me: true %7D)%0A
   %7D%0A
@@ -3906,631 +3906,7 @@
 %0A  %7D
-%0A%0A  function getRtcConfig (cb) %7B%0A    // WARNING: This is *NOT* a public endpoint. Do not depend on it in your app.%0A    get.concat(%7B%0A      url: 'https://instant.io/__rtcConfig__',%0A      timeout: 5000%0A    %7D, function (err, res, data) %7B%0A      if (err %7C%7C res.statusCode !== 200) %7B%0A        cb(new Error('Could not get WebRTC config from server. Using default (without TURN).'))%0A      %7D else %7B%0A        var rtcConfig%0A        try %7B%0A          rtcConfig = JSON.parse(data)%0A        %7D catch (err) %7B%0A          return cb(new Error('Got invalid WebRTC config from server: ' + data))%0A        %7D%0A        cb(null, rtcConfig)%0A      %7D%0A    %7D)%0A  %7D
 %0A%7D%0A

Remove TURN server