"""AFL AFLTables player model."""

# pylint: disable=line-too-long,duplicate-code,too-many-arguments,too-many-locals
import datetime
import os
from urllib.parse import urlparse

import pytest_is_running
import requests_cache

from ....cache import MEMORY
from ...player_model import PlayerModel


def _create_afl_afltables_player_model(
    player_url: str,
    jersey: str,
    kicks: int | None,
    name: str,
    marks: int | None,
    handballs: int | None,
    disposals: int | None,
    goals: int | None,
    behinds: int | None,
    hit_outs: int | None,
    tackles: int | None,
    rebounds: int | None,
    insides: int | None,
    clearances: int | None,
    clangers: int | None,
    free_kicks_for: int | None,
    free_kicks_against: int | None,
    brownlow_votes: int | None,
    contested_possessions: int | None,
    uncontested_possessions: int | None,
    contested_marks: int | None,
    marks_inside: int | None,
    one_percenters: int | None,
    bounces: int | None,
    goal_assists: int | None,
    percentage_played: float | None,
) -> PlayerModel:
    o = urlparse(player_url)
    last_component = o.path.split("/")[-1]
    identifier, _ = os.path.splitext(last_component)
    jersey = "".join(filter(str.isdigit, jersey))
    return PlayerModel(
        identifier=identifier,
        jersey=jersey,
        kicks=kicks,
        fumbles=None,
        fumbles_lost=None,
        field_goals=None,
        field_goals_attempted=None,
        offensive_rebounds=None,
        assists=None,
        turnovers=None,
        name=name,
        marks=marks,
        handballs=handballs,
        disposals=disposals,
        goals=goals,
        behinds=behinds,
        hit_outs=hit_outs,
        tackles=tackles,
        rebounds=rebounds,
        insides=insides,
        clearances=clearances,
        clangers=clangers,
        free_kicks_for=free_kicks_for,
        free_kicks_against=free_kicks_against,
        brownlow_votes=brownlow_votes,
        contested_possessions=contested_possessions,
        uncontested_possessions=uncontested_possessions,
        contested_marks=contested_marks,
        marks_inside=marks_inside,
        one_percenters=one_percenters,
        bounces=bounces,
        goal_assists=goal_assists,
        percentage_played=percentage_played,
    )


@MEMORY.cache
def _cached_create_afl_afltables_player_model(
    player_url: str,
    jersey: str,
    kicks: int | None,
    name: str,
    marks: int | None,
    handballs: int | None,
    disposals: int | None,
    goals: int | None,
    behinds: int | None,
    hit_outs: int | None,
    tackles: int | None,
    rebounds: int | None,
    insides: int | None,
    clearances: int | None,
    clangers: int | None,
    free_kicks_for: int | None,
    free_kicks_against: int | None,
    brownlow_votes: int | None,
    contested_possessions: int | None,
    uncontested_possessions: int | None,
    contested_marks: int | None,
    marks_inside: int | None,
    one_percenters: int | None,
    bounces: int | None,
    goal_assists: int | None,
    percentage_played: float | None,
) -> PlayerModel:
    return _create_afl_afltables_player_model(
        player_url,
        jersey,
        kicks,
        name,
        marks,
        handballs,
        disposals,
        goals,
        behinds,
        hit_outs,
        tackles,
        rebounds,
        insides,
        clearances,
        clangers,
        free_kicks_for,
        free_kicks_against,
        brownlow_votes,
        contested_possessions,
        uncontested_possessions,
        contested_marks,
        marks_inside,
        one_percenters,
        bounces,
        goal_assists,
        percentage_played,
    )


def create_afl_afltables_player_model(
    player_url: str,
    jersey: str,
    kicks: int | None,
    dt: datetime.datetime,
    session: requests_cache.CachedSession,
    name: str,
    marks: int | None,
    handballs: int | None,
    disposals: int | None,
    goals: int | None,
    behinds: int | None,
    hit_outs: int | None,
    tackles: int | None,
    rebounds: int | None,
    insides: int | None,
    clearances: int | None,
    clangers: int | None,
    free_kicks_for: int | None,
    free_kicks_against: int | None,
    brownlow_votes: int | None,
    contested_possessions: int | None,
    uncontested_possessions: int | None,
    contested_marks: int | None,
    marks_inside: int | None,
    one_percenters: int | None,
    bounces: int | None,
    goal_assists: int | None,
    percentage_played: float | None,
) -> PlayerModel:
    """Create a player model from AFL Tables."""
    if (
        not pytest_is_running.is_running()
        and dt < datetime.datetime.now() - datetime.timedelta(days=7)
    ):
        return _cached_create_afl_afltables_player_model(
            player_url,
            jersey,
            kicks,
            name,
            marks,
            handballs,  # pyright: ignore
            disposals,
            goals,
            behinds,
            hit_outs,
            tackles,
            rebounds,
            insides,
            clearances,
            clangers,
            free_kicks_for,
            free_kicks_against,
            brownlow_votes,
            contested_possessions,
            uncontested_possessions,
            contested_marks,
            marks_inside,
            one_percenters,
            bounces,
            goal_assists,
            percentage_played,
        )
    with session.cache_disabled():
        return _create_afl_afltables_player_model(
            player_url,
            jersey,
            kicks,
            name,
            marks,
            handballs,
            disposals,
            goals,
            behinds,
            hit_outs,
            tackles,
            rebounds,
            insides,
            clearances,
            clangers,
            free_kicks_for,
            free_kicks_against,
            brownlow_votes,
            contested_possessions,
            uncontested_possessions,
            contested_marks,
            marks_inside,
            one_percenters,
            bounces,
            goal_assists,
            percentage_played,
        )
