var MsTranslator = require('../mstranslator');
var assert = require('assert');
var expect = require('chai').expect;
var _ = require('underscore');

var client_id = process.env.MSCLIENT_ID;
var client_secret = process.env.MSCLIENT_SECRET;
var api_key = process.env.API_KEY;

if (!client_id || !client_secret) {
  console.log('missing client_id and client_secret');
  process.exit(1);
}

var translator = new MsTranslator({client_id: client_id, client_secret: client_secret, api_key: api_key}, true);

describe('MsTranslator', function () {
  /*
   it('test breakSentences', function() {
   var text = encodeURIComponent("This is one sentence. The method will count this as the second sentences. Finally, the third sentence.");
   var params = { text: text, language: 'en' };
   translator.access_token(client_id, client_secret, function(err, access_token) {
   translator.breakSentences(params, access_token, function(err, data) {
   });
   });
   }*/


  it('tests detect', function (done) {
    var text = encodeURIComponent("The language of this text is going to be detected.");
    var params = {text: text};
    translator.detect(params, function (err, data) {
      assert.equal(data, 'en');
      done();
    });
  });

  it('tests detectArray', function (done) {
    var texts = "[\"This is English text.\", \"Das ist deutsche Text.\", \"Questo un testo italiano.\"]";
    var params = {texts: texts};
    translator.detectArray(params, function (err, data) {
      assert.deepEqual(data, ['en', 'de', 'it']);
      done();
    });
  });

  it('tests getLanguageNames', function (done) {
    var languageCodes = "[\"de\", \"fr\", \"it\"]";
    var params = {locale: 'en', languageCodes: languageCodes};
    translator.getLanguageNames(params, function (err, data) {
      assert.deepEqual(data, ['German', 'French', 'Italian']);
      done();
    });
  });

  it('tests getLanguagesForSpeak', function (done) {
    translator.getLanguagesForSpeak(function (err, data) {
      assert.equal(typeof data, 'object');
      done();
    });
  });

  it('tests getLanguagesForTranslate', function (done) {
    translator.getLanguagesForTranslate(function (err, data) {
      assert.equal(typeof data, 'object');
      done();
    });
  });


  it('tests speak', function (done) {
    var params = {text: 'Muchas gracias.', language: 'es', format: 'audio/wav'};
    translator.speak(params, function (err, data) {
      assert.equal(typeof data, 'object');
      done();
    });
  });

  it('tests speakURL', function (done) {
    var params = {text: 'Muchas gracias.', language: 'es', format: 'audio/wav'};
    translator.speakURL(params, function (err, data) {
      assert.equal(typeof data, 'string');
      done();
    });
  });

  it('tests translate', function (done) {
    var params = {text: 'translate this.', from: 'en', to: 'es'};
    translator.translate(params, function (err, data) {
      assert.equal(data.toLowerCase(), 'traducir esto.');
      done();
    });
  });

  it('tests translateArray', function (done) {
    var texts = ['monkey', 'cow'];
    var params = {texts: texts, from: 'en', to: 'es', maxTranslations: 5};
    translator.translateArray(params, function (err, data) {
      assert.equal(data[0].TranslatedText, 'mono');
      assert.equal(data[1].TranslatedText, 'vaca');
      done();
    });
  });

  it('tests translateArray for texts with quotes', function (done) {
    var texts = ['"start"', 'single "', 'escaped \\"'];
    var params = {texts: texts, from: 'en', to: 'es', maxTranslations: 5};
    translator.translateArray(params, function (err, data) {
      assert.equal(Array.isArray(data), true);
      assert.equal(data[0].TranslatedText, '«start»');
      assert.equal(data[1].TranslatedText, 'solo"');
      assert.equal(data[2].TranslatedText, 'escapó"');
      done();
    });
  });

  it('tests translateArray2', function (done) {
    var texts = ['monkey', 'cow'];
    var params = {texts: texts, from: 'en', to: 'es', maxTranslations: 5};
    translator.translateArray2(params, function (err, data) {
      assert.equal(data[0].TranslatedText, 'mono');
      assert.equal(data[0].Alignment, '0:5-0:3');
      assert.equal(data[1].TranslatedText, 'vaca');
      assert.equal(data[1].Alignment, '0:2-0:3');
      done();
    });
  });

  it('tests addTranslation', function (done) {
    var addParams = {
      originalText: 'Esto es una prueba',
      translatedText: 'This is a quiz!',
      from: 'es',
      to: 'en',
      user: 'testuser',
    };

    var getParams = {
      text: 'Esto es una prueba',
      from: 'es',
      to: 'en',
      maxTranslations: 200,
    };

    var params = {text: 'translate this.', from: 'en', to: 'es'};
    translator.addTranslation(addParams, function (err, data) {
      assert.equal(err, null);

      // Added translation should now appear in results
      translator.getTranslations(getParams, function (err, data) {
        expect(data).to.have.property('Translations');
        translations = _.pluck(data.Translations, 'TranslatedText');
        expect(translations).to.contain('This is a quiz!');
        done();
      });
    });
  });

  it('handles an ArgumentOutOfRangeException', function (done) {
    var params = {
      text: 'whatever',
      from: 'en',
      to: 'easdfn'
    };
    translator.translate(params, function (err, data) {
      assert.ok(
        err.message.indexOf('ArgumentOutOfRangeException:') !== -1,
        'An error reports the ArgumentOutOfRangeException.'
      );
      done();
    });
  });

  it('handles an ArgumentNullException', function (done) {
    var params = {
      from: 'en',
      to: 'es'
    };
    translator.translate(params, function (err, data) {
      assert.ok(
        err.message.indexOf('ArgumentNullException:') !== -1,
        'An error reports the ArgumentNullException.'
      );
      done();
    });
  });

  it('expires current token if token is bad', function (done) {
    var params = {
      text: 'Esto es una prueba',
      from: 'en',
      to: 'es'
    };
    translator.access_token = 'bad-token';
    var expirationInOneMinute = Date.now() + 60000;
    translator.expires_at = expirationInOneMinute;
    translator.translate(params, function (err, data) {
      assert.ok(
        err.message.indexOf('token') !== -1,
        'The error contains the word token'
      );
      assert.ok(
        translator.expires_at < expirationInOneMinute,
        'The access_token expiration was set to now'
      );
      done();
    });
  });

});

@@ -149,329 +149,128 @@
 var 
-client_id = process.env.MSCLIENT_ID;%0Avar client_secret = process.env.MSCLIENT_SECRET;%0Avar api_key = process.env.API_KEY;%0A%0Aif (!client_id %7C%7C !client_secret) %7B%0A  console.log('missing client_id and client_secret');%0A  process.exit(1);%0A%7D%0A%0Avar translator = new MsTranslator(%7Bclient_id: client_id, client_secret: client_secret, 
+api_key = process.env.API_KEY;%0A%0Aif (!api_key) %7B%0A  console.log('missing api_key');%0A%7D%0A%0Avar translator = new MsTranslator(%7B
 api_

Update tests to just use api_key since client_id/client_secret don't work anymore.