import os
import asyncio
from time import time
from functools import _make_key

import aiohttp
import redis

import static_data

API_URL_BASE = 'https://{platform}.api.riotgames.com/{api_url}'
REGIONS = {'BR', 'EUNE', 'EUW', 'JP', 'KR', 'LAN', 'LAS', 'NA', 'OCE', 'TR',
           'RU', 'PBE'}

PLATFORMS = {
    'BR':   'BR1',
    'EUNE': 'EUN1',
    'EUW':  'EUW1',
    'JP':   'JP1',
    'KR':   'KR',
    'LAN':  'LA1',
    'LAS':  'LA2',
    'NA':   'NA',
    'OCE':  'OC1',
    'TR':   'TR1',
    'RU':   'RU',
    'PBE':  'PBE1',
}


def time_based_async_cache(async_method):
    cache = redis.from_url(os.environ['REDIS_URL'])

    async def wrapped_method(self, *args, **kwargs):
        name_and_args = (async_method.__name__,) + tuple(arg for arg in args)
        key = _make_key(name_and_args, kwargs, False)
        cached_result = cache.get(key)
        if cached_result is not None:
            return cached_result.decode('utf-8')
        result = await async_method(self, *args, **kwargs)
        cache.setex(key, result, static_data.LEAGUE_UPDATE_TIMEOUT)
        print('Setting revisionDate:', result)
        return result
    return wrapped_method


class AsyncRateLeagueAPI:
    def __init__(self, api_key):
        self.api_key = api_key
        self.session = aiohttp.ClientSession()

    async def _session_get_simple(self, url, params):
        async with self.session.get(url, params=params) as response:
            return await response.json()

    async def _session_get(self, url, params):
        response = await self._session_get_simple(url, params)
        while response.get('status', dict()).get('status_code') == 429:
            await asyncio.sleep(1)
            response = await self._session_get_simple(url, params)
        return response

    def _request(self, api_url, region, **kwargs):
        api_url = api_url.format(region=region, **kwargs)
        url = API_URL_BASE.format(platform=PLATFORMS[region], api_url=api_url)
        kwargs['api_key'] = self.api_key
        return self._session_get(url, params=kwargs)

    def get_summoner_by_name(self, region, summoner_name):
        url = '/lol/summoner/v3/summoners/by-name/{summonerName}'
        return self._request(url, region, summonerName=summoner_name)

    @time_based_async_cache
    async def get_summoner_revison_date(self, region, summoner):
        summoner = await self.get_summoner_by_name(region, summoner)
        try:
            return summoner['revisionDate']
        except KeyError:
            return None

    async def get_revision(self, region, summoner):
        output = str()
        revision_date = await self.get_summoner_revison_date(region, summoner)
        print('Got revision date: ', revision_date)
        if revision_date not in ['None', None]:
            revision_date = int(revision_date)
            revision_date /= 1000
            days_ago = int((time() - revision_date)//static_data.DAY)
            if days_ago == 0:
                output = 'Today'
            elif days_ago % 10 == 1 and days_ago != 11:
                output = f'{days_ago} day ago'
            else:
                output = f'{days_ago} days ago'
        else:
            output = '*Never (not found)!*'
        return output

@@ -451,16 +451,17 @@
 ':   'NA
+1
 ',%0A    '

Fix for NA