import requests
import json
import pandas as pd

def download_draft_json(draft_id):
    url = "https://aoe2cm.net/api/draft/{}".format(draft_id)

    payload={}
    headers = {}

    response = requests.request("GET", url, headers=headers, data=payload)

    # print(response.text)
    draft_json_string = response.text
    draft_dict = json.loads(draft_json_string)
    return draft_dict

def get_picked_maps(draft_json):
    draft_events = draft_json['events']
    draft_events_df = pd.DataFrame.from_dict(draft_events)
    draft_picks = draft_events_df[draft_events_df['actionType'] == 'pick']
    return draft_picks

# the last pick is the neutral map
def neutral_map_as_string(draft_picks):
    neutral_map = draft_picks.tail(1)['chosenOptionId'].values[0]
    return neutral_map