/* eslint-disable no-console */
/* eslint-disable func-names */
/* eslint-disable arrow-parens */
import Koa from 'koa';
import IO from 'koa-socket';
import send from 'koa-send';
import path from 'path';
import chalk from 'chalk'
import moment from 'moment'

const app = Koa();
const io = new IO();

const g = chalk.green.bind(chalk);
const b = chalk.blue.bind(chalk);
const gr = chalk.grey.bind(chalk);
const r = chalk.red.bind(chalk);
const y = chalk.yellow.bind(chalk);

const port = process.env.PORT || 3000

const d = () => y(`[${moment().format('HH:mm:ss')}]`)

app.use(function* () {
  yield send(this, this.path,
    { root: path.join(__dirname, '../../dist/index.html') });
})
io.attach(app);

io.on('connection', ctx => {
  const ip = ctx.socket.handshake.headers['x-forwarded-for'] || ctx.socket.handshake.address.address;
  console.log(d() + b(' Connection: ') + r('New user connected'), gr(ctx.socket.id), gr(ip));
});

let usernames = [];
io.on('disconnect', ctx => {
  const { username } = ctx.socket;
  if (username) {
    console.log(`[server] disconnected: ${username}`);
    usernames = usernames.filter(u => u !== username)
  }
});

io.on('login', (ctx, { username }) => {
  console.log(`[server] login: ${username}`);
  usernames.push(username);
  console.log('this is usernames array: ', usernames);
  ctx.socket.username = username;
  ctx.socket.usernames = usernames;

  io.broadcast('users.login', { username, usernames });
  console.log('users.login', username, usernames);
});

io.on('logout', ctx => {
  const { username } = ctx.socket;
  if (username) {
    console.log(`[server] logout: ${username}`);
    usernames = usernames.filter(u => u !== username)
    delete ctx.socket['username'];

    io.broadcast('users.logout', { username });
  }
});

let messages = [];
io.on('message', (ctx, { text }) => {
  console.log(`[server] message: ${text}`);
  const message = {
    id: messages.length,
    text,
    username: ctx.socket.username,
  };
  messages.push(message);

  io.broadcast('messages.new', { message });
});

const env = process.env.NODE_ENV || 'dev'

app.listen(port, () => {
  console.log(`Server started on port ${r(port)}, environement: ${b(env)}`);
});

@@ -289,24 +289,49 @@
  new IO();%0A%0A
+const log = console.log;%0A
 const g = ch
@@ -850,32 +850,24 @@
 .address;%0A  
-console.
 log(d() + b(
@@ -1045,32 +1045,24 @@
 name) %7B%0A    
-console.
 log(%60%5Bserver
@@ -1193,32 +1193,24 @@
 e %7D) =%3E %7B%0A  
-console.
 log(%60%5Bserver
@@ -1259,32 +1259,24 @@
 sername);%0A  
-console.
 log('this is
@@ -1437,24 +1437,16 @@
 s %7D);%0A  
-console.
 log('use
@@ -1565,24 +1565,16 @@
 ) %7B%0A    
-console.
 log(%60%5Bse
@@ -1804,32 +1804,24 @@
 t %7D) =%3E %7B%0A  
-console.
 log(%60%5Bserver
@@ -2084,24 +2084,16 @@
  =%3E %7B%0A  
-console.
 log(%60Ser

modify logging