import Message from './Message'
import Map from 'es6-map'

/**
 * Represents an IRC Channel on the server.
 *
 * @param {string} name Channel name. (Starting with # or &, preferably.)
 *
 * @constructor
 */
export default function Channel (name) {
  if (!(this instanceof Channel)) return new Channel(name)

  this.name = name
  this.users = []
  this.topic = null

  this.modes = new Map()
}

/**
 * Joins a user into this channel.
 *
 * @param {User} user Joining user.
 */
Channel.prototype.join = function (user) {
  this.users.push(user)
  user.channels.push(this)
}

/**
 * Parts a user from this channel.
 *
 * @param {User} user Parting user.
 */
Channel.prototype.part = function (user) {
  let i = this.users.indexOf(user)
  if (i !== -1) {
    this.users.splice(i, 1)
  }
  i = user.channels.indexOf(this)
  if (i !== -1) {
    user.channels.splice(i, 1)
  }
}

/**
 * Checks if a user is in this channel.
 *
 * @param {User} user User to look for.
 *
 * @return boolean Whether the user is here.
 */
Channel.prototype.hasUser = function (user) {
  return this.users.indexOf(user) !== -1
}

/**
 * Sends a message to all users in a channel, including the sender.
 *
 * @param {Message} message Message to send.
 */
Channel.prototype.send = function (message) {
  if (!(message instanceof Message)) {
    message = Message.apply(null, arguments)
  }
  this.users.forEach(u => { u.send(message) })
}

/**
 * Broadcasts a message to all users in a channel, except the sender.
 *
 * @param {Message} message Message to send.
 */
Channel.prototype.broadcast = function (message) {
  if (!(message instanceof Message)) {
    message = Message.apply(null, arguments)
  }
  this.users.forEach(u => {
    if (!u.matchesMask(message.prefix)) u.send(message)
  })
}

Channel.prototype.setMode = function (user, mode) {
  if (mode == null) this.modes.delete(user)
  else this.modes.set(user, mode)
}

Channel.prototype.getMode = function (user) {
  return this.modes.get(user)
}

@@ -29,33 +29,55 @@
 ge'%0A
-import Map from 'es6-map'
+%0Aconst debug = require('debug')('ircs:Channel')
 %0A%0A/*
@@ -392,25 +392,57 @@
 his.
-m
+flagM
 odes = 
-new Map()
+%5B%5D%0A  this.ops = %5B%5D%0A  this.voices = %5B%5D
 %0A%7D%0A%0A
@@ -616,16 +616,75 @@
 sh(this)
+%0A%0A  if (this.users.length === 1) %7B%0A    this.addOp(user)%0A  %7D
 %0A%7D%0A%0A/**%0A
@@ -1458,32 +1458,79 @@
  arguments)%0A  %7D%0A
+  debug(this.name, 'send', message.toString())%0A
   this.users.for
@@ -1942,197 +1942,1154 @@
 ype.
-setMode = function (user, mode) %7B%0A  if (mode == null) this.modes.delete
+addOp = function (user) %7B%0A  if (!this.hasOp(user)) %7B%0A    this.ops.push(user)%0A  %7D%0A%7D%0A%0AChannel.prototype.removeOp = function (user) %7B%0A  this.ops = this.ops.filter((op) =%3E op !== user)%0A%7D%0A%0AChannel.prototype.hasOp = function (user) %7B%0A  return this.ops.indexOf(user) !== -1%0A%7D%0A%0AChannel.prototype.addVoice = function (user) %7B%0A  if (!this.hasVoice(user)) %7B%0A    this.voices.push(user)%0A  %7D%0A%7D%0A%0AChannel.prototype.removeVoice = function (user) %7B%0A  this.voices = this.voices.filter((voice) =%3E voice !== user)%0A%7D%0A%0AChannel.prototype.hasVoice = function 
 (user)
+ %7B
 %0A  
-else this.modes.set(user, mode)%0A%7D%0A%0AChannel.prototype.getMode = function (user) %7B%0A  return this.modes.get(user)
+return this.voices.indexOf(user) !== -1%0A%7D%0A%0AChannel.prototype.addFlag = function (flag) %7B%0A  this.flagModes.push(flag)%0A%7D%0A%0AChannel.prototype.removeFlag = function (flag) %7B%0A  this.flagModes = this.flagModes.filter((mode) =%3E mode !== flag)%0A%7D%0A%0AChannel.prototype.isPrivate = function () %7B%0A  return this.flagModes.indexOf('p') !== -1%0A%7D%0A%0AChannel.prototype.isSecret = function () %7B%0A  return this.flagModes.indexOf('s') !== -1%0A%7D%0A%0AChannel.prototype.isInviteOnly = function () %7B%0A  return this.flagModes.indexOf('i') !== -1%0A%7D%0A%0AChannel.prototype.isModerated = function () %7B%0A  return this.flagModes.indexOf('m') !== -1
 %0A%7D%0A

implement mode flags, op and voice on Channel model