import fse from 'fs-extra';
import path from 'path';
import glob from 'glob';
import R from 'ramda';
import Promise from 'bluebird';

const fs = Promise.promisifyAll(require('fs'));
const fseCopy = Promise.promisify(fse.copy);
const PromisifiedGlob = Promise.promisify(glob);


function copySingleFile(src, dest, newFileName) {
  const promise = fseCopy(src, path.join(dest, newFileName));
  return promise;
}

// will need refactoring - good enough for POC
// ain't not much consistency in file naming tho, gotta have to make it work
// with:
//  - x = reference
   //  - y = number from 0 to 9
//  - Q = quality (caps) from A to G
         //  - q = quality (min) from a to g
//
//  match:
//  - xq.jpg          [xa.jpg ... xg.jpg]
//  - x.y.jpg         [x.1.jpg ... x.9.jpg]
//  - x.q.jpg         [x.a.jpg ... x.g.jpg]
//  - xQ.y.jpg        [xA.1.jpg ... xA.9.jpg ... xG.1.jpg ... xG.9.jpg]
//  - xQ.q.jpg        [xA.a.jpg ... xA.g.jpg ... xG.a.jpg ... xG.g.jpg]
//  - xBis.y.jpg      [xBis.1.jpg ... xBis.9.jpg]
//  - xBis.q.jpg      [xBis.a.jpg ... xBis.g.jpg]
export function copyFiles(src, dest, files, cb) {

  cb(`Starting copy process`);
  cb(`Starting async copy process for ${files.length} file(s): ${files}`);

  const promise = new Promise((resolve, reject) => {
    let counter = 0;

    Promise.each(files, file => {

      const wildcard = path.join(src, '**', `${file}?(?|?(|Bis|A|B|C|D|E|G).??(|.?)).+(jpg|jpeg|JPG|JPEG)`);

      return new PromisifiedGlob(wildcard, {})
        .then(matchingFiles => {

          return Promise
            .filter(matchingFiles, matchingFile => !R.test(new RegExp(dest), matchingFile)) // don't copy from subfolder
            .then(matchingFiles => {
              cb(`Start processing input file ${file}`);
              cb(`Found ${matchingFiles.length} matching file(s) : ${matchingFiles}`);
              return matchingFiles;
            })
            .filter(matchingFile => {
              // remove files with the term 'copy' in the name
              if (/copy/i.test(matchingFile)) {
                cb(`Decided not to process following file: ${matchingFile}`);
                return false;
              } else {
                return true;
              }


            })
            .each(fileToCopy => {
              const fileName = R.last(fileToCopy.split('/'));

              return copySingleFile(fileToCopy, dest, fileName)
                .then(data => {
                  counter++;
                  cb('Done copying ' + fileName)
                })
                .catch(err => cb('ERROR copying ' + fileName))
            })
            .then(data => cb(`Done processing input file ${file}`))
            .catch(err => {
              debugger;
              console.error(err)
              cb(`Error processing input file ${file} - ${err}`)
            })
          });
    })
    .then(data => {
      cb(`A total of ${counter} file(s) have been copied`);
      resolve();
    })
    .catch(err => {
      cb(`Copy process ERROR: ${err}`);
      reject(err);
    });
  })
  .then(data => cb('Copy process terminated succesfully'))
  .catch(err => cb('Copy process terminated with ERROR'))
  .finally(() => cb('Shutting system down\n\n'));

  return promise;
}

@@ -270,17 +270,16 @@
 glob);%0A%0A
-%0A
 function
@@ -1884,350 +1884,8 @@
 es;%0A
-            %7D)%0A            .filter(matchingFile =%3E %7B%0A              // remove files with the term 'copy' in the name%0A              if (/copy/i.test(matchingFile)) %7B%0A                cb(%60Decided not to process following file: $%7BmatchingFile%7D%60);%0A                return false;%0A              %7D else %7B%0A                return true;%0A              %7D%0A%0A%0A
     

remove now obsolete file filter