const Promise = require('bluebird');
const fsort = require('redis-filtered-sort');
const is = require('is');
const { FILES_DATA, FILES_INDEX, FILES_INDEX_PUBLIC } = require('../constant.js');

/**
 * List files
 * @return {Promise}
 */
module.exports = function postProcessFile(opts) {
  const { redis } = this;
  const { owner, filter, public: isPublic, offset, limit, order, criteria } = opts;
  const strFilter = is.string(filter) ? filter : fsort.filter(filter || {});

  // choose which set to use
  let filesIndex;
  if (isPublic && owner) {
    filesIndex = `${FILES_INDEX}:${owner}:pub`;
  } else if (owner) {
    filesIndex = `${FILES_INDEX}:${owner}`;
  } else if (isPublic) {
    filesIndex = FILES_INDEX_PUBLIC;
  } else {
    filesIndex = FILES_INDEX;
  }

  return redis
    .fsort(filesIndex, `${FILES_DATA}:*`, criteria, order, strFilter, offset, limit)
    .then(filenames => {
      const length = +filenames.pop();
      if (length === 0 || filenames.length === 0) {
        return [
          filenames || [],
          [],
          length,
        ];
      }

      const pipeline = redis.pipeline();
      filenames.forEach(filename => {
        pipeline.hgetall(`${FILES_DATA}:${filename}`);
      });

      return Promise.join(
        filenames,
        pipeline.exec(),
        length
      );
    })
    .spread((filenames, props, length) => {
      const files = filenames.map(function remapData(filename, idx) {
        const meta = props[idx][1];

        return {
          ...meta,
          id: filename,
          files: JSON.parse(meta.files),
        };
      });

      return {
        files,
        cursor: offset + limit,
        page: Math.floor(offset / limit + 1),
        pages: Math.ceil(length / limit),
      };
    });
};

@@ -467,16 +467,216 @@
 %7C %7B%7D);%0A%0A
+  return Promise%0A    .bind(this)%0A    .then(() =%3E %7B%0A      if (!owner) %7B%0A        return %5B%5D;%0A      %7D%0A%0A      return this.postHook.call(this, 'files:info:pre', owner);%0A    %7D)%0A    .spread(username =%3E %7B%0A    
   // cho
@@ -696,16 +696,20 @@
  to use%0A
+    
   let fi
@@ -718,16 +718,20 @@
 sIndex;%0A
+    
   if (is
@@ -740,29 +740,32 @@
 blic && 
-owner
+username
 ) %7B%0A    
 filesInd
@@ -748,32 +748,36 @@
 username) %7B%0A    
+   
+ 
 filesIndex = %60$%7B
@@ -795,21 +795,24 @@
 %7D:$%7B
-owner
+username
 %7D:pub%60;%0A
   %7D 
@@ -807,16 +807,20 @@
 %7D:pub%60;%0A
+    
   %7D else
@@ -824,25 +824,32 @@
 lse if (
-owner) %7B%0A
+username) %7B%0A    
     file
@@ -879,17 +879,24 @@
 %7D:$%7B
-owner%7D%60;%0A
+username%7D%60;%0A    
   %7D 
@@ -912,24 +912,28 @@
 isPublic) %7B%0A
+    
     filesInd
@@ -957,16 +957,20 @@
 PUBLIC;%0A
+    
   %7D else
@@ -972,16 +972,20 @@
  else %7B%0A
+    
     file
@@ -1008,19 +1008,27 @@
 NDEX;%0A  
-%7D%0A%0A
+    %7D%0A%0A    
   return
@@ -1033,21 +1033,16 @@
 rn redis
-%0A    
 .fsort(f
@@ -1113,16 +1113,24 @@
 , limit)
+;%0A    %7D)
 %0A    .th
@@ -1253,17 +1253,17 @@
  return 
-%5B
+%7B
 %0A       
@@ -1278,14 +1278,8 @@
 ames
- %7C%7C %5B%5D
 ,%0A  
@@ -1285,16 +1285,23 @@
         
+ props:
  %5B%5D,%0A   
@@ -1323,17 +1323,17 @@
         
-%5D
+%7D
 ;%0A      
@@ -1505,21 +1505,15 @@
 ise.
-join(%0A       
+props(%7B
  fil
@@ -1519,24 +1519,23 @@
 lenames,
-%0A       
+ props:
  pipelin
@@ -1547,53 +1547,61 @@
 c(),
-%0A        length%0A      );%0A    %7D)%0A    .spread((
+ length %7D);%0A    %7D)%0A    .then(data =%3E %7B%0A      const %7B 
 file
@@ -1620,22 +1620,26 @@
 , length
-) =%3E %7B
+ %7D = data;
 %0A      c

allow searching with owner set to alias