from cod_api import API, platforms
import os
import asyncio
import json

# Configure asyncio for Windows
if os.name == 'nt':
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

COOKIE_FILE = 'cookie.txt'
STATS_DIR = 'dev/'

api = API()

def save_to_file(data, filename):
    """Save data to a JSON file."""
    file_path = os.path.join(STATS_DIR, filename)
    with open(file_path, 'w') as json_file:
        json.dump(data, json_file, indent=4)
    print(f"Data saved to {file_path}")

def ensure_directories_exist():
    if not os.path.exists(STATS_DIR):
        os.makedirs(STATS_DIR)

def get_api_key():
    if os.path.exists(COOKIE_FILE):
        with open(COOKIE_FILE, 'r') as f:
            return f.read().strip()
    else:
        api_key = input("Please enter your ACT_SSO_COOKIE: ")
        with open(COOKIE_FILE, 'w') as f:
            f.write(api_key)
        return api_key

# def doc():
    # print(api.Me.__doc__)
    # print(api.Shop.__doc__)
    # print(api.Misc.__doc__)
    # print(api.ModernWarfare.__doc__)
    # print(api.Warzone.__doc__)
    # print(api.ColdWar.__doc__)
    # print(api.Vanguard.__doc__)
    # print(api.ModernWarfare2.__doc__)

def get_mw_data(player_name):
    api_key = get_api_key()

    api.login(api_key)

    player_stats = api.ModernWarfare.fullData(platforms.Activision, player_name)
    match_info = api.ModernWarfare.combatHistory(platforms.Activision, player_name)
    season_loot_data = api.ModernWarfare.seasonLoot(platforms.Activision, player_name)
    identities_data = api.Me.loggedInIdentities()
    map_list = api.ModernWarfare.mapList(platforms.Activision)
    details = api.ModernWarfare.matchInfo(platforms.Battlenet, 4543631618795926894)

    save_to_file(details, 'details.json')
    save_to_file(player_stats, 'stats.json')
    save_to_file(match_info, 'match_info.json')
    save_to_file(season_loot_data, 'season_loot.json')
    save_to_file(map_list, 'map_list.json')
    save_to_file(identities_data, 'identities.json')

    user_info_file = os.path.join('userInfo.json')
    if os.path.exists(user_info_file):
        info = api.Me.info()
        friend_feed = api.Me.friendFeed()
        event_feed = api.Me.eventFeed()
        cod_points = api.Me.codPoints()
        connected_accounts = api.Me.connectedAccounts()
        settings = api.Me.settings()

        save_to_file(info, 'info.json')
        save_to_file(friend_feed, 'friendFeed.json')
        save_to_file(event_feed, 'eventFeed.json')
        save_to_file(cod_points, 'cp.json')
        save_to_file(connected_accounts, 'connectedAccounts.json')

def get_game_data(player_name, game):
    api_key = get_api_key()

    api.login(api_key)

    game_api = getattr(api, game, None)

    if game_api is None:
        print(f"Error: The game '{game}' is not supported.")
        return

    # Fetch game data
    player_stats = game_api.fullData(platforms.Activision, player_name)
    match_info = game_api.combatHistory(platforms.Activision, player_name)
    # season_loot_data = game_api.seasonLoot(platforms.Activision, player_name)
    # identities_data = api.Me.loggedInIdentities()
    # map_list = game_api.mapList(platforms.Activision)
    details = api.ModernWarfare.matchInfo(platforms.Battlenet, 4543631618795926894)
    save_to_file(player_stats, 'stats.json')
    save_to_file(match_info, 'match_info.json')
    save_to_file(details, 'details.json')
    # save_to_file(season_loot_data, 'season_loot.json')
    # save_to_file(map_list, 'map_list.json')
    # save_to_file(identities_data, 'identities.json')

def main():
    player_name = 'Ahrimdon'
    game = 'ModernWarfare'

    ensure_directories_exist()
    get_api_key()
    # doc()
    # get_mw_data(player_name)
    get_game_data(player_name, game)

if __name__ == "__main__":
    main()
