import * as WebSocket from 'ws';
import * as ChannelManager from '../channelManager';
import ChannelBase from '../channels/channelBase';
import MHSChannel from '../channels/sites/mhsChannel';

var logger = require('../logger');

class ClientBase {
  ws: WebSocket;
  data: any;
  id: number;
  channel: ChannelBase|MHSChannel|null;

  constructor(data: any, ws: WebSocket) {
    this.ws = ws;
    this.data = data;
    this.id = data.user_id;
    this.channel = null;
  }

  getData() {
    return this.data;
  }

  type() {
    return this.data.client_type;
  }

  clientType() {
    return this.data.client;
  }

  connectToChannel(channel: ChannelBase|MHSChannel) {
    this.channel = channel;

    this.ws.on('message', (message: string) => {
      logger.accessLog.info(`starting broadcast on channel ${channel.id}: `, {message: message});
      channel.broadcastMessage(this, message);
      logger.accessLog.info(`broadcast complete on channel ${channel.id}: `, {message: message});
    });
  }

  replaceWebSocket(ws: WebSocket) {
    this.ws.close();
    this.ws = ws;
  }
};

export default ClientBase;
