/* eslint-disable spaced-comment */
var restify = require('restify')
var storage = require('node-persist')
var engine = require('scrape-engine')()
var fs = require('fs')

/*******************************************************************
 * Initialization
 *******************************************************************/

// Initialize node persist storage
storage.init({
  logging: true,
  dir: __dirname + '/store'
})

// Initialize restify
var server = restify.createServer()
server.use(restify.queryParser())
server.use(restify.bodyParser())

// Setup the routes
server.post('/similarTrack', getSimilarTrack)
server.post('/similarArtist', getSimilarArtist)
server.post('/Cover', getCover)

// Start the server
server.listen(8080, '127.0.0.1', function () {
  console.log('%s listening at %s', server.name, server.url)
})

/*******************************************************************
 * Controllers
 *******************************************************************/

// Get the cover for an album
function getCover (request, response, next) {
  // Get the request from the payload
  var payload = JSON.parse(request.body).payload
  var artist = payload.artist
  var album = payload.album
  var filename = './store/COVER_' + artist + '_' + album

  // Check if we already have that in cache
  fs.readFile(filename, function (err, data) {
    // We already have the file in cache, send it back to the user
    if (data !== undefined) {
      response.send(200, data.toString('utf8'))
      return next()
    }

    // We need to get the data from our engine
    engine.getCover(album, artist, function (err, file) {
      if (err) {
        return errorLogger(err, response, next)
      }

      // Attach the base64 marker
      file = 'data:image/jpeg;base64,' + file

      // We got the cover from our engine, let's save it and return
      fs.writeFile(filename, file, function (err) {
        if (err) {
          return errorLogger(err, response, next)
        }
        console.log('The file "' + filename + '" got saved!')
        response.send(200, file)
        return next()
      })
    })
  })
}

// Get similar tracks
function getSimilarTrack (request, response, next) {
  // Get the request from the payload
  var payload = JSON.parse(request.body).payload
  var title = payload.title

  if (!title) {
    return errorLogger('Title is not defined', response, next)
  }

  // Check if we already have that in cache
  storage.getItem(title, function (err, cache) {
    // We already have the file in cache, send it back to the user
    if (cache !== undefined && title === cache.titel) {
      response.send(200, cache)
      return next()
    }

    // We need to get the data from our engine
    engine.getSimilarTitel(title, function (err, result) {
      if (err) {
        return errorLogger(err, response, next)
      }

      // We got the data from our engine, let's save it and return
      var entry = {title: title, result: result}
      storage.setItem(entry.title, entry, function (err) {
        if (err) {
          return errorLogger(err, response, next)
        }
        response.send(200, entry)
        return next()
      })
    })
  })
}

// Get similar artists
function getSimilarArtist (request, response, next) {
  // Get the request from the payload
  var payload = JSON.parse(request.body).payload
  var artist = payload.artist

  if (!artist) {
    return errorLogger('Artist is not defined', response, next)
  }

  // Check if we already have that in cache
  storage.getItem(artist, function (err, cache) {
    // We already have the file in cache, send it back to the user
    if (cache !== undefined && artist === cache.artist) {
      response.send(200, cache)
      return next()
    }

    // We need to get the data from our engine
    engine.getSimilarArtist(artist, function (err, result) {
      if (err) {
        return errorLogger(err, response, next)
      }

      // We got the data from our engine, let's save it and return
      result = result.map(function (x) { return x.content })
      var entry = {artist: artist, result: result}
      storage.setItem(entry.artist, entry, function (err) {
        if (err) {
          return errorLogger(err, response, next)
        }
        response.send(200, entry)
        return next()
      })
    })
  })
}

// Log an error out to the user
function errorLogger (err, response, next) {
  console.error('Error: ' + err)
  response.status(500)
  response.json({
    type: false,
    data: 'Error occurred: ' + err
  })
  return next()
}

@@ -545,16 +545,78 @@
 arser())
+%0Aserver.use(restify.CORS())%0Aserver.use(restify.fullResponse())
 %0A%0A// Set
@@ -1169,35 +1169,24 @@
 r payload = 
-JSON.parse(
 request.body
@@ -1177,33 +1177,32 @@
 d = request.body
-)
 .payload%0A  var a
@@ -1288,16 +1288,17 @@
 VER_' + 
+(
 artist +
@@ -1299,17 +1299,17 @@
 tist + '
-_
+ 
 ' + albu
@@ -1309,16 +1309,143 @@
  + album
+).replace(/%5B%5E0-9a-zA-Z_%5D/g, '_')%0A  var response_wrapper = %7B%0A    request: %7Bartist: artist, album: album%7D,%0A    response: null%0A  %7D
 %0A%0A  // C
@@ -1640,26 +1640,35 @@
 response
-.send(200,
+_wrapper.response =
  data.to
@@ -1681,16 +1681,58 @@
 ('utf8')
+%0A      response.send(200, response_wrapper
 )%0A      
@@ -2273,16 +2273,57 @@
 aved!')%0A
+        response_wrapper.response = file%0A
         
@@ -2341,20 +2341,32 @@
 nd(200, 
-file
+response_wrapper
 )%0A      
@@ -2530,35 +2530,24 @@
 r payload = 
-JSON.parse(
 request.body
@@ -2538,33 +2538,32 @@
 d = request.body
-)
 .payload%0A  var t
@@ -3592,19 +3592,8 @@
 d = 
-JSON.parse(
 requ
@@ -3600,17 +3600,16 @@
 est.body
-)
 .payload

Fix cover response and make sure CORS is enabled