import Queue from 'bull';
import Arena from 'bull-arena';
import logger from './logger';
import { core as config } from './conf';

const host = config.get('redis.host');
const port = config.get('redis.port');
const password = config.get('redis.password');

export default {
  _instance: null,
  get instance() {
    if (!this._instance) {
      logger.debug(`Redis connection info: ${host}:${port} (password: ${password ? 'yes' : 'no'})`);
      this._instance = {
        queues: mapBullQueues(),
        arenaQueues: mapArenaQueues(),
        arena(listenPort = config.get('listen')) {
          return Arena(
            { queues: mapArenaQueues() },
            { port: listenPort, disableListen: true },
          );
        },
        get ripQ() {
          return this.queues.find(q => q.name === 'disc ripping');
        },
        get encodeQ() {
          return this.queues.find(q => q.name === 'video encoding');
        },
      };
    }
    return this._instance;
  },
};

function mapBullQueues() {
  return config.get('queues').map(q => {
    const newQ = Queue(q.name, {
      redis: {
        host,
        port,
        password,
      },
    });
    logger.info(`Created ${newQ.name} queue (${q.id})`, newQ);
    return newQ;
  });
}

function mapArenaQueues() {
  return config.get('queues').map(q => ({
    name: q.name,
    port,
    host,
    hostId: 'ncodr',
  }));
}
//
// export default class Queues {
//   constructor() {
//     logger.debug(`Redis connection info: ${host}:${port} (password: ${password ? 'yes' : 'no'})`);
//     this.queues = config.get('queues').map(q => {
//       const newQ = Queue(q.name, {
//         redis: {
//           host,
//           port,
//           password,
//         },
//       });
//       logger.info(`Created ${newQ.name} queue (${q.id})`, newQ);
//       return newQ;
//     });
//     this.arenaQueues = this.queues.map(q => ({
//       name: q.name,
//       port,
//       host,
//       hostId: 'ncodr',
//     }));
//   }
//
//   arena(port = config.get('listen')) {
//     return Arena({ queues: this.arenaQueues }, { port, disableListen: true });
//   }
//
//   queues() {
//     return this.queues;
//   }
//
//   get ripQ() {
//     return this.queues.find(q => q.name === 'disc ripping');
//   }
//
//   get encodeQ() {
//     return this.queues.find(q => q.name === 'video encoding');
//   }
// }

@@ -23,40 +23,8 @@
 l';%0A
-import Arena from 'bull-arena';%0A
 impo
@@ -50,16 +50,16 @@
 ogger';%0A
+
 import %7B
@@ -463,243 +463,8 @@
 (),%0A
-        arenaQueues: mapArenaQueues(),%0A        arena(listenPort = config.get('listen')) %7B%0A          return Arena(%0A            %7B queues: mapArenaQueues() %7D,%0A            %7B port: listenPort, disableListen: true %7D,%0A          );%0A        %7D,%0A
     
@@ -984,1131 +984,4 @@
 ;%0A%7D%0A
-%0Afunction mapArenaQueues() %7B%0A  return config.get('queues').map(q =%3E (%7B%0A    name: q.name,%0A    port,%0A    host,%0A    hostId: 'ncodr',%0A  %7D));%0A%7D%0A//%0A// export default class Queues %7B%0A//   constructor() %7B%0A//     logger.debug(%60Redis connection info: $%7Bhost%7D:$%7Bport%7D (password: $%7Bpassword ? 'yes' : 'no'%7D)%60);%0A//     this.queues = config.get('queues').map(q =%3E %7B%0A//       const newQ = Queue(q.name, %7B%0A//         redis: %7B%0A//           host,%0A//           port,%0A//           password,%0A//         %7D,%0A//       %7D);%0A//       logger.info(%60Created $%7BnewQ.name%7D queue ($%7Bq.id%7D)%60, newQ);%0A//       return newQ;%0A//     %7D);%0A//     this.arenaQueues = this.queues.map(q =%3E (%7B%0A//       name: q.name,%0A//       port,%0A//       host,%0A//       hostId: 'ncodr',%0A//     %7D));%0A//   %7D%0A//%0A//   arena(port = config.get('listen')) %7B%0A//     return Arena(%7B queues: this.arenaQueues %7D, %7B port, disableListen: true %7D);%0A//   %7D%0A//%0A//   queues() %7B%0A//     return this.queues;%0A//   %7D%0A//%0A//   get ripQ() %7B%0A//     return this.queues.find(q =%3E q.name === 'disc ripping');%0A//   %7D%0A//%0A//   get encodeQ() %7B%0A//     return this.queues.find(q =%3E q.name === 'video encoding');%0A//   %7D%0A// %7D%0A

Move common queue interface to JS object instead of being a stateful class