#! /usr/bin/env python
import os
import time

import tweepy
from didyoumean3.didyoumean import did_you_mean
from tweepy import Cursor


class Tweeper(object):
    def __init__(self, name: str):
        auth = tweepy.OAuthHandler(
            os.environ["ZOO_DAWA_KEY"],
            os.environ["ZOO_DAWA_KEY_SECRET"])
        auth.set_access_token(
            os.environ["ZOO_DAWA_TOKEN"],
            os.environ["ZOO_DAWA_TOKEN_SECRET"])
        self.api = tweepy.API(auth)
        self.name = name

    @property
    def all_tweets(self):
        return [t.text for t in Cursor(self.api.user_timeline, id=self.name).items()]

    def tweet(self, message, wait_delay=5, prevent_duplicate=True):
        """Tweets a message after spellchecking it."""
        if prevent_duplicate and message in self.all_tweets:
            print("Was already tweeted: %s." % message)
        else:
            message = did_you_mean(message)
            print("About to tweet:", message)
            time.sleep(wait_delay)
            self.api.update_status(message)