'use strict';

const exec = require('child_process').exec;
const path = require('path');
const formidable = require('formidable');
const settings = require('../../lib/settings');

class ThemesService {
  constructor() {}

  exportCurrentTheme(req, res) {
    const randomFileName = Math.floor(Math.random() * 10000);
    exec(`npm --silent run theme:export -- ${randomFileName}.zip`, (error, stdout, stderr) => {
      if (error) {
        res.status(500).send(this.getErrorMessage(error));
      } else {
        if (stdout.includes('success')) {
          res.send({'file': `/${randomFileName}.zip`});
        } else {
          res.status(500).send(this.getErrorMessage('Something went wrong in scripts'));
        }
      }
    });
  }

  installTheme(req, res) {
    this.saveThemeFile(req, res, (err, fileName) => {
      if (err) {
        res.status(500).send(this.getErrorMessage(err));
      } else {
        exec(`npm run theme:install ${fileName} && npm run theme:build:prod`, (error, stdout, stderr) => {
          if (error) {
            res.status(500).send(this.getErrorMessage(error));
          } else {
            res.send({'success': true, 'warn': stderr});
          }
        });
      }
    });
  }

  saveThemeFile(req, res, callback) {
    let form = new formidable.IncomingForm(),
      file_name = null,
      file_size = 0;

    form.multiples = false;

    form.on('fileBegin', (name, file) => {
      // Emitted whenever a field / value pair has been received.
      if (file.name.endsWith('.zip')) {
        let dir = path.resolve('public');
        file.path = dir + '/' + file.name;
      }
      // else - will save to /tmp
    }).on('file', function(field, file) {
      // every time a file has been uploaded successfully,
      if (file.name.endsWith('.zip')) {
        file_name = file.name;
        file_size = file.size;
      }
    }).on('error', (err) => {
      callback(err);
    }).on('end', () => {
      //Emitted when the entire request has been received, and all contained files have finished flushing to disk.
      if (file_name) {
        callback(null, file_name);
      } else {
        callback('Cant upload file');
      }
    });

    form.parse(req);
  }

  getErrorMessage(err) {
    return {'error': true, 'message': err.toString()};
  }
}

module.exports = new ThemesService();

@@ -171,16 +171,69 @@
 tings');
+%0Aconst dashboardEvents = require('../../lib/events');
 %0A%0Aclass 
@@ -949,32 +949,64 @@
 %0A      %7D else %7B%0A
+        // run async NPM script%0A
         exec(%60np
@@ -1135,131 +1135,280 @@
     
-res.status(500).send(this.getErrorMessage(error));%0A          %7D else %7B%0A            res.send(%7B'success': true, 'warn':
+dashboardEvents.sendMessage(%7B'type': dashboardEvents.THEME_INSTALLED, 'success': false%7D)%0A          %7D else %7B%0A            dashboardEvents.sendMessage(%7B'type': dashboardEvents.THEME_INSTALLED, 'success': true%7D)%0A            exec('npm run restart', (error, stdout,
  stderr
+) =%3E %7B
 %7D);%0A
@@ -1427,24 +1427,118 @@
         %7D);%0A
+        // close request and don't wait result from NPM script%0A        res.status(200).end();%0A
       %7D%0A    

Send event when theme installed