import json
from collections import Counter
from os import listdir
import pandas as pd
import numpy as np

from os.path import exists

from datetime import datetime

from pymongo import MongoClient, InsertOne

from db import CassandraUtils

import pymongo

SEED_PATH = "/Users/Labtime/Dropbox/Twitter/Friends.Distributions/union.mat"

STR_DATE_FORMAT = "%b %d, %Y %I:%M:%S %p"

_cass = CassandraUtils()


def get_time(to_str=False):
    time = datetime.now()
    if to_str:
        return str(time)
    return time


def parse_date(str_date):
    return datetime.strptime(str_date, STR_DATE_FORMAT)


def handle_friends(friends):
    if friends is None or len(friends) == 0:
        return []
    return [r.friend_id for r in friends]


def handle_likes(likes):
    if likes is not None and len(likes) > 0:
        for like in likes:
            return [like['id'] for like in likes]
    return []


def handle_mentions(mt_tweets):
    vals = []
    if mt_tweets is not None and len(mt_tweets) > 0:
        for tweet in mt_tweets:
            vals.extend([me['id'] for me in tweet['userMentionEntities']])
    return vals


def handle_retweet(rt_tweets=None):
    if rt_tweets is not None and len(rt_tweets) > 0:
        return [tweet['retweetedStatus']['id'] for tweet in rt_tweets]
    return []


def print_(_file, line):
    _file.write('{0}\n'.format(line))


def handle_user(_user):
    likes = handle_likes(_cass.find_likes(_user))
    tweets = _cass.find_tweets(_user)
    mentions = handle_mentions(_cass.find_mentions(tweets=tweets))
    retweets = handle_retweet(_cass.find_retweets(tweets=tweets))

    return {'_id': _user, 'likes': likes, 'retweets': retweets, 'mentions': mentions}


if __name__ == '__main__':
    client = MongoClient()

    db = client['twitter_db']
    # for doc in db.interactions.find({'_id': 10002062}):
    #     print(doc)

    with open(SEED_PATH, 'r') as infile:
        i = 0
        for l in infile.readlines():
            user = int(json.loads(l).keys()[0])
            # db.interactions.insert_one()
            docs = []

            if db.interactions.count({"_id": user}) == 0:
                docs.append(handle_user(_user=user))

            friends = handle_friends(_cass.find_friends(user))
            friends = friends if friends is not None else []
            for friend in friends:
                if db.interactions.count({"_id": friend}) == 0:
                    docs.append(handle_user(_user=friend))

            if len(docs) > 0:
                db.interactions.insertMany(docs)

            print user, get_time(True)
