# function to display cool(?) statistics about the subtitle

import datetime
from dateutil.relativedelta import relativedelta
from alive_progress import alive_bar
from pymongo import MongoClient

# 1. get minimum date
# 2. get number of document that fulfill the criteria for each month starting from min month
# 3. display on graph or put into csv for easy R implementation

def main():
    # Connect to database
    mClient = MongoClient("mongodb://localhost:27017/")
    mDatabase = mClient["test"]
    mCollection = mDatabase["crossref"]

    # Get minimum date
    minimumDateQuery = mCollection.find().sort("dateCreated").limit(1)
    # Get documents sorted by date
    sortedCollectionQuery = mCollection.find().sort("dateCreated")
    # And the count
    countQuery = mCollection.estimated_document_count()


    minimumDate = minimumDateQuery[0]["dateCreated"]

    #print("done with process")
    #print(found)

    #print(found[0]["dateCreated"])

    dTMinimumDate = datetime.datetime.strptime(minimumDate, "%Y-%m-%dT%H:%M:%SZ")
    print(dTMinimumDate)

    statistics = []
    statsToAdd = [0]
    currentDate = dTMinimumDate + relativedelta(months = 1)

    with alive_bar(countQuery) as bar: 
        for item in sortedCollectionQuery:
            itemDate = item["dateCreated"]
            if itemDate <= currentDate:
                if "subtitle" in item.keys():
                    statsToAdd[0] = statsToAdd[0] + 1
                else:
                    statistics.append(statsToAdd)
                    statsToAdd = []
                    currentDate = currentDate + relativedelta(months = 1)
                bar()

            #if not past a month past current month:
                #add to array
            #else
                #add array to statistics
                #increase currentdate by a month

        # currently unfinished. Need to finish the statistics insertion part of the method, and actually do something with that data.

main()