import os
import tweepy
import json
import yaml
import logging
import concurrent.futures
from datetime import timedelta, datetime

full_path = os.path.dirname(os.path.realpath(__file__))

logging.basicConfig(filename=full_path + '/log.log', level=logging.INFO,
        format='%(asctime)s %(message)s')

logger = logging.getLogger(__name__)

def main():
    logger.info('starting twitterwipe')

    with open(full_path + '/config.yaml', 'r') as yamlfile:
        config = yaml.load(yamlfile, Loader=yaml.FullLoader)

    delete_timestamps = get_delete_timestamps(config)
    purge_activity(delete_timestamps)

    logger.info('done')


def get_delete_timestamps(config):
    curr_dt_utc = datetime.utcnow()

    days = config['days_to_save']
    likes = days['likes']
    retweets = days['retweets']
    tweets = days['tweets']

    likes_time = None
    retweets_time = None
    tweets_time = None

    if likes >= 0:
        likes_delta = timedelta(days=likes)
        likes_time = curr_dt_utc - likes_delta
    else:
        likes_time = datetime.min

    if retweets >= 0:
        retweets_delta = timedelta(days=retweets)
        retweets_time = curr_dt_utc - retweets_delta
    else:
        retweets_time = datetime.min

    if tweets >=0:
        tweets_delta = timedelta(days=tweets)
        tweets_time = curr_dt_utc - tweets_delta
    else:
        tweets_time = datetime.min

    return (likes_time, retweets_time, tweets_time)


def purge_activity(delete_timestamps):
    api = get_api()

    with concurrent.futures.ThreadPoolExecutor(max_workers=3) as e:
        e.submit(delete_tweets, api, delete_timestamps[2])
        e.submit(delete_retweets, api, delete_timestamps[1])
        e.submit(delete_favorites, api, delete_timestamps[0])


def delete_tweets(api, ts):
    logger.info('deleting tweets before {}'.format(str(ts)))

    count = 0

    for status in tweepy.Cursor(api.user_timeline).items():
        if status.created_at < ts:
            try:
                api.destroy_status(status.id)
                count += 1
            except Exception as e:
                logger.error("failed to delete {}".format(status.id), exc_info=True)

    logger.info('{} tweets deleted'.format(count))

    return


def delete_retweets(api, ts):
    logger.info('deleting retweets before {}'.format(str(ts)))

    count = 0

    for status in tweepy.Cursor(api.user_timeline).items():
        if status.created_at < ts:
            try:
                api.unretweet(status.id)
                count+=1
            except:
                logger.error('failed to unretweet {}'.format(status.id),exc_info=True)
    logger.info('{} retweets deleted'.format(count))


    return


def delete_favorites(api, ts):
    logger.info('deleting favorites before {}'.format(str(ts)))

    count = 0

    for status in tweepy.Cursor(api.favorites).items():
        if status.created_at < ts:
            try:
                api.destroy_favorite(status.id)
                count+=1
            except:
                logger.error('failed to delete favorite'.format(status.id), exc_info=True)

    logger.info('{} favorites deleted'.format(count))

    return


def get_api( ):

    try:
        # get keys from os environment variables if they exist
        d = { 'consumer_key' : os.environ['CONSUMER_KEY'],
              'consumer_secret' : os.environ['CONSUMER_SECRET'],
              'app_key' : os.environ['APP_KEY'],
              'app_secret' :os.environ['APP_SECRET'] }
    except KeyError:
        # environment variables are not found, so try the json file instead
        with open(full_path + '/keys.json', 'r') as f:
            d = json.load(f)

    auth = tweepy.OAuthHandler(d['consumer_key'], d['consumer_secret'])
    auth.set_access_token(d['app_key'], d['app_secret'])

    return tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)


if __name__ == '__main__':
    main()
