"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const events = require("events");
const SipClient_1 = require("./SipClient");
const StompClient_1 = require("./StompClient");
class CallablePhone extends events.EventEmitter {
    constructor(options) {
        super();
        this._sipClient = new SipClient_1.default(options);
        this._stompClient = new StompClient_1.default(options);
        this.addUAEventListeners();
        this.addStompListeners();
    }
    start() {
        this._stompClient.connect();
        this._sipClient.start();
    }
    disconnect() {
        this._stompClient.disconnect();
        this._sipClient.stop();
    }
    addSipSessionEventHandlers(session) {
        var self = this;
        session.on('ringing', () => {
            self.emit('update', {
                type: 'session',
                status: 'ringing',
                session,
            });
        });
        session.on('answer', () => {
            session.setActive(true);
            self.emit('update', {
                type: 'session',
                status: 'answer',
                session,
            });
        });
        session.on('terminate', () => {
            self.emit('update', {
                type: 'session',
                status: 'terminated',
                session,
            });
        });
        session.on('change', () => {
            self.emit('update', {
                type: 'session',
                status: session.status,
                session,
            });
        });
    }
    addUAEventListeners() {
        var self = this;
        this._sipClient.on('connected', ({ client }) => {
            self._debug && console.log({ client }, 'connected');
            self.emit('update', {
                type: 'client',
                status: 'connected',
                client,
            });
        });
        this._sipClient.on('disconnected', ({ client }) => {
            self._debug && console.log({ client }, 'disconnected');
            self.emit('update', {
                type: 'client',
                status: 'disconnected',
                client,
            });
        });
        this._sipClient.on('registered', ({ client }) => {
            self._debug && console.log({ client }, 'registered');
            client.registered = true;
            self.emit('update', {
                type: 'client',
                status: 'registered',
                client,
            });
        });
        this._sipClient.on('unregistered', ({ client }) => {
            self._debug && console.log({ client }, 'unregistered');
            client.registered = false;
            self.emit('update', {
                type: 'client',
                status: 'unregistered',
                client,
            });
        });
        this._sipClient.on('registrationFailed', ({ response, client }) => {
            self._debug &&
                console.log({ client }, `unregistered: registration failed with status ${response.status_code}`);
            client.registered = false;
            self.emit('update', {
                type: 'client',
                status: 'registrationFailed',
                client,
            });
        });
        this._sipClient.on('session', (session) => {
            self._debug && console.log({ session }, 'got a new session');
            self.addSipSessionEventHandlers(session);
            self.emit('update', {
                type: 'session',
                status: 'new',
                client: self._phoneClient,
                session,
            });
        });
    }
    addStompListeners() {
        var self = this;
        this._stompClient.on('msg', (json) => {
            self._debug && console.log({ json }, 'got a new message');
            self.emit('message', json);
        });
    }
}
exports.default = CallablePhone;
