from datetime import datetime
from typing import TYPE_CHECKING

from pyot.conf.model import models
from pyot.core.functional import empty
from .base import PyotCore

if TYPE_CHECKING:
    from .league import SummonerLeague
    from .thirdpartycode import ThirdPartyCode
    from .profileicon import ProfileIcon
    from ..riot.account import Account
    from .match import MatchHistory


# PYOT CORE OBJECTS

class Summoner(PyotCore):
    name: str
    id: str
    account_id: str
    level: int
    puuid: str
    profile_icon_id: int
    revision_date_millis: int

    class Meta(PyotCore.Meta):
        rules = {
            "summoner_v1_by_id": ["id"],
            "summoner_v1_by_account_id": ["account_id"],
            "summoner_v1_by_puuid": ["puuid"],
            "summoner_v1_by_name": ["name"],
        }
        renamed = {"summoner_level": "level", "revision_date": "revision_date_millis"}

    def __init__(self, id: str = empty, account_id: str = empty, name: str = empty, puuid: str = empty, platform: str = models.tft.DEFAULT_PLATFORM):
        self.initialize(locals())

    @property
    def revision_date(self) -> datetime:
        return datetime.fromtimestamp(self.revision_date_millis//1000)

    @property
    def league_entries(self) -> "SummonerLeague":
        from .league import SummonerLeague
        return SummonerLeague(summoner_id=self.id, platform=self.platform)

    @property
    def third_party_code(self) -> "ThirdPartyCode":
        from .thirdpartycode import ThirdPartyCode
        return ThirdPartyCode(summoner_id=self.id, platform=self.platform)

    @property
    def profile_icon(self) -> "ProfileIcon":
        from .profileicon import ProfileIcon
        return ProfileIcon(id=self.profile_icon_id)

    @property
    def account(self) -> "Account":
        from ..riot.account import Account
        return Account(puuid=self.puuid).using(self.metapipeline.name)

    @property
    def match_history(self) -> "MatchHistory":
        from .match import MatchHistory
        return MatchHistory(puuid=self.puuid, region=self.region)
