"use strict";
const mongoose = require('mongoose')
const logger = require('../logger')
const fs = require('fs')
const path  = require('path')
const websockets = require('../websockets')()

/**
 * @swagger
 * definitions:
 *   Media:
 *     properties:
 *       label:
 *         type: string
 *         description: Media's label
 *       uri:
 *         type: string
 *         description: Path to the media file
 *         required: true
 *       mimeType:
 *         type: string
 *         description: mime type of the file
 *       position:
 *         type: integer
 *         format: int32
 *         minimum: 1
 *         description: current position inside the parent's list
 *       started:
 *         type: date
 *         description: time and date when the Media has started
 *       ended:
 *         type: date
 *         description: time and date when the Media has ended
 */
let mediaSchema = new mongoose.Schema({
  label: { type: String },
  uri: { type: String, required: true},
  mimeType: { type: String, required: true},
  path: { type: String }, // in the case of a local file
  position: { type: Number },
  started: { type: Date },
  ended: { type: Date },
  created: { type: Date, required: true },
  modified: { type: Date, required: true },
  topic: { type: mongoose.Schema.Types.ObjectId, ref:'Topic', required: true },
})

let mediaModel = mongoose.model('Media', mediaSchema)

mediaSchema.methods.toJSON = function() {
 var obj = this.toObject()
 delete obj.path // remove/hide the local path
 return obj
}

// when a Media is removed, delete its file
mediaSchema.post('remove', function(media) {
  logger.debug("Removed Media " + media._id)
  websockets.sockets.emit('medias.' + media._id + '.delete', media)

  if (fs.existsSync(path.join(__base, media.path))) {
    fs.unlinkSync(path.join(__base, media.path))
  }
})

mediaSchema.pre('save', function(next) {
  this.wasNew = this.isNew

  // stop all others medias if this one starts playing
  if (this.isModified('started') && this.started && !this.ended) {
    mediaModel
      .find({ ended : null })
      .where('_id').ne(this._id)
      .where('started').ne(null)
      .then(function(results) {
        results.forEach(function (media) {
          media.ended = Date.now()
          media.save()
        })
        next()
      })
      .catch(function(error) {
        logger.error(error)
      })
  } else {
    next()
  }
})

mediaSchema.post('save', function(media) {
  if (this.wasNew) {
    websockets.sockets.emit('medias.add', media)
  }
  websockets.sockets.emit('medias.' + media._id, media)
})

module.exports = mediaModel
@@ -1138,16 +1138,51 @@
 l file%0D%0A
+  // thumbnail: %7B type: String %7D,%0D%0A
   positi
@@ -1204,16 +1204,16 @@
 ber %7D,%0D%0A
-
   starte
@@ -1842,16 +1842,30 @@
 %0D%0A  if (
+media.path && 
 fs.exist
@@ -2742,16 +2742,834 @@
 %0D%0A%7D)%0D%0A%0D%0A
+mediaSchema.virtual('thumbnail').get(function() %7B%0D%0A  if (this.mimeType.includes('html')) %7B%0D%0A    // https://stackoverflow.com/a/2068371%0D%0A    if (this.uri.startsWith('https://youtube') %7C%7C this.uri.startsWith('https://www.youtube')) %7B%0D%0A      const %5Buri, queries%5D = this.uri.split('?')%0D%0A      if (/v=(%5B%5E&%5D*)/.test(queries)) %7B%0D%0A        // https://www.youtube.com/watch?v=XXXXXXXXX%0D%0A        return %60https://img.youtube.com/vi/$%7Bqueries.match(/v=(%5B%5E&%5D*)/).pop()%7D/default.jpg%60%0D%0A      %7D else %7B%0D%0A        // https://www.youtube.com/embed/XXXXXXXXX?autoplay=true%0D%0A        return %60https://img.youtube.com/vi/$%7Buri.match(/(%5B%5E/%5D*)%5C/*$/).pop()%7D/default.jpg%60%0D%0A      %7D%0D%0A    %7D%0D%0A    return null%0D%0A  %7D%0D%0A  return %60$%7Bthis.uri%7D?height=256%60%0D%0A%7D)%0D%0AmediaSchema.set('toObject', %7B virtuals: true %7D)%0D%0AmediaSchema.set('toJSON', %7B virtuals: true %7D)%0D%0A%0D%0A
 module.e

Add thumbnail property to Media