import logging

from gamescrapyr.gamescrapyr import GameChangerClient

# This retrieves a Python logging instance (or creates it)
logger = logging.getLogger(__name__)

url = "https://gc.com/t/{season_id}/{team_slug}-{team_id}/{page}"


def get_gamechanger_client(request):
    managed_team = request.user.gamechanger_preferences.managed_team

    if (
        not request.session.get("gamechanger_client")
        or not request.session["gamechanger_client"].is_authorized()
    ):
        logger.info("GameChanger client not found or not authorized, creating...")
        gc_username = request.user.gamechanger_account.user
        gc_password = request.user.gamechanger_account.password
        client = GameChangerClient(
            email=gc_username,
            password=gc_password,
            team_id=managed_team.id,
            team_slug=managed_team.slug,
            season_slug=managed_team.season_slug,
        )
        request.session["gamechanger_client"] = client
    else:
        logger.info("GameChanger client found and authorized, loading...")
        client = request.session["gamechanger_client"]

    return client
