'use strict';

let streamingSession =  function({roomId, pinHash, streamId, href, iceServers, accessToken} = {}, room, wt) {

    let ___;

    return (___ = {

        get userId() {
            return room.userId;
        },
        get roomId() {
            return roomId;
        },
        get sessionId () {
            return room.sessionId;
        },
        get constructId () {
            return room.constructId;
        },

        kill: function () {
            return room.destroy()
                .finally(() => {
                    this.$clear();
                    return true;
                });
        },

        destroy: function() {
            return wt.room.destroySession(room.constructId)
        },

        connect: function() {
            if(roomId) {
                return Promise.all([wt.room.getRoomById(roomId, pinHash), wt.user.getUserSelf()])
                    .then(([roomData, r]) => room.startStream(roomData.data.wtChannel.streamId, roomData.data.wtChannel.href, roomData.data.wtChannel.iceServers, roomData.data.wtChannel.accessToken, r.data._id));
            }
            else if(href) {
                return wt.user.getUserSelf()
                    .then((r) => room.startStream(streamId, href, iceServers, accessToken, r.data._id));
            }
            else
                return Promise.reject('Bad Input')
        },

        disconnect: function() {
            return room.stopStream();
        },

        $on: (key, callback, that) => {
            return room.on(key, callback, that || this);
        },

        $off: (key, callback, that) => {
            return room.off(key, callback, that || this);
        },

        $clear: function() {
            room.clear();
        }
    })
};

export default streamingSession;
