import json


def search_for_game_id(game_name: str):
    """
    Searches all_games_data.json file for occurences of game_name phrase, and saves findings to
    found_games_data.json file.
    :param game_name: string containing name of the game
    :return: list containing found games as well as their id's
    """
    with open('all_games_data.json', 'r', encoding="utf-8") as file:
        parsed = json.load(file)
    games_list = parsed["applist"]["apps"]

    found_games = []
    print(f"Searching for {game_name}")
    # Search for keyword game_name in available application names
    for i in games_list:
        if game_name.lower() in i["name"].lower():
            app_id = i["appid"]
            app_name = i["name"]
            found_games.append([app_name, app_id])
    print("Found:", found_games)
    file_content = {}
    with open("found_games_data.json", "w") as f:
        for game_info in found_games:
            file_content[game_info[0]] = game_info[1]
        json.dump(file_content, f)
    return found_games