"use strict";

/*
    Adds methods for accessing TVDB service
*/

(function () {

    var serviceURL = 'http://api.tvmaze.com/';

    var requestLimit = 1000; // minimal requests interval (in ms)
    var lastRequest = 0;


    var searchURL = serviceURL + 'search/shows?q=';

    var downloadPromise = null;

    window.TVMaze = {

        _query: function (query) {
            return new Promise(function (resolve, reject) {
                var call = function () {
                    if (lastRequest + requestLimit < performance.now()) {
                        lastRequest = performance.now();
                        app.utils.web.getURLContentAsync(query).then(function (data) {
                            resolve(JSON.parse(data));
                        }.bind(this), function () {
                            reject();
                        });
                    } else
                        setTimeout(call, 10);
                };

                call();

            }.bind(this));
        },

        getSeriesInfo: function (serie) {
            return this._query(searchURL + serie);
        },
    };

})();
