module.exports.search = search;

var xml2js = require("xml2js"),
    defaultify = require("defaultify"),
    sc = require("service-client"),
    util = require("util"),
    querystring = require("querystring");

function search(term, opts, callback) {
    if (arguments.length === 2 && typeof(opts) === "function") {
        callback = opts;
        opts = {};
    }
    
    var defaultOpts = {
        sort: "seeders",
        page: 1,
        totalPages: 1
    };
    opts = defaultify(opts, defaultOpts, true).value;
    
    var req = {
        rss: 1,
        field: opts.sort,
        sorder: "desc"
    };
    
    var resultList = [];
    var want = opts.totalPages;
    for (var i = 0; i < opts.totalPages; ++i) {
        requestPage(term, opts.page, i, req, resultList, function(err, data) {
            if (want === 0) return;
            
            if (err) {
                callback(err);
                want = 0;
            } else {
                if (--want == 0) {
                    var obj = {
                        title: resultList[0].title,
                        items: resultList[0].items
                    };
                    for (i = 1; i < opts.totalPages; i++) {
                        obj.items = obj.items.concat(resultList[i].items);
                    }
                    callback(null, obj);
                }
            }
        });
    }
}

function requestPage(term, startPage, page, req, result, callback) {
    var url = "http://kickass.to/usearch/" + term + "/" + (startPage + page) + "/?" + querystring.stringify(req);
    console.log(url);
    
    sc.get(url, {
        parse: xml2js.parseString,
        transform: transformRss
    },
    function(err, data) {
        if (err) {
            callback(err);
        } else {
            result[page] = data;
            callback(null);
        }
    });
}


function transformRss(data, callback) {
    process.nextTick(function() {
        if (data && data.rss && data.rss.channel) {
            transformChanel(data.rss.channel[0], callback);
        } else {
            callback("missing element");
        }
    });
}

function transformChanel(data, callback) {
    var obj = {
        title: data.description[0],
        items: []
    };
    
    for (var i = 0; i < data.item.length; ++i) {
        obj.items[i] = transformItem(data.item[i]);
    }
    
    callback(null, obj);
}

function transformItem(item) {
    return {
        title: mapArray(item.title),
        category: mapArray(item.category),
        author: mapArray(item.author),
        link: mapArray(item.link),
        date: new Date(mapArray(item.pubDate)),
        size: parseInt(mapArray(item["torrent:contentLength"])),
        infohash: mapArray(item["torrent:infoHash"]),
        magnet: mapArray(item["torrent:magnetURI"]),
        seeds: parseInt(mapArray(item["torrent:seeds"])),
        peers: parseInt(mapArray(item["torrent:peers"])),
        verified: mapArray(item["torrent:verified"]) === "1",
        filename: mapArray(item["torrent:fileName"]),
        url: mapArray(item.enclosure)["$"].url
    };
}

function mapArray(field) {
    if (field) {
        return field[0];
    }
}

@@ -1577,30 +1577,8 @@
 q);%0A
-    console.log(url);%0A
     

remove console.log