'use strict';

var Jingle = require('jingle');


module.exports = function (client, stanzas) {
    stanzas.use(require('../stanza/jingle'));
    stanzas.use(require('../stanza/rtp'));
    stanzas.use(require('../stanza/iceUdp'));
    stanzas.use(require('../stanza/file'));


    var jingle = client.jingle = new Jingle();

    jingle.capabilities.forEach(function (cap) {
        client.disco.addFeature(cap);
    });

    var mappedEvents = [
        'outgoing', 'incoming', 'accepted', 'terminated',
        'ringing', 'mute', 'unmute', 'hold', 'resumed'
    ];
    mappedEvents.forEach(function (event) {
        jingle.on(event, function (session, arg1) {
            client.emit('jingle:' + event, session, arg1);
        });
    });

    jingle.on('createdSession', function (session) {
        client.emit('jingle:created', session);
    });

    jingle.on('peerStreamAdded', function (session, stream) {
        client.emit('jingle:remotestream:added', session, stream);
    });

    jingle.on('peerStreamRemoved', function (session, stream) {
        client.emit('jingle:remotestream:removed', session, stream);
    });

    jingle.on('send', function (data) {
        client.sendIq(data, function (err) {
            if (err) {
                client.emit('jingle:error', err);
            }
        });
    });

    client.on('iq:set:jingle', function (data) {
        jingle.process(data);
    });

    client.on('unavailable', function (pres) {
        var peer = pres.from.full;
        jingle.endPeerSessions(peer, true);
    });

    /*
     * gone for now, might come back in the future
    client.call = function (peer) {
        peer = peer.full || peer;
        var sess = jingle.createMediaSession(peer);
        client.sendPresence({to: peer});
        sess.start();
        return sess;
    };
    */

    client.discoverICEServers = function (cb) {
        return this.getServices(client.config.server).then(function (res) {
            var services = res.services.services;
            var discovered = [];

            for (var i = 0; i < services.length; i++) {
                var service = services[i];
                var ice = {};
                if (service.type === 'stun' || service.type === 'stuns') {
                    ice.url = service.type + ':' + service.host;
                    if (service.port) {
                        ice.url += ':' + service.port;
                    }
                    discovered.push(ice);
                    client.jingle.addICEServer(ice);
                } else if (service.type === 'turn' || service.type === 'turns') {
                    ice.url = service.type + ':' + service.host;
                    if (service.port) {
                        ice.url += ':' + service.port;
                    }
                    if (service.transport && service.transport !== 'udp') {
                        ice.url += '?transport=' + service.transport;
                    }

                    if (service.username) {
                        ice.username = service.username;
                    }
                    if (service.password) {
                        ice.credential = service.password;
                    }
                    discovered.push(ice);
                    client.jingle.addICEServer(ice);
                }
            }

            return discovered;
        }).nodeify(cb);
    };
};

@@ -40,16 +40,146 @@
 ngle');%0A
+var JingleMediaSession = require('jingle-media-session');%0Avar JingleFileTransferSession = require('jingle-filetransfer-session');%0A
 %0A%0Amodule
@@ -443,16 +443,372 @@
  Jingle(
+%7B%0A        jid: client.jid,%0A        prepareSession: function (opts) %7B%0A            if (opts.descriptionTypes.indexOf('rtp') %3E= 0) %7B%0A                return new JingleMediaSession(opts);%0A            %7D%0A            if (opts.descriptionTypes.indexOf('filetransfer') %3E= 0) %7B%0A                return new JingleFileTransferSession(opts);%0A            %7D%0A        %7D%0A    %7D
 );%0A%0A    
@@ -2030,287 +2030,8 @@
 );%0A%0A
-    /*%0A     * gone for now, might come back in the future%0A    client.call = function (peer) %7B%0A        peer = peer.full %7C%7C peer;%0A        var sess = jingle.createMediaSession(peer);%0A        client.sendPresence(%7Bto: peer%7D);%0A        sess.start();%0A        return sess;%0A    %7D;%0A    */%0A%0A
     

Add jingle session types