var req = require('request');
var wg = require('./wordsGenerator');
var jsonp = require('./jsonp');

module.exports = Checker;

var DEFAULT_CHECK_URL = 'http://pandavip.www.net.cn/check/checkdomain';
var TOKEN_REG_URL = 'https://ynuf.alipay.com/service/um.json?xv=0.8.1&xt=';
var DOMAIN_LIST = ['com', 'net', 'cn', 'xyz', 'im', 'me', 'top', 'ren', 'cc'];
var DEFAULT_METHOD = 'call';

function Checker(options) {

}

Checker.prototype.wgRun = function(num) {
    var words = wg.numberAndlettre(num);
    var that = this;
    var i = 0;
    var k;

    function _check() {
        if (i < words.length) {
            for(k = 0; k<DOMAIN_LIST.length; k++){
                that.check(words[i] + '.'+DOMAIN_LIST[k], function(error, response, body) {
                    if(body.module[0].avail){
                        console.log(body.module[0].avail);
                    }
                    i++;
                    _check();
                });
            }
        }
    }

    _check();
}

Checker.prototype.check = function(domain, callback) {
    var token = genToken();
    req(TOKEN_REG_URL + token, function(error, response) {
        req(DEFAULT_CHECK_URL + '?callback=' + DEFAULT_METHOD + '&domain=' + domain + '&token=' + token, function(error, response, body) {
            console.log(body);
            if (error) {
                callback(error);
            } else {
                jsonp.parse(DEFAULT_METHOD, body, function(err, data) {
                    callback(error, response, data);
                });
            }

        });
    });
}

function genToken() {
    var data = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
    var result = [];
    var length = data.length;
    for (var i = 0; i < 32; i++) {
        var r = Math.floor(Math.random() * length);
        result.push(data[r]);
    }
    return 'check-web-hichina-com:' + result.join('');
}
@@ -376,16 +376,36 @@
  'call';
+%0Avar DELAY = '1000';
 %0A%0Afuncti
@@ -426,16 +426,113 @@
 ions) %7B%0A
+    this.opts = options %7C%7C %7B%7D;%0A    if (!this.opts.delay) %7B%0A        this.opts.delay = DELAY;%0A    %7D
 %0A%7D%0A%0AChec
@@ -710,24 +710,54 @@
 s.length) %7B%0A
+            var domains = %5B%5D;%0A
             
@@ -759,16 +759,17 @@
      for
+ 
 (k = 0; 
@@ -769,17 +769,19 @@
 k = 0; k
-%3C
+ %3C 
 DOMAIN_L
@@ -796,16 +796,17 @@
 th; k++)
+ 
 %7B%0A      
@@ -819,18 +819,20 @@
     
-that.check
+domains.push
 (wor
@@ -842,17 +842,19 @@
 i%5D + '.'
-+
+ + 
 DOMAIN_L
@@ -863,146 +863,430 @@
 T%5Bk%5D
-, function(error, response, body) %7B%0A                    if(body.module%5B0%5D.avail)%7B%0A                        console.log(body.module%5B0%5D.avail
+);%0A            %7D%0A            domains = domains.join(',');%0A%0A            that.check(domains, function(error, response, body) %7B%0A                if (error %7C%7C response.statusCode !== 200 %7C%7C !body.module) %7B%0A                    console.log('something wrong!');%0A                    return;%0A                %7D%0A                body.module.forEach(function(m) %7B%0A                    if (m.avail) %7B%0A                        console.log(m
 );%0A 
@@ -1302,32 +1302,48 @@
           %7D%0A    
+            %7D);%0A
                 
@@ -1347,16 +1347,17 @@
    i++;%0A
+%0A
         
@@ -1368,39 +1368,137 @@
     
-    _check();%0A                %7D
+console.log('domain:' + words%5Bi - 1%5D + ' finished:' + i + '/' + words.length);%0A                setTimeout(_check, that.opts.delay
 );%0A 
@@ -1501,32 +1501,34 @@
 );%0A            %7D
+);
 %0A        %7D%0A    %7D
@@ -1831,39 +1831,8 @@
 ) %7B%0A
-            console.log(body);%0A
     

modify query logic，make more easy to find domain