"""
Methods to extract data from Twitter
"""

# Twitter
TWITTER_TOKEN = {
    'TOKEN'           : "3571004715-NiOnRpRrQZVGpQFvgT2B5zYB6vm3ey01ZBk9QT9",
    'SECRET'          : "WhKkqFzshpFLzRIsS9puPTVZZgKYWhOYcf8JPcAbBFKMI",
    'CONSUMER_KEY'    : "zuDY4LEW37TCesUfObKMeMBPf",
    'CONSUMER_SECRET' : "os60OvpWjb9TLW1ABaiZeRZy8QWcOfwknwYGLBgJOGBE5tQfrM"
}

def search_tweets(lat=None, lng=None, radius=None, keyword=None,
                  NR_ITEMS=500, only_geo=None):
    """
    Basic tool to extract data from Twitter using a keyword and/or a buffer
    
    * radius should be in Km
    
    Returns an array with the encountered data
    """
    
    import tweepy
    import pandas
    
    from gasp.encoding import unicode_to_str
    
    # Give our credentials to the Twitter API
    auth = tweepy.OAuthHandler(
        TWITTER_TOKEN['CONSUMER_KEY'], TWITTER_TOKEN['CONSUMER_SECRET']
    )
    
    auth.set_access_token(
        TWITTER_TOKEN['TOKEN'], TWITTER_TOKEN['SECRET']
    )
    
    api = tweepy.API(auth)
    
    # Request data from twitter
    if not keyword:
        keyword = ''
    
    else:
        if type(keyword) == unicode:
            keyword = unicode_to_str(keyword)
    
    if not lat or not lng or not radius:
        data = [i._json for i in tweepy.Cursor(
            api.search, q=keyword
        ).items(NR_ITEMS)]
    
    else:
        __geostr = '{_lat},{_lng},{r}km'.format(
            _lat=str(lat), _lng=str(lng), r=str(radius)
        )
        
        data = [i._json for i in tweepy.Cursor(
            api.search, q=keyword, geocode=__geostr
        ).items(NR_ITEMS)]
    
    data = pandas.DataFrame(data)
    
    if not data.shape[0]:
        return None
    
    data["fid"] = data["id"]
    
    cols = list(data.columns.values)
    delCols = []
    
    for col in cols:
        if col != 'user' and col != 'text' and col != 'fid' and col != 'geo':
            delCols.append(col)
    
    data.drop(delCols, axis=1, inplace=True)
    
    dfGeom = data[data["geo"].astype(str) != 'None']
    
    if only_geo and not dfGeom.shape[0]:
        return None
    
    elif not only_geo and not dfGeom.shape[0]:
        result = data
        
        result["latitude"]  = result["geo"]
        result["longitude"] = result["geo"]
        result.drop("geo", axis=1, inplace=True)
    
    else:
        dfGeom = pandas.concat([
            dfGeom.drop(["geo"], axis=1),
            dfGeom["geo"].apply(pandas.Series)
        ], axis=1)
        
        dfGeom = pandas.concat([
            dfGeom.drop(["coordinates"], axis=1),
            dfGeom["coordinates"].apply(pandas.Series)
        ], axis=1)
        
        dfGeom.rename(columns={0 : 'latitude', 1 : 'longitude'}, inplace=True)
        
        dfGeom.drop("type", axis=1, inplace=True)
        
        if only_geo:
            result = dfGeom
        
        else:
            dfNoGeom = data[data["geo"].astype(str) == 'None']
            dfNoGeom["latitude"] = dfNoGeom["geo"]
            dfNoGeom["longitude"] = dfNoGeom["geo"]
            
            dfNoGeom.drop("geo", axis=1, inplace=True)
            
            result = dfGeom.append(dfNoGeom, ignore_index=True)
    
    result = pandas.concat([
        result.drop(["user"], axis=1),
        result["user"].apply(pandas.Series)
    ], axis=1)
    
    result.rename(columns={'screen_name' : 'user'}, inplace=True)
    
    result["url"] = 'https://twitter.com/' + \
        result["user"].astype(str) + '/status/' + \
        result["fid"].astype(str)
    
    return result


def tweets_to_json(lat, lng, radius, keyword, jsonfile,
                   NR_ITEMS=500, ONLY_GEO=None):
    """
    Search for tweets and save them in a json file
    """

    import tweepy
    import json

    # Give our credentials to the Twitter API
    auth = tweepy.OAuthHandler(
        TWITTER_TOKEN['CONSUMER_KEY'], TWITTER_TOKEN['CONSUMER_SECRET']
    )

    auth.set_access_token(
        TWITTER_TOKEN['TOKEN'], TWITTER_TOKEN['SECRET']
    )

    api = tweepy.API(auth_handler=auth)

    if not keyword:
        keyword=''

    data = search_tweets(lat=lat, lng=lng, radius=float(radius)/1000.0, keyword=keyword,
                         NR_ITEMS=NR_ITEMS, only_geo=ONLY_GEO)

    with open(jsonfile, mode='w') as f:
        json.dump(data, f, encoding='utf-8')

    return jsonfile

