'use strict';

const electron = require('electron');
const app = electron.app;


const VideoManager = require('./videomanager');
const YoutubeApi = require('./youtubeapi');
const Windows = require('./windows')(electron);
const server = require('./server');
const database = require('./database');

const youtubeRegex = require('youtube-regex');

// adds debug features like hotkeys for triggering dev tools and reload
require('electron-debug')();

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', () => {
  Windows.openMainWindow();

  VideoManager.refresh();

  server.io.on('connection', (socket) => {
    let subscriptions = [];

    socket.on('youtube/auth', () => {
      socket.emit('youtube/waiting');

      YoutubeApi.getAuthUrl((url) => {
        socket.emit('youtube/waitingforuser');
        Windows.openLogInWindow(url);
      });
    });

    socket.on('subscriptions/list', () => {
      if (subscriptions.length)
        return socket.emit('subscriptions/list', subscriptions);
      
      YoutubeApi.getSubscriptions((err, _subscriptions) => {
        if (err) {
          console.error(err);
        } else {
          subscriptions = _subscriptions;
          socket.emit('subscriptions/list', subscriptions);
        }
      });
    });

    // Video
    socket.on('video/start', (id) => {
      console.log('Video started: ', id);
    });

    socket.on('video/pause', (id) => {
      console.log('Video paused: ', id);
    });

    socket.on('video/buffer', (id) => {
      console.log('Video buffering: ', id);
    });

    /* When something is pasted, if it is a YouTube video, play it */
    socket.on('video/paste', (text) => {
      if (!youtubeRegex().test(text))
        return;

      let videoId = youtubeRegex().exec(text)[1];
      YoutubeApi.getVideo(videoId, function (err, theVideo) {
        if (err) return;
        playVideo(theVideo);
      });
    });

    socket.on('app/authenticate', () => {
      YoutubeApi.tryStoredAccessToken((noValidAccessToken, token) => {
        if (noValidAccessToken) {
          socket.emit('youtube/notauthenticated');
        } else {
          socket.emit('youtube/callback', token);
        }
      });
    });
  });

  server.hapi.route({
    method: 'GET',
    path:'/youtube/callback',
    handler: function (request, reply) {
      server.io.emit('youtube/waiting');
      YoutubeApi.getToken(request.query.code, function (token) {
        server.io.emit('youtube/callback', token);

        Windows.closeLogInWindow();
      });
    }
  });
});

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', function () {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  Windows.activateWithNoOpenWindows();
});

@@ -56,28 +56,48 @@
 nst 
-app = electron.app;%0A
+youtubeRegex = require('youtube-regex');
 %0A%0Aco
@@ -274,95 +274,36 @@
 ');%0A
+%0A%0A
 const 
-database = require('./database');%0A%0Aconst youtubeRegex = require('youtube-regex');
+app = electron.app;%0A
 %0A%0A//
@@ -1018,16 +1018,20 @@
 s.length
+ %3E 0
 )%0A      
@@ -1089,22 +1089,16 @@
 tions);%0A
-      
 %0A      Y
@@ -1805,18 +1805,20 @@
 %0A%0A      
-le
+cons
 t videoI
@@ -1889,22 +1889,16 @@
 oId,
- function
  (err
+/* 
 , th
@@ -1903,17 +1903,23 @@
 theVideo
-)
+ */) =%3E
  %7B%0A     
@@ -1950,28 +1950,15 @@
     
-playVideo(theVideo);
+// ToDo
 %0A   
@@ -2361,33 +2361,20 @@
 er: 
-function (request, reply)
+(request) =%3E
  %7B%0A 
@@ -2462,25 +2462,16 @@
 ode,
- function
  (token)
  %7B%0A 
@@ -2462,24 +2462,27 @@
 ode, (token)
+ =%3E
  %7B%0A        s
@@ -2874,19 +2874,13 @@
 e', 
-function ()
+() =%3E
  %7B%0A 

Fix src/app.js