const YouTube = require('./youtube'), readJson = require("r-json");

var OUTQUOTA = [
    //    'AIzaSyD-JPfe8IRrN-ch0UfhLeu8d6P7rvBFu0w', 'AIzaSyCT8c1p06WzgkG2kSfqx-97RAzwSXEMLnE'
]
const CREDENTIALS = readJson(`${__dirname}/credentials.json`);

module.exports = {
    getKey: function () {
        for (var i in CREDENTIALS.keys) {
            if (OUTQUOTA.indexOf(CREDENTIALS.keys[i]) == -1) {
                return CREDENTIALS.keys[i];
            }
        }
        OUTQUOTA = []
        return CREDENTIALS.keys[0];
    },
    search: {
        list: function (res, key, keyword, limit, options) {
            const youtube = new YouTube(key);
            youtube.search(keyword, limit, options)
                .then(results => {
                    res.send(results)
                })
                .catch(e => {
                    if (!e.code) e.code = 404
                    res.send({ code: e.code, message: e.message, data: { keyword, limit, options } })
                    OUTQUOTA.push(key)
                })
        },
        playlist: function (res, key, keyword, limit, options) {
            const youtube = new YouTube(key);
            youtube.searchPlaylists(keyword, limit, options)
                .then(results => {
                    res.send(results)
                })
                .catch(e => {
                    if (!e.code) e.code = 404
                    res.send({ code: e.code, message: e.message, data: { keyword, limit, options } })
                    OUTQUOTA.push(key)
                })
        },
        video: function (res, key, keyword, limit, options) {
            const youtube = new YouTube(key);
            youtube.searchVideos(keyword, limit, options)
                .then(results => {
                    res.send(results)
                })
                .catch(e => {
                    if (!e.code) e.code = 404
                    res.send({ code: e.code, message: e.message, data: { keyword, limit, options } })
                    OUTQUOTA.push(key)
                })
        },
        channel: function (res, key, keyword, limit, options) {
            const youtube = new YouTube(key);
            youtube.searchChannels(keyword, limit, options)
                .then(results => {
                    res.send(results)
                })
                .catch(e => {
                    if (!e.code) e.code = 404
                    res.send({ code: e.code, message: e.message, data: { keyword, limit, options } })
                    OUTQUOTA.push(key)
                })
        }
    },
    target: {
        playlist: function (res, key, id, options) {
            const youtube = new YouTube(key);
            youtube.getPlaylistByID(id, options)
                .then(results => {
                    res.send(results)
                })
                .catch(e => {
                    if (!e.code) e.code = 404
                    res.send({ code: e.code, message: e.message, data: { id: id, options: options } })
                    OUTQUOTA.push(key)
                })
        },
        playlistitems: function (res, key, id, options) {
            const youtube = new YouTube(key);
            youtube.getPlaylistItemByID(id, options)
                .then(results => {
                    res.send(results)
                })
                .catch(e => {
                    if (!e.code) e.code = 404
                    res.send({ code: e.code, message: e.message, data: { id: id, options: options } })
                    OUTQUOTA.push(key)
                })
        },
        video: function (res, key, id, options) {
            const youtube = new YouTube(key);
            youtube.getVideoByID(id, options)
                .then(results => {
                    res.send({ code: 200, data: results })
                })
                .catch(e => {
                    if (!e.code) e.code = 404
                    res.send({ code: e.code, message: e.message, data: { id: id, options: options } })
                    OUTQUOTA.push(key)
                })
        },
        channel: function (res, key, id, options) {
            const youtube = new YouTube(key);
            youtube.getChannelByID(id, options)
                .then(results => {
                    res.send(results)
                })
                .catch(e => {
                    if (!e.code) e.code = 404
                    res.send({ code: e.code, message: e.message, data: { id: id, options: options } })
                    OUTQUOTA.push(key)
                })
        }
    }
}