"use strict";

var kue = require('kue'),
    Promise = require('bluebird'),
    gm = require('gm'),
    fs = require('fs'),
    path = require('path'),
    queue = kue.createQueue();

queue.on('job enqueue', function (id, type) {
    console.log('Job %s got queued of type %s', id, type);
}).on('job complete', function (id, result) {
    kue.Job.get(id, function (err, job) {
        if (err) return;
        job.remove(function (err) {
            if (err) throw err;
            console.log('removed completed job #%d', job.id);
        });
    });
});

module.exports.startFrontend = function () {
    kue.app.listen(4444);
};

//
//
// Confirmation mail

queue.process('confirmationMail', function (job, done) {
    if (!job.user) {
        done();
    }
    var domain = require('domain').create();
    domain.on('error', function (err) {
        done(err);
    });
    domain.run(function () { // your process function
        return Promise.resolve(job.user.sendConfirmationMail())
            .then(function () {
                done();
            })
            .catch(function (err) {
                done(err);
            });
    });
});

module.exports.sendConfirmationMail = function (user) {
    return Promise.resolve()
        .then(function () {
            var job = queue.create('confirmationMail', user).attempts(10).backoff({delay: 60 * 1000, type: 'fixed'});
            return Promise.promisify(function (cb) {
                job.save(cb);
            })();
        });
};

//
//
// Password reset

queue.process('resetMail', function (job, done) {
    if (!job.user) {
        done();
    }
    var domain = require('domain').create();
    domain.on('error', function (err) {
        done(err);
    });
    domain.run(function () { // your process function
        return Promise.resolve(job.user.requestPasswordReset())
            .then(function () {
                done();
            })
            .catch(function (err) {
                done(err);
            });
    });
});

module.exports.sendResetMail = function (user) {
    return Promise.resolve()
        .then(function () {
            var job = queue.create('resetMail', user).attempts(10).backoff({delay: 60 * 1000, type: 'fixed'});
            return Promise.promisify(function (cb) {
                job.save(cb);
            })();
        });
};

//
//
// Create thumbnails

queue.process('createThumbs', function (job, done) {
    if (!job.data) {
        done();
    }
    var domain = require('domain').create();
    domain.on('error', function (err) {
        done(err);
    });
    domain.run(function () { // your process function
        return Promise.promisify(function (cb) {
                gm(path.join(__dirname, '..', 'assets', 'photos', job.data.uuid))
                    .resize('200', '200', '^>')
                    .gravity('Center')
                    .crop('200', '200')
                    .write(path.join(__dirname, '..', 'assets', 'photos', job.data.uuid + '-thumb'), function (err) {
                        cb(err);
                    });
            })()
            .catch(function (err) {
                done(err);
            });
    });
});

module.exports.createThumbnails = function (photo) {
    return Promise.resolve()
        .then(function () {
            var job = queue.create('createThumbs', photo);
            return Promise.promisify(function (cb) {
                job.save(cb);
            })();
        });
};
@@ -934,32 +934,16 @@
  return 
-Promise.resolve(
 job.user
@@ -965,17 +965,16 @@
 onMail()
-)
 %0A       
@@ -1776,32 +1776,16 @@
  return 
-Promise.resolve(
 job.user
@@ -1807,17 +1807,16 @@
 dReset()
-)
 %0A       
@@ -2599,65 +2599,8 @@
 ion%0A
-        return Promise.promisify(function (cb) %7B%0A        
     
@@ -2681,24 +2681,16 @@
         
-      
-  
 .resize(
@@ -2721,24 +2721,16 @@
         
-        
 .gravity
@@ -2752,24 +2752,16 @@
         
-  
-      
 .crop('2
@@ -2772,24 +2772,16 @@
  '200')%0A
-        
         
@@ -2886,118 +2886,8 @@
 ) %7B%0A
-                        cb(err);%0A                    %7D);%0A            %7D)()%0A            .catch(function (err) %7B%0A
     

fix workers