// Copyright 2017-2018, University of Colorado Boulder

/* eslint-env node */
'use strict';


const constants = require( './constants' );
const devSsh = require( '../common/devSsh' );
const scp = require( 'scp' );
const walk = require( 'walk' );
const winston = require( 'winston' );

const user = constants.BUILD_SERVER_CONFIG.devUsername;
const host = constants.BUILD_SERVER_CONFIG.devDeployServer;

/**
 * Performs a recursive copy to the dev server
 * @param {String} buildDir - filepath of build directory
 * @param {String} simVersionDirectory - target filepath
 * @param {function} shouldFilter - shouldFilter( file:string ) : {boolean}
 * @returns {Promise<any>}
 */
async function scpAll( buildDir, simVersionDirectory, shouldFilter ) {
  return new Promise( ( resolve, reject ) => {
    walk.walk( buildDir ).on( 'file', ( root, fileStats, next ) => {
      const path = simVersionDirectory + root.replace( buildDir, '' );
      const file = root + '/' + fileStats.name;

      if ( !shouldFilter( file ) ) {
        winston.debug( 'Sending file "' + file + '" to dev server path "' + path + '"' );
        scp.send( { file, path, user, host }, err => {
          if ( err ) { reject( err ); }
          else { next(); }
        } );
      }
      else {
        next();
      }
    } ).on( 'errors', ( root, nodeStatsArray ) => {
      nodeStatsArray.forEach( nodeStats => {
        winston.error( JSON.stringify( nodeStats ) );
      } );
      reject( new Error( 'error during dev deploy walk' ) );
    } ).on( 'end', () => {
      resolve();
    } );
  } );
}

/**
 * Copy files to dev server, typically spot.colorado.edu.
 *
 * @param simDir
 * @param simName
 * @param version
 * @param chipperVersion
 * @param brands
 *
 * @return Promise
 */
module.exports = async function( simDir, simName, version, chipperVersion, brands ) {
  const simVersionDirectory = constants.BUILD_SERVER_CONFIG.devDeployPath + simName + '/' + version;

  try {
    // mkdir first in case it doesn't exist already
    await devSsh( 'mkdir -p ' + simVersionDirectory );
    const buildDir = simDir + '/build';

    // copy the files
    let shouldFilter = ( file ) => { return false; };
    if ( brands.includes( constants.PHET_BRAND ) ) {
      shouldFilter = ( file ) => {
        // Do not filter file if it contains if it contains '_en' or '_all' unless it is the canadian translation.
        if ( file.includes( '_en' ) || file.includes( '_all' ) ) {
          return file.includes( '_en_CA' );
        }
        else {
          // Second check for phet brand for chipper 2.0
          if ( chipperVersion.major === 2 && chipperVersion.minor === 0 ) {
            return file.includes( '/phet/' ) && file.includes( '.html' );
          }
          else if ( chipperVersion.major === 0 && chipperVersion.minor === 0 ) {
            return file.includes( '.html' );
          }
        }
      };
    }
    await scpAll( buildDir, simVersionDirectory, shouldFilter );
  }
  catch
    ( err ) {
    return Promise.reject( err );
  }

  return await devSsh( 'chmod -R g+w ' + simVersionDirectory );
}
;
@@ -580,16 +580,39 @@
 function
+(file:string):%7Bboolean%7D
 %7D should
@@ -636,35 +636,8 @@
 lter
-( file:string ) : %7Bboolean%7D
 %0A * 
@@ -735,16 +735,42 @@
 ter ) %7B%0A
+  if ( shouldFilter ) %7B%0A  
   return
@@ -804,24 +804,26 @@
 ject ) =%3E %7B%0A
+  
     walk.wal
@@ -875,24 +875,26 @@
 next ) =%3E %7B%0A
+  
       const 
@@ -954,24 +954,26 @@
 '' );%0A      
+  
 const file =
@@ -1005,24 +1005,26 @@
 ame;%0A%0A      
+  
 if ( !should
@@ -1042,32 +1042,34 @@
 e ) ) %7B%0A        
+ 
+ 
 winston.debug( '
@@ -1142,16 +1142,18 @@
         
+  
 scp.send
@@ -1197,24 +1197,26 @@
 %7B%0A          
+ 
+ 
 if ( err ) %7B
@@ -1239,24 +1239,26 @@
 %7D%0A          
+  
 else %7B next(
@@ -1258,24 +1258,26 @@
 %7B next(); %7D%0A
+  
         %7D );
@@ -1279,26 +1279,30 @@
  %7D );%0A      
-%7D%0A
+  %7D%0A  
       else %7B
@@ -1310,16 +1310,18 @@
         
+ 
+ 
 next();%0A
@@ -1322,26 +1322,30 @@
 xt();%0A      
-%7D%0A
+  %7D%0A  
     %7D ).on( 
@@ -1390,16 +1390,18 @@
 %7B%0A      
+  
 nodeStat
@@ -1427,24 +1427,26 @@
 eStats =%3E %7B%0A
+  
         wins
@@ -1489,29 +1489,33 @@
  ) );%0A      
+ 
+ 
 %7D );%0A
+  
       reject
@@ -1563,24 +1563,26 @@
 k' ) );%0A    
+  
 %7D ).on( 'end
@@ -1598,16 +1598,18 @@
 %7B%0A      
+  
 resolve(
@@ -1607,24 +1607,267 @@
  resolve();%0A
+      %7D );%0A    %7D );%0A  %7D%0A  else %7B%0A    return new Promise( ( resolve, reject ) =%3E %7B%0A      scp.send( %7B file: buildDir, path: simVersionDirectory, user, host %7D, err =%3E %7B%0A        if ( err ) %7B reject( err ); %7D%0A        else %7B resolve(); %7D%0A      %7D );%0A
     %7D );%0A  %7D
@@ -1866,19 +1866,16 @@
 %7D );%0A  %7D
- );
 %0A%7D%0A%0A/**%0A
@@ -2449,37 +2449,12 @@
 r = 
-( file ) =%3E %7B return false; %7D
+null
 ;%0A  

Switch to copying dir if no filter provided