import logging
import time

import praw
import requests

from . import const


logger = logging.getLogger(__package__)


def _retrieve_comment_ids(submission_id):
    # https://www.reddit.com/r/redditdev/comments/5to97v/slug/ddoeedj/
    comments = requests.request(
        "GET",
        const.PUSHSHIFT_ENDPOINTS.COMMENT_IDS.format(submission=submission_id)
    )

    data = comments.json()["data"]

    # See if endpoint returns fullnames. If so, it's fine, if not, we'll have
    # to add the "t1_" (comment type prefix) to each one, and make sure we
    # return it as a list, as `reddit.info` really doesn't like generators.
    if data[0].startswith("t1_"):  # assume if okay for one, it's okay for all
        return data

    return ["t1_" + x for x in data]


def _retrieve_submissions(**params):
    """
    https://goo.gl/mHn7P3

    :param q: The search keyword(s) or phrase
    :param subreddit: Restrict to a specific subreddit
    :param limit: Return up to limit comments
    :param sort: Sort by comment ids in descending or ascending order
    :param after: Show only comments older than epoch date. The API also
        understands m,h,d (i.e. 4h to search the previous 4 hours, etc.)
    """
    # I love pushshift
    submissions = requests.request(
        "GET",
        const.PUSHSHIFT_ENDPOINTS.SUBMISSION_SEARCH,
        params=params
    )
    return submissions.json()["data"]


def _create_reddit_instance():
    return praw.Reddit("reddit", user_agent=const.REDDIT_USER_AGENT)


def comment_stream():
    reddit = _create_reddit_instance()

    return reddit.subreddit("anime").stream.comments()


def submission(submission_id):
    reddit = _create_reddit_instance()

    comments = _retrieve_comment_ids(submission_id)

    return reddit.info(comments)


def ftfs(limit=const.DEFAULTS.FTF_LIMIT):
    reddit = _create_reddit_instance()

    ftfs = _retrieve_submissions(q="Free Talk Fridays", subreddit="anime",
                                 limit=limit, sort="desc")

    # TODO: Generator?
    comments = []

    for ftf in ftfs:
        # Don't make Pushshift servers angry
        time.sleep(5)

        logger.info("Retrieving comment ids for FTF: {} ({})"
                    .format(ftf["title"], ftf["id"]))

        comments.extend(_retrieve_comment_ids(ftf["id"]))

    logger.debug("Received {} comments".format(len(comments)))

    # Sorry, reddit servers
    return reddit.info(comments)

@@ -2412,28 +2412,64 @@
 eturn reddit.info(comments)%0A
+%0A%0A# Aliases%0Astream = comment_stream%0A

Add alias for `comment_stream` source