import tmp from 'tmp';
import chai from 'chai';
import fs from 'fs';
import path from 'path';
import nativefier from './../../lib/index';
import _ from 'lodash';
import async from 'async';

let assert = chai.assert;

const PLATFORMS = ['darwin', 'linux', 'win32'];

function checkApp(appPath, inputOptions, callback) {
    try {
        let relPathToConfig;

        switch (inputOptions.platform) {
            case 'darwin':
                relPathToConfig = path.join('google-test-app.app', 'Contents/Resources/app');
                break;
            case 'linux':
                relPathToConfig = 'resources/app';
                break;
            case 'win32':
                relPathToConfig = 'resources/app';
                break;
            default:
                throw 'Unknown app platform';
        }

        const nativefierConfigPath = path.join(appPath, relPathToConfig, 'nativefier.json');
        const nativefierConfig = JSON.parse(fs.readFileSync(nativefierConfigPath));

        assert.strictEqual(inputOptions.targetUrl, nativefierConfig.targetUrl, 'Packaged app must have the same targetUrl as the input parameters');
        // app name is not consistent for linux
        // assert.strictEqual(inputOptions.appName, nativefierConfig.name, 'Packaged app must have the same name as the input parameters');
        callback();
    } catch (exception) {
        callback(exception);
    }
}

describe('Nativefier Module', function() {
    this.timeout(30000);
    it('Can build an app from a target url', function(done) {

        var tmpObj = tmp.dirSync({unsafeCleanup: true});
        after(function() {
            tmpObj.removeCallback();
        });

        const tmpPath = tmpObj.name;
        const options = {
            appName: 'google-test-app',
            targetUrl: 'http://google.com',
            outDir: tmpPath,
            overwrite: true,
            platform: null
        };

        async.each(PLATFORMS, (platform, callback) => {
            let platformOptions = _.clone(options);
            platformOptions.platform = platform;
            nativefier(platformOptions, (error, appPath) => {
                if (error) {
                    callback(error);
                    return;
                }

                checkApp(appPath, platformOptions, error => {
                    callback(error);
                });
            });
        }, error => {
            done(error);
        });
    });
});

@@ -181,16 +181,42 @@
 'async';
+%0Atmp.setGracefulCleanup();
 %0A%0Alet as
@@ -1570,25 +1570,24 @@
 done) %7B%0A
-%0A
         
 var tmpO
@@ -1582,134 +1582,131 @@
     
-var tmpObj = tmp.dirSync(%7BunsafeCleanup: true%7D);%0A        after(function() %7B%0A            tmpObj.removeCallback();%0A        %7D);%0A%0A
+async.eachSeries(PLATFORMS, (platform, callback) =%3E %7B%0A%0A            const tmpObj = tmp.dirSync(%7BunsafeCleanup: true%7D);%0A%0A    
     
@@ -1742,24 +1742,28 @@
 me;%0A        
+ 
+   
 const option
@@ -1768,16 +1768,20 @@
 ons = %7B%0A
+    
         
@@ -1824,16 +1824,20 @@
         
+   
+ 
 targetUr
@@ -1872,16 +1872,20 @@
         
+    
 outDir: 
@@ -1893,16 +1893,20 @@
 mpPath,%0A
+    
         
@@ -1942,142 +1942,42 @@
     
-platform: null%0A        %7D;%0A%0A        async.each(PLATFORMS, (platform, callback) =%3E %7B%0A            let platformOptions = _.clone(options);
+    platform: null%0A            %7D;%0A
 %0A   
@@ -1985,25 +1985,17 @@
         
-platformO
+o
 ptions.p
@@ -2037,25 +2037,17 @@
 ivefier(
-platformO
+o
 ptions, 
@@ -2219,17 +2219,9 @@
 th, 
-platformO
+o
 ptio
@@ -2375,12 +2375,13 @@
     %7D);%0A%7D);%0A
+%0A

Fix problem with async each and creation of temporary directories