'use strict'

const pmpProcessConfig      = require('./configs').pmpProcessConfig;
const pmpEngineStatusEvts   = require('./configs').pmpEngineStatusEvts;
const pmpEvts               = require('./configs').pmpGulpEvts;
const PmpEvt                = require('./configs').PmpGulpEvt;
const isJsonString = function (str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}

let starter = function(spawnWatch, pmpConfig, statusStream, additionalArguments) {
    /*************************************************************************
     *  four steps process:
     *  1 - first start child process itself
     *  2 - wait for gulp pimpCommands config prompt
     *  3 - pass gulp pimpCommands config to child process
     *  4 - receive pmpEngine started notification
     * ********************************************************************* */

    //STEP 1
    let pEvtSub = spawnWatch.processEventStream.subscribe((evt)=>{
        console.log(evt)
        //react to process evts
        if(evt === 'pending stop' || evt === 'pending start') statusStream.onNext(pmpEngineStatusEvts.pending);
        if(evt === 'stopped') {
            statusStream.onNext(pmpEngineStatusEvts.stopped);
            pEvtSub.dispose();
        }
        //do not react to started evt because this is handled below (at the hand of step 4)
    });
    let ipsEvtSub = spawnWatch.ipcStream.subscribe(IPCmsg => {
        console.log(IPCmsg)
        switch(IPCmsg.type){ 
            //STEP 2                   
            case pmpEvts.init:
                //STEP 3 - send config to process
                spawnWatch.ipcInput(new PmpEvt(pmpEvts.configSend, pmpConfig));
            break;

            //STEP 4
            case pmpEvts.started:
                statusStream.onNext(pmpEngineStatusEvts.started);
                ipsEvtSub.dispose();
            break;
        };
    });

    //setup process config with optional arguments
    let processConfig = Object.assign({}, pmpProcessConfig);
    if(additionalArguments) processConfig.args = processConfig.args.concat(additionalArguments);

    return spawnWatch.start(processConfig);
}

module.exports = {
    starter: starter
}
@@ -915,18 +915,16 @@
 *** */%0D%0A
-%0D%0A
     //ST
@@ -933,374 +933,615 @@
  1%0D%0A
+%0D%0A
     
-let pEvtSub = spawnWatch.processEventStream.subscribe((evt)=%3E%7B%0D%0A        console.log(evt)%0D%0A        //react to process evts%0D%0A        if(evt === 'pending stop' %7C%7C evt === 'pending start') statusStream.onNext(pmpEngineStatusEvts.pending);%0D%0A        if(evt === 'stopped') %7B%0D%0A            statusStream.onNext(pmpEngineStatusEvts.stopped);%0D%0A            pEvtSub.dispose();
+//status update - pending start / pending stop / stopped (started update is done at step 4)%0D%0A    //all updates can happen once%0D%0A    spawnWatch.processEventStream%0D%0A        .filter(status =%3E (status === 'pending start'))%0D%0A        .first()%0D%0A        .subscribe((evt)=%3E%7B statusStream.onNext(pmpEngineStatusEvts.pending); %7D);%0D%0A%0D%0A    spawnWatch.processEventStream%0D%0A        .filter(status =%3E (status === 'pending stop'))%0D%0A        .first()%0D%0A        .subscribe((evt)=%3E%7B statusStream.onNext(pmpEngineStatusEvts.pending); %7D);%0D%0A%0D%0A    spawnWatch.processEventStream%0D%0A        .filter(status =%3E (status === 'stopped'))
 %0D%0A  
@@ -1542,25 +1542,32 @@
 ))%0D%0A        
-%7D
+.first()
 %0D%0A        //
@@ -1568,102 +1568,85 @@
     
-//do not react to started evt because this is handled below (at the hand of step 4)%0D%0A   
+.subscribe((evt)=%3E%7B statusStream.onNext(pmpEngineStatusEvts.stopped);
  %7D);%0D%0A
+%0D%0A
     
@@ -1709,37 +1709,8 @@
  %7B%0D%0A
-        console.log(IPCmsg)%0D%0A
     
@@ -1959,24 +1959,24 @@
   break;%0D%0A%0D%0A
-
             
@@ -1983,16 +1983,83 @@
 //STEP 4
+ (! ipc started event means pmp-gulp, not the child process itself)
 %0D%0A      

fix multiple calls to start when restarting instance