import csv
import datetime
from config import config
from utils import scopus_query_request, scopus_query, get_next_link

"""
Collect stats on AI papers and AI papers in computer science (a subset) over
many years.
"""


def query_over_time(query_items, start_year=1900, end_year=2021):

    results = []

    api_key = config.api_key

    tmp_query_items = query_items
    if start_year != "":
        tmp_query_items = tmp_query_items + ["PUBYEAR AFT %d" % (start_year)]

    if end_year != "":
        tmp_query_items = tmp_query_items + ["PUBYEAR BEF %d" % (end_year)]

    query_string = " AND ".join(tmp_query_items)
    count = config.count

    if count > 0:
        response = scopus_query_request(query_string, api_key, count)
    else:
        response = scopus_query_request(query_string, api_key)

    responseData = response.json()
    print(responseData)
    total_count = int(responseData.get("search-results").get("opensearch:totalResults"))
    entityArray = responseData.get('search-results').get('entry')
    print("X-%d:" % start_year, total_count)

    while total_count > 0:
        for item in entityArray:
            data = {
                "title": item['dc:title'],
                "authors": item.get('dc:creator'),
                "publication": item.get('prism:publicationName'),
                "volume": item.get('prism:volume'),
                "page": item.get('prism:pageRange'),
                "date": item.get('prism:coverDate'),
                "cited": item.get('citedby-count')
            }
            results.append(data)
            total_count = total_count - 1

            #print("%s" % (data))

        links = responseData.get('search-results').get('link')
        print(links)
        print(total_count)
        next = get_next_link(links)
        if next != "":
            response = scopus_query(next)
            responseData = response.json()
            # count = responseData.get("search-results").get("opensearch:totalResults")
            entityArray = responseData.get('search-results').get('entry')
        else:
            break

    print()

    return results


def create_ai_papers_csv(
    filename,
    ai_data,
    start_year=1900
):
    year = start_year
    with open(filename, 'w', encoding='utf-8') as csvfile:
        papers_writer = csv.writer(csvfile, delimiter=',')
        papers_writer.writerow(["title", "authors", "publication", "volume", "page", "date", "cited"])
        for item in ai_data:
            papers_writer.writerow([item['title'], item['authors'], item['publication'], item['volume'], item['page'], item['date'], item['cited']])
            year += 1


if __name__ == "__main__":
    filename = config.filename
    start_year = config.start_year
    if config.end_year == "":
        end_year = datetime.datetime.now().year
    else:
        end_year = config.end_year

    title_abs_key_terms = config.search_terms

    ai_query = [" AND ".join("%s" %
                            term for term in title_abs_key_terms)]
    cs_query = []
    total_query = []

    ai_data = query_over_time(ai_query, start_year=start_year, end_year=end_year)

    create_ai_papers_csv(
        filename,
        ai_data,
        start_year=start_year+1
    )
