/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

var crypto = require('crypto');
var request = require('request');
var conf = require('./config').conf;

// Be sure to use the exported OpenTok so we can mock it in the
// tests.
exports.OpenTok = require('opentok');

function TokBox(settings) {
  this.apiKey = settings.apiKey;
  this.tokenDuration = settings.tokenDuration;
  this.serverURL = settings.apiUrl || "https://api.opentok.com";
  this._opentok = new exports.OpenTok(this.apiKey, settings.apiSecret,
                                      this.serverURL);
}

TokBox.prototype = {
  getSessionTokens: function(cb) {
    var self = this;
    this._opentok.createSession({
      mediaMode: 'relayed'
    }, function(err, sessionId) {
        if (err || sessionId === undefined || sessionId === null) {
          cb(err || new Error("Got an empty sessionId from tokbox, check " +
                              "your credentials."));
          return;
        }
        var now = Math.round(new Date().getTime() / 1000.0);
        var expirationTime = now + self.tokenDuration;
        cb(null, {
          sessionId: sessionId,
          callerToken: self._opentok.generateToken(sessionId, {
            role: 'publisher',
            expireTime: expirationTime
          }),
          calleeToken: self._opentok.generateToken(sessionId, {
            role: 'publisher',
            expireTime: expirationTime
          })
        });
      }
    );
  }
};

function FakeTokBox(serverURL) {
  this._counter = 0;
  this.serverURL = conf.get("fakeTokBoxURL");
}

FakeTokBox.prototype = {
  _urlSafeBase64RandomBytes: function(number_of_bytes) {
    return crypto.randomBytes(number_of_bytes).toString('base64')
                 .replace(/\+/g, '-').replace(/\//g, '_');
  },
  _fakeSessionId: function() {
    this._token = 0;
    this._counter += 1;
    return this._counter + '_' + this._urlSafeBase64RandomBytes(51);

  },
  _generateFakeToken: function() {
    this._token += 1;
    return 'T' + this._token + '==' + this._urlSafeBase64RandomBytes(293);
  },
  getSessionTokens: function(cb) {
    var self = this;
    // Do a real HTTP call to have a realistic behavior.
    request.get(self.serverURL, function(err) {
      cb(err, {
        sessionId: self._fakeSessionId(),
        callerToken: self._generateFakeToken(),
        calleeToken: self._generateFakeToken()
      });
    });
  }
};


exports.TokBox = TokBox;
exports.FakeTokBox = FakeTokBox;
exports.OpenTok = exports.OpenTok;
exports.request = request;

@@ -899,18 +899,16 @@
  session
-Id
 ) %7B%0A    
@@ -923,136 +923,38 @@
 err 
-%7C%7C sessionId === undefined %7C%7C sessionId === null) %7B%0A          cb(err %7C%7C new Error(%22Got an empty sessionId from tokbox, check %22 +
+!== null) %7B%0A          cb(err);
 %0A   
@@ -956,36 +956,40 @@
 ;%0A          
-    
+return;%0A
         
         %22you
@@ -976,24 +976,26 @@
         
+%7D%0A
         
 %22your cr
@@ -990,58 +990,42 @@
     
-%22your credentials.%22));%0A          return;%0A        %7D
+var sessionId = session.sessionId;
 %0A   

Update opentok library usage