import base64
import json

import requests
import time

from .utils import currenttimemillis_to_python_datetime


__all__= [
    'get_statuses',
    'get_uuid',
    'get_usernames',
    'get_profiles',
    'get_detailed_profile',
    'get_blocked_server_hashes'
]


def get_statuses():
    return requests.get('https://status.mojang.com/check/').json()


def get_uuid(username, unix_timestamp=None):
    if unix_timestamp is None:
        unix_timestamp = int(time.time())
    return requests.get(
        'https://api.mojang.com/users/profiles/minecraft/{}?at={}'.format(username, unix_timestamp)
    ).json()


def get_usernames(uuid):
    username_infos = requests.get('https://api.mojang.com/user/profiles/{}/names'.format(uuid)).json()
    for username_info in username_infos:
        try:
            changed_to_at = username_info['changedToAt']
        except KeyError:
            continue
        else:
            username_info['changedToAt'] = currenttimemillis_to_python_datetime(changed_to_at)
    return username_infos


def get_profiles(*usernames):
    return requests.post(
        url='https://api.mojang.com/profiles/minecraft',
        headers={
            b'Content-Type': b'application/json'
        },
        data=json.dumps(list(usernames))
    ).json()


def get_detailed_profile(uuid):
    response = requests.get('https://sessionserver.mojang.com/session/minecraft/profile/{}'.format(uuid))
    response.raise_for_status()
    body = response.json()
    for property in body['properties']:
        if property['name'] == 'textures':
            skin_info = json.loads(base64.decodebytes(property['value'].encode()).decode())
            timestamp = skin_info['timestamp']
            skin_info['timestamp'] = currenttimemillis_to_python_datetime(timestamp)
            property['value'] = skin_info
    return body


def get_blocked_server_hashes():
    response = requests.get('https://sessionserver.mojang.com/blockedservers')
    response.raise_for_status()
    sha1_hashes = response.content.split(b'\n')
    return sha1_hashes

@@ -254,16 +254,38 @@
 _hashes'
+,%0A    'get_statistics'
 %0A%5D%0A%0A%0Adef
@@ -2076,8 +2076,532 @@
 _hashes%0A
+%0A%0Adef get_statistics(keys=None):%0A    if keys is None:%0A        keys = %5B%0A            'item_sold_minecraft',%0A            'prepaid_card_redeemed_minecraft',%0A            'item_sold_cobalt',%0A            'item_sold_scrolls'%0A        %5D%0A    response = requests.post(%0A        url='https://api.mojang.com/orders/statistics',%0A        headers=%7B%0A            b'Content-Type': b'application/json'%0A        %7D,%0A        data=json.dumps(%7B%0A            'metricKeys': keys%0A        %7D)%0A    )%0A    response.raise_for_status()%0A    return response.json()%0A

Add get statistics call