/**
 * Created by mikedanylov on 10/1/16.
 */
var config = require('./config');

var uri = process.env.MONGOLAB_URI || config.mongoUri;

var mongoose = require('mongoose');

mongoose.connect(uri);

var db = mongoose.connection;

db.on('error', function (err) {
    console.log('Failed to connect to mongodb', err);
});

db.once('open', function (resp) {
    console.log('Connected to mongodb, uri: ' + uri);
});

var eventSchema = mongoose.Schema({
    viewUrl      : String,
    type         : String,
    origX        : Number,
    origY        : Number,
    scaledX      : Number,
    scaledY      : Number,
    selector     : String,
    timestamp    : Date,
    width        : Number,
    height       : Number,
    platform     : String,
    modifications: [String]
});



eventSchema.statics.findEvents = function (queryParams, remove, cb) {
    return this.find({
        type        : queryParams.type,
        platform    : queryParams.platform,
        viewUrl     : queryParams.url,
        $and        : [
            { timestamp: { $gt: startTime } },
            { timestamp: { $lt: endTime } }
        ]
    }, remove).exec(cb);
};

eventSchema.statics.findEventsWithModifications = function (queryParams, remove, cb) {
    this.find({
        type        : queryParams.type,
        platform    : queryParams.platform,
        viewUrl     : queryParams.url,
        $and        : [
            { timestamp: { $gt: startTime } },
            { timestamp: { $lt: endTime } }
        ],
        modifications: {$all: queryParams.modifications}
    }, remove).exec(cb);
};

eventSchema.statics.findEventsWithModificationsExclusive = function (queryParams, remove, cb) {
    this.find({
        type        : queryParams.type,
        platform    : queryParams.platform,
        viewUrl     : queryParams.url,
        $and        : [
            { timestamp: { $gt: queryParams.startTime } },
            { timestamp: { $lt: queryParams.endTime } },
            {modifications: {$all: queryParams.modifications}},
            {modifications: {$size: queryParams.modifications.length}}
        ]
    }, remove).exec(cb);
};

var Event = mongoose.model('Event', eventSchema);

exports.Event = Event;


@@ -1036,32 +1036,44 @@
 mestamp: %7B $gt: 
+queryParams.
 startTime %7D %7D,%0A 
@@ -1095,32 +1095,44 @@
 mestamp: %7B $lt: 
+queryParams.
 endTime %7D %7D%0A    
@@ -1448,16 +1448,28 @@
  %7B $gt: 
+queryParams.
 startTim
@@ -1507,16 +1507,28 @@
  %7B $lt: 
+queryParams.
 endTime 

fix typo