import user_agents
import random
import requests
import re
import os
import json
import concurrent.futures


def playerArchives(username):
    # Create archive list. This is a list of pages containing lists of games used for futher downloading

    user_agent = random.choice(user_agents.user_agents) # load up random user agent from list
    headers = {
            'User-Agent':user_agent
            }
    url = f"https://api.chess.com/pub/player/{username}/games/archives"
    archive = requests.get(url, headers=headers).json()['archives']

    try:
        cwd = os.getcwd()
        path = os.path.join(cwd, 'archives')
        os.makedirs(path)
    except OSError:
        pass

    file_name = os.path.join(path, username+'_archive.txt')
    with open(file_name, 'w') as f:
        json.dump(archive, f)

    return archive


def playerMonthly(url=None):

    user_agent = random.choice(user_agents.user_agents) # load up random user agent from list
    headers = {
            'User-Agent':user_agent
            }

    if url:
        url=url+'/pgn' # connect to multi-game pgn download endpoint by appending a "pgn" to the url provided by url in archive list
    else:
        username=input("username: ")
        YYYY=input("year in YYYY: ")
        MM=input("month in MM: ") 
        url = f"https://api.chess.com/pub/player/{username}/games/{YYYY}/{MM}/pgn"

    # get and save games list in .pgn format
    games = requests.get(url, headers=headers).content.decode("utf-8")

    return games

# Multithreaded games download
def threddGames(username=None, archive=None):

    if archive:
        with open(archive) as f:
            archive = json.load(f)
    else:
        archive = playerArchives(username)

    # async download games
    games_list = []
    with concurrent.futures.ThreadPoolExecutor() as executor:
        for future in executor.map(playerMonthly, archive):
            games_list.extend(future)
    pgn_db = "\n\n".join(games_list)
    with open(username+'.pgn', 'w') as f:
        f.write(pgn_db)

    return pgn_db

