'use strict';

var tap = require('tap');
var pz = require('promzard');
var spawn = require('child_process').spawn;
var utils = require('./util/utils');
var path = require('path');
var helper = require('./util/helpers');

var node = process.execPath;
var output = '';
function respond(c, lastOutput, custom) {
    custom = custom || function () {
        c.stdin.write('\n');
    };
    var regex = new RegExp(lastOutput + ' $');
    var testOut = [
        'Name: ',
        'Package: (com.company.app) '
    ];
    if (output.match(regex)) {
        c.stdin.write('myIndiTemplate\n');
        c.stdin.end();
        return;
    } else if (output === testOut[0]) {
        c.stdin.write('name\n');
        return;
    } else if (output === testOut[0] + testOut[1]) {
        c.stdin.write('de.mway.test\n');
        return;
    }

    custom(c, output);
    return;
}

function spawnProcess(t, process, expect, lastAttributeOutput, custom) {
    var c = spawn(node, [process], { customFds: [-1, -1, -1] });
    output = '';
    c.stdout.on('data', function (d) {
        output += d;
        respond(c, lastAttributeOutput, custom);
    });

    var actual = '';
    c.stderr.on('data', function (d) {
        actual += d;
    });

    c.on('close', function () {
//        console.error('actual', actual);
//        console.error('expect', expect);
        actual = JSON.parse(actual);
        t.deepEqual(actual, expect);
        t.end();
    });
}

tap.test('new app', function (t) {

    var process = require.resolve('./process/new.js');

    var expect = {
        'templateValues': {
            'name': 'name',
            'package': 'de.mway.test'
        },
        'template': 'myIndiTemplate'
    };

    var lastAttributeOutput = 'Template: \\(helloworld\\)';

    //console.error('%s %s', node, process);
    spawnProcess(t, process, expect, lastAttributeOutput);
});

tap.test('run mcap new without any arguments', function(t) {

    utils.executeCommand('../../cli.js', ['new'], function(output) {
        //console.error(t.deepEqual);
        t.equal(output.toString(), 'Name: ', 'first output should be Name:');
        t.end();
    });

});

tap.test('Create new app named testName', function(t) {

    utils.executeCommand('../../cli.js', ['new', 'testName'], function(output) {
        t.equal(output.toString(), '\u001b[32minfo\u001b[39m: [command/new.js] Done, without errors.\n', 'first output should be Name:');
        t.end();
    });

});

tap.test('WTF', function(t) {
    // Not sure why i need to do this?!
    // Without exiting manually, the previous test would never fail
    // no matter what i check
    process.exit(0);

});

@@ -183,16 +183,17 @@
 r helper
+s
  = requi
@@ -214,16 +214,16 @@
 pers');%0A
-
 %0Avar nod
@@ -784,18 +784,18 @@
 .write('
-de
+io
 .mway.te
@@ -1637,18 +1637,18 @@
 kage': '
-de
+io
 .mway.te
@@ -1988,16 +1988,23 @@
 %5B'new'%5D,
+ false,
  functio
@@ -2280,16 +2280,22 @@
 tName'%5D,
+ true,
  functio
@@ -2467,32 +2467,674 @@
 ;%0A    %7D);%0A%0A%7D);%0A%0A
+tap.test('Create new app named testName2', function(t) %7B%0A%0A    var expectedFiles = %5B%0A        'testName2/mcap.json',%0A        'testName2/server'%0A    %5D;%0A%0A    var expectedContent = %5B%0A        %5B'testName2/mcap.json', /(%5C%22name%5C%22%5C: %5C%22testName2%5C%22)/%5D%0A    %5D;%0A%0A%0A%0A    utils.executeCommand('../../cli.js', %5B'new', 'testName2'%5D, true, function(output, tmpPath) %7B%0A        t.equal(output.toString(), '%5Cu001b%5B32minfo%5Cu001b%5B39m: %5Bcommand/new.js%5D Done, without errors.%5Cn', 'first output should be Name:');%0A%0A        helpers.assertFile(expectedFiles);%0A        helpers.assertFileContent(expectedContent);%0A        utils.removeTmpDir();%0A        t.end();%0A    %7D);%0A%0A%7D);%0A%0A
 tap.test('WTF', 

add test helper