from __future__ import annotations
from typing import Union
from .struct import *
import aiohttp


class Get:

    def __init__(self, main) -> None:
        self.main = main
        self.session = aiohttp.ClientSession()

        self.request_types = {
            'get_player': Player,
            'get_player_bans': PlayerBans,
            'vanityurl_to_steamid': ConvertedURL
        }

        self.request_key = {
            'get_player': ['response', 'players'],
            'get_friendlist': ['friendslist', 'friends'],
            'get_achievements': ['playerstats', 'achievements'],
            'get_game_user_stats': ['playerstats', 'stats'],
            'get_owned_games': ['response', 'games'],
            'get_recently_played': ['response', 'games'],
            'get_player_bans': ['players'],
            'vanityurl_to_steamid': ['response']
        }

        self.special_process = {
            'get_friendlist': self.friendlist_process,
            'get_achievements': self.achievements_process,
            'get_game_user_stats': self.game_user_stats_process,
            'get_owned_games': self.owned_games_process,
            'get_recently_played': self.recently_played_process
        }

    async def process(self) -> Union[ConvertedURL, PlayerBans, Player, list]:
        request_url = f'{self.main.base_url}{self.main.endpoint}'

        async with self.session.get(request_url) as resp:

            resp = await resp.json()
            request_key = self.request_key[self.main.request]
            resp_fetch = resp
            for key in request_key:
                resp_fetch = resp_fetch[key]

            if self.main.request not in self.special_process:
                try:
                    resp_fetch = resp_fetch[0]
                except:
                    pass

                request_class = self.request_types[self.main.request]

                return request_class(**resp_fetch)

            else:
                return self.special_process[self.main.request](resp_fetch)

    def friendlist_process(self, friendlist: list) -> list:
        friendlist_return = []

        for dict_object in friendlist:
            friendlist_return.append(Friend(**dict_object))

        return friendlist_return

    def achievements_process(self, achievements: list) -> list:
        achievements_return = []

        for dict_obj in achievements:
            achievements_return.append(Achievement(**dict_obj))

        return achievements_return

    def game_user_stats_process(self, user_stats: list) -> list:
        user_stats_return = []

        for dict_obj in user_stats:
            user_stats_return.append(GameStat(**dict_obj))

        return user_stats_return

    def owned_games_process(self, owned_games: list) -> list:
        owned_games_return = []

        for dict_obj in owned_games:
            owned_games_return.append(OwnedGame(**dict_obj))

        return owned_games_return

    def recently_played_process(self, recently: list) -> list:
        recently_return = []

        for dict_obj in recently:
            recently_return.append(RecentlyPlayedGame(**dict_obj))

        return recently_return
