import praw
import logging
import argparse

class RedditClient(object):
    def __init__(self, app_id, app_secret):
        self.logger = logging.getLogger(__name__)
        self.app_id = app_id
        self.app_secret = app_secret
        self.reddit = praw.Reddit(
            client_id=self.app_id,
            client_secret=self.app_secret,
            user_agent="miscdebris-cli/1.0"
        )
    
    @staticmethod
    def print_submission(s):
        '''
        print subbreddit submission to stdout
        '''
        print(f"title:{s.title}\nid:{s.id}\nnum_comments:{s.num_comments}\nshortlink:{s.shortlink}")

    def top(self, subbreddit, limit=10):
        return self.reddit.subreddit(subbreddit).top(limit=limit)
    
    def hot(self, subreddit, limit=10):
        return self.reddit.subreddit(subreddit).hot(limit=limit)
    
    def new(self, subreddit, limit=50):
        return self.reddit.subreddit(subreddit).new(limit=limit)

    def search(self, search, subbreddit=None, limit=10):
        '''
            search syntax example: "subreddit:wallstreetbets AMC GME" searches AMC GME in wallstreetbets reddit.
        '''
        sub = subreddit
        if not subbreddit:
            sub = "all"
        
        return self.reddit.search(subreddit).search(search, limit=limit)

    
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Get reddit posts")
    parser.add_argument("-appid", type=str, required=True, help="Reddit app id")
    parser.add_argument("-appsecret", type=str, required=True, help="Reddit app secret")

    runtime_args = parser.parse_args()

    cli = RedditClient(app_id=runtime_args.appid, app_secret=runtime_args.appsecret)
    for s in cli.top("wallstreetbets"):
        RedditClient.print_submission(s)
    
    for s in cli.hot("wallstreetbets"):
        RedditClient.print_submission(s)

