/**
 * 百度翻译的 API 支持
 *
 * http://dwz.cn/bau5M
 * 
 * jshint strict:true
 */
var sugar = require('sugar');
var $ = require('jquery');
var utils = require('../lib/utils');

// TODO: Auth detect word
var WORD_URL = 'http://dict.baidu.com/s?wd=';
var PHRASE_URL = 'http://openapi.baidu.com/public/2.0/bmt/translate?client_id=hXxOZlP7bsOYFS6EFRmGTOe5&from=en&to=zh&q=';

var SEL_WORD = '.en-simple-means';
var SEL_WORD_MEANS = '.en-simple-means .en-content > div > p';
var SEL_WORD_PHONETIC = '.pronounce [lang="EN-US"]:last';

function formatWord(result) {
  var $result = $(result);

  if (!$result.find(SEL_WORD).length) return null;

  var response = {};
  
  var $phonetic = $result.find(SEL_WORD_PHONETIC);
  if ($phonetic.length) {
    response.phonetic = $phonetic.text();
  }
  
  var $means = $result.find(SEL_WORD_MEANS);
  response.translation = $means.map(function() {
    return $(this).text();
  }).toArray().join('<br />');

  return response;
}

function formatPhrase(result) {
  if (!result) return null;

  var response = {};
  var trans_result = result.trans_result[0];

  if (trans_result.src == trans_result.dst) return null;

  response.translation = trans_result.dst;

  return response;
}

function requestWord(text, callback) {
  var request = $.get(WORD_URL + encodeURIComponent(text));

  request.done(function(html) {
    callback(formatWord(utils.sanitizeHTML(html)));
  });

  request.fail(function() {
    // TODO: Raise Error instead
    callback(null);
  });
}

function requestPhrase(text, callback) {
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (this.readyState == 4) {
      var result = JSON.parse(this.responseText);
      callback(formatPhrase(result));
    }
  };
  xhr.open('GET', PHRASE_URL + encodeURIComponent(text), true);
  xhr.send();
}

var BaiduTranslator = { name: 'baidu' };

BaiduTranslator.translate = function(text, callback) {
  if (/^\s*$/.test(text)) {
    callback(null);
  } else if (/^[a-zA-Z]+$/.test(text)) {
    requestWord(text, callback);
  } else {
    requestPhrase(text, callback);
  }
};

module.exports = BaiduTranslator;
@@ -265,14 +265,12 @@
 p://
-openap
+fany
 i.ba
@@ -281,84 +281,18 @@
 com/
-public/2.0/bmt/translate?client_id=hXxOZlP7bsOYFS6EFRmGTOe5&from=en&to=zh&q=
+v2transapi
 ';%0A%0A
@@ -1005,16 +1005,21 @@
 s_result
+.data
 %5B0%5D;%0A%0A  
@@ -1472,282 +1472,332 @@
 var 
-xhr = new XMLHttpRequest();%0A  xhr.onreadystatechange = function() %7B%0A    if (this.readyState == 4) %7B%0A      var result = JSON.parse(this.responseText);%0A      callback(formatPhrase(result));%0A    %7D%0A  %7D;%0A  xhr.open('GET', PHRASE_URL + encodeURIComponent(text), true);%0A  xhr.send(
+payload = %7B%0A    from: 'en',%0A    to: 'zh',%0A    query: text,%0A    transtype: 'translang',%0A    simple_means_flag: 3%0A  %7D;%0A%0A  var request = $.post(PHRASE_URL, payload);%0A  request.done(function(result) %7B%0A    callback(formatPhrase(result));%0A  %7D);%0A%0A  request.fail(function() %7B%0A    // TODO: Raise Error instead%0A    callback(null);%0A  %7D
 );%0A%7D

migrate baidu service to use new api.