# This is a pip install
from newsapi import NewsApiClient
# This is from file articles.py
from article import Article
import os, bookmark_articles_db, user_interface
from functools import lru_cache # functools is a python built-in modules that handles caching

# api key for the environment variables
# daaba2aab3d54874a0a154c18715e82c
apiNewsClient = os.environ.get('NEWS_CLIENT_API_KEY')



# This is the api Key
## TODO: Make the actual key on another page and import it it
newsapi = NewsApiClient(api_key = apiNewsClient)
# top_headlines = newsapi.get_top_headlines(q = 'white house')
# all_articles = newsapi.get_everything(q = 'us economy', from_parameter = '2017-01-01', to = '2017-12-31')


class ApiController:
    '''this will wrap the following code with the built-in function @lru_cache
    which basically handles the caching'''
    @lru_cache(maxsize = 128) # maxsize = 128 is the default caching size
    def query_api(self, query_argument, query_type, start_date=None, end_date=None):

        valid_date_range = self.validate_dates(start_date, end_date)

        if not valid_date_range:

            if query_type is 'headlines':
                return self.build_articles_list(newsapi.get_top_headlines(q = query_argument))

            elif query_type is 'all':
                return self.build_articles_list(newsapi.get_everything(q = query_argument))

        if valid_date_range and query_type is 'all':

            return self.build_articles_list(newsapi.get_everything(
                q=query_argument, from_parameter=start_date, to=end_date
            ))


    def build_articles_list(self, api_query):

        articles_list = []

        for i in range(len(api_query['articles'])):
            raw_article_data = api_query['articles'][i]
            new_article_object = self.build_article_object(raw_article_data)
            articles_list.append(new_article_object)

        return articles_list


    @staticmethod
<<<<<<< HEAD
    def build_article_object(article_data):
        title = article_data['title']
        author = article_data['author']
        source = article_data['source']['name']
        published_time= article_data['publishedAt']
=======
    def build_article_object(raw_article_data):
        Id = None #this is defined None since it will be auto generated by sqlite as primary key
        cached_data_list = [] #creates an empty list
        query_term = user_interface.get_user_query()
        '''this will hold the list of tuples returned by the function'''
        cached_data = articles_db.get_cached_data()
        '''This will split each element of every tuples in a list(here, in query_term)
        and append them to cached_data_list'''
        for data in cached_data: #for each tuple of 'cached_data' list
            for d in data:#for each element of tuple (that is 'data')
                cached_data_list.append(d)
        '''find if query term present in the list'''
        if query_term in cached_data_list:
            '''if query term found, then process list of tuples to obtain title, author, source,
            and published_time'''
            for i in range(len(cached_data)):
                title = cached_data[i][1]
                author = cached_data[i][2]
                source = cached_data[i][3]
                published_time = cached_data[i][4]
        # return Article(title, author, source, published_time)
        # '''if query term not found in the list(cached_data_list), then execute the following'''
        else:
            title = raw_article_data['title']
            author = raw_article_data['author']
            source = raw_article_data['source']['name']
            published_time= raw_article_data['publishedAt']
            articles_db.add_to_db(Id, title, author, source, published_time)
>>>>>>> 9f4112c57ebc68254392166f62b99ccc54093277
        return Article(title, author, source, published_time)


    @staticmethod
    def validate_dates(start_date, end_date):
        return False
        pass
        # # TODO:  -- this function should actually be in the user_interface - use a datetime object to validate inputs
        # # TODO:  -- should be in YYYY-MM-DD format
