// TODO: Make it so this script can be run somewhere, maybe via npm.
/* eslint-disable  no-console */
// Import the neccesary modules.
import asyncq from 'async-q';

import ProviderConfig from '../../models/ProviderConfig';
import Setup from '../../config/Setup';

import horriblesubsanime from './horriblesubsanime.json';
import nyaaanime from './nyaaanime.json';

import etmovies from './etmovies.json';
// import katmovies from './katmovies.json';
import ytsmovies from './ytsmovies.json';

import etshows from './etshows.json';
import eztvshows from './eztvshows.json';
// import katshows from './katshows.json';

Setup.connectMongoDB();

/**
 * NOTE: The order of the json arrays is important. It will determine at what
 * order the objects are inserted. The `insertMany` function inserts the array
 * backwards. So in the case for `animes` the HorribleSubs provider configs are
 * inserted first and the Nyaa provider configs are inserted second.
 */
const animes = [].concat(
  nyaaanime,
  horriblesubsanime
);

const movies = [].concat(
  // katmovies,
  etmovies,
  ytsmovies
);

const shows = [].concat(
  // katshows,
  etshows,
  eztvshows
);

/**
 * NOTE: Again the order of the promises are important. It will determine at
 * what order the objects are inserted. Because we are using the `eachSeries`
 * function the first promise will be resolved first before going to the second
 * promise and etc. This is important because when fetching the provider configs
 * in the `Scraper` class we want it to go in a certain order. The order will be
 * determined by the order of inserting the provider configs. We can do this
 * with: `.sort({ $natural: <order> }).`
 */
asyncq.eachSeries([
  ProviderConfig.insertMany(shows),
  ProviderConfig.insertMany(movies),
  ProviderConfig.insertMany(animes)
], promise => promise).then(res => {
  const length = res.map(r => r.length).reduce((acc, cur) => acc + cur);

  console.log(`Inserted: '${length}' provider configurations.`);
  process.exit(0);
}).catch(err => {
  console.error(`Oops something went wrong: ${err}`);
  process.exit(1);
});

@@ -80,17 +80,16 @@
 disable 
- 
 no-conso
@@ -955,21 +955,24 @@
 /%0Aconst 
-anime
+provider
 s = %5B%5D.c
@@ -984,68 +984,29 @@
 (%0A  
-nyaaanime,%0A  horriblesubsanime%0A);%0A%0Aconst movies = %5B%5D.concat(
+eztvshows,%0A  etshows,
 %0A  /
@@ -1014,20 +1014,20 @@
  kat
-movie
+show
 s,%0A  
-et
+yts
 movi
@@ -1036,84 +1036,66 @@
 ,%0A  
-yts
+et
 movies
-%0A);%0A%0Aconst shows = %5B%5D.concat(%0A  // katshows,%0A  etshows,%0A  eztvshows
+,%0A  // katmovies,%0A  horriblesubsanime,%0A  nyaaanime
 %0A);%0A
@@ -1640,230 +1640,87 @@
 ies(
-%5B%0A  P
+p
 rovider
-Config.insertMany(shows),%0A  ProviderConfig.insertMany(movies),%0A  ProviderConfig.insertMany(animes)%0A%5D, promise =%3E promise).then(res =%3E %7B%0A  const length = res.map(r =%3E r.length).reduce((acc, cur) =%3E acc + cur);%0A%0A
+s, provider =%3E new ProviderConfig(provider).save())%0A  .then(res =%3E %7B%0A  
   co
@@ -1743,16 +1743,20 @@
 ted: '$%7B
+res.
 length%7D'
@@ -1782,24 +1782,26 @@
 tions.%60);%0A  
+ 
+ 
 process.exit
@@ -1805,18 +1805,23 @@
 xit(0);%0A
-%7D)
+  %7D)%0A  
 .catch(e
@@ -1828,16 +1828,18 @@
 rr =%3E %7B%0A
+  
   consol
@@ -1884,16 +1884,18 @@
 err%7D%60);%0A
+  
   proces
@@ -1905,12 +1905,14 @@
 xit(1);%0A
+  
 %7D);%0A

put configs in one array