'use strict';

const MsgQueueClient = require('msgqueue-client');
const GoogleScrape = require('./google-img-scrape-cse.js');
const s3 = require('./s3.js');
const fb = require('./firebase.js');

const mqServerConfig = require('../common/config/mqserver.js');
const CSE_secrets = require('../common/secrets/scrape-img.js').CSE;

const mq = new MsgQueueClient(`${mqServerConfig.url}:${mqServerConfig.port}`, { log: true });
const gClient = new GoogleScrape(CSE_secrets.CSE_ID, CSE_secrets.API_KEY);

mq.on('connected', () => { console.log('connected to mq'); });

mq.listen('srvc_img_scrape_req', (ack, reject, payload) => {
    // fire off query via search clients
  getWebImgObjSet(payload.query, payload.num)
  .then(webImgObjSet => {
    return Promise.all(
      flattenArray(webImgObjSet).map(webImgObj => {
        console.log(`got back webImgObjSet from ${payload.query}, ${webImgObj.length} of ${payload.num}`);
        // console.log(webImgObj);
        return uploadWebImgObjToS3(webImgObj)
            // TODO save img obj to db
          .then(s3ImgObj => {
            console.log('got back s3ImgObj');
            ack();
            syncToFirebase(s3ImgObj);
          })
          .then(fbImgRef => {
            console.log('got back fbImgRef');
            console.log(fbImgRef);
          });
      })
    );
  })
  .then(allImgRefs => {
    // TODO enqueue img refs to db
    let queue = 'ctrl_img_scrape_res';
    mq.enqueue(queue, imgObjRefs) // TODO add payload (firebase ref to obj that contains img ref to S3 img)
    .then(() => {
      console.log('service: srvc_img_scrape_req --> ctrl_img_scrape_res')
      ack();
    });
  })
});

function getWebImgObjSet (query, num) {
  return gClient.search(query, num);
}

function uploadWebImgObjToS3 (imgObj) {
  return s3.pipeTransformAndUploadImgObjToS3(imgObj);
}

function syncToFirebase (imgObj) {
  return fb.pushAndAddUID(imgObj);
}

function flattenArray(arrTwoDim) {
  return [].concat.apply([], arrTwoDim);
}

@@ -55,24 +55,62 @@
 e-client');%0A
+const Firebase = require('firebase');%0A
 const Google
@@ -597,18 +597,17 @@
 );%0A%0A
-mq.listen(
+let lQ = 
 'srv
@@ -623,16 +623,30 @@
 ape_req'
+;%0Amq.listen(lQ
 , (ack, 
@@ -663,26 +663,24 @@
 yload) =%3E %7B%0A
-  
   // fire of
@@ -706,16 +706,39 @@
 clients%0A
+  Firebase.goOnline();%0A
   getWeb
@@ -862,16 +862,17 @@
 et).map(
+(
 webImgOb
@@ -868,24 +868,28 @@
 p((webImgObj
+, i)
  =%3E %7B%0A      
@@ -921,19 +921,16 @@
 ebImgObj
-Set
  from $%7B
@@ -951,24 +951,9 @@
 , $%7B
-webImgObj.length
+i
 %7D of
@@ -998,32 +998,73 @@
 log(webImgObj);%0A
+        webImgObj.query = payload.query;%0A
         return u
@@ -1216,28 +1216,44 @@
             
-ack(
+console.log(s3ImgObj
 );%0A         
@@ -1254,16 +1254,30 @@
         
+ if (s3ImgObj)
  syncToF
@@ -1286,32 +1286,87 @@
 ebase(s3ImgObj);
+ // TODO reconsider this hotfix to exclude failing urls
 %0A          %7D)%0A  
@@ -1422,61 +1422,36 @@
 og('
-got back fbImgRef');%0A            console.log(fbImgRef
+synced s3ImgObj to firebase'
 );%0A 
@@ -1516,38 +1516,101 @@
     
-// TODO enqueue img refs to db
+console.log('synced', allImgRefs.length, 'to Firebase');%0A    ack();%0A    Firebase.goOffline();
 %0A   
@@ -1614,21 +1614,18 @@
     let 
-queue
+nQ
  = 'ctrl
@@ -1661,25 +1661,19 @@
 eue(
-queue, imgObjRefs
+nQ, payload
 ) //
@@ -1780,17 +1780,17 @@
 ole.log(
-'
+%60
 service:
@@ -1794,64 +1794,24 @@
 ce: 
-srvc_img_scrape_req --%3E ctrl_img_scrape_res')%0A      ack(
+$%7BlQ%7D --%3E $%7BnQ%7D%60
 );%0A 

Fix full service chain, refactor for new mq queue naming convention