import json # JSON tools
import requests # URL api tools
import re # regex tools
from .champion_info import convert_champion_alias, champion_id_from_name

def query_wiki(year, region, tournament):
    """
    query_wiki takes identifying sections and subsections for a page title on leaguepedia and formats and executes a set of requests to the
    API looking for the pick/ban data corresponding to the specified sections and subsections. This response is then
    pruned and formatted into a list of dictionaries. Specified sections and subsections should combine into a unique identifying string
    for a specific tournament and query_wiki() will return all games for that tournament.

    For example, if we are interested in the regular season of the 2017 European Summer Split we would call:
    query_wiki("2017", "EU_LCS", "Summer_Season")

    If we were interested in 2016 World Championship we would pass:
    query_wiki("2016", "International", "WORLDS/Main_Event")

    Each dictionary corresponds to the pick/ban phase of an LCS game with the following keys:
        "region":
        "season":
        "tournament":
        "bans": {"blue":, "red":}
        "blue_team":
        "blue_team_score"
        "red_team":
        "red_team_score:"
        "tourn_game_id":
        "picks": {"blue":, "red":}

    Args:
        year (string): year of game data of interest
        region (string): region of play for games
        tournament (string): which tournament games were played in
    Returns:
        List of dictionaries containing formatted response data from lol.gamepedia api
    """
    # Common root for all requests
    url_root = "https://lol.gamepedia.com/api.php"

    # Semi-standardized page suffixes for pick/ban pages
    page_suffixes = ["", "/Bracket_Stage", "/3-4", "/5-6", "/5-8", "/4-6", "/4-7", "/7-9", "/7-10", "/8-10", "/7-8", "/9-11"]

    formatted_regions = {"NA_LCS":"League_Championship_Series/North_America",
                        "NA_ACA":"NA_Academy_League",
                        "EU_LCS":"League_Championship_Series/Europe",
                        "LCK":"LCK",
                        "KR_CHAL":"Challengers_Korea",
                        "LPL":"LPL",
                        "LMS":"LMS",
                        "LDL":"LDL"}

    formatted_international_tournaments = {
                        "WORLDS/Play-In": "Season_World_Championship/Play-In",
                        "WORLDS/Main_Event": "Season_World_Championship/Main_Event",
                        "MSI/Play-In": "Mid-Season_Invitational/Play-In",
                        "MSI/Main_Event": "Mid-Season_Invitational/Main_Event",
                        "WORLDS_QUALS/NA": "Season_North_America_Regional_Finals",
                        "WORLDS_QUALS/EU": "Season_Europe_Regional_Finals",
                        "WORLDS_QUALS/LCK": "Season_Korea_Regional_Finals",
                        "WORLDS_QUALS/LPL": "Season_China_Regional_Finals",
                        "WORLDS_QUALS/LMS": "Season_Taiwan_Regional_Finals",
    }

    with open('../data/patch_info.json','r') as infile:
        patch_data = json.load(infile)
        patches = patch_data["patch_info"][year][region][tournament]
        print(patches)

    # Build list of titles of pages to query
    if region == "International":
        title_root = ["_".join([year,formatted_international_tournaments[tournament]])]