"""
Player API implementation for Malody wrapper.
"""

import requests

class Player:
    """Class for accessing player-related Malody API endpoints"""
    
    BASE_URL = "https://api.mugzone.net"
    HEADERS = {
        'User-Agent': 'MalodyPythonWrapper/1.0'
    }
    
    def info(self, touid, key):
        """
        fetches player info from the malody api.

        args:
            touid = id to search
            key = api key

        returns:
        returns dict if found, if not return none
        """
        
        # build the api url with parameters
        url = f"{self.BASE_URL}/api/player/info?touid={touid}&key={key}&uid=1"
        
        try:
            # send get request with headers
            response = requests.get(url, headers=self.HEADERS)
            # raise an exception for bad status codes (4xx or 5xx)
            response.raise_for_status()
            # parse and return the json data
            return response.json()
        except requests.exceptions.RequestException as e:
            # log error (optional) and return none
            print(f"request failed: {e}")
            return None

    def activity(self, touid, key):
        """
        fetches player activity from the malody api.

        args:
            touid = id to search
            key = api key

        returns:
        returns dict if found, if not return none
        """
        
        # build the api url with parameters
        url = f"{self.BASE_URL}/api/player/activity?touid={touid}&key={key}&uid=1"
        
        try:
            # send get request with headers
            response = requests.get(url, headers=self.HEADERS)
            # raise an exception for bad status codes (4xx or 5xx)
            response.raise_for_status()
            # parse and return the json data
            return response.json()
        except requests.exceptions.RequestException as e:
            # log error (optional) and return none
            print(f"request failed: {e}")
            return None

    def chart(self, touid, key):
        """
        fetches player chart info from the malody api.

        args:
            touid = id to search
            key = api key

        returns:
        returns dict if found, if not return none
        """
        
        # build the api url with parameters
        url = f"{self.BASE_URL}/api/community/player/chart?touid={touid}&key={key}&uid=1"
        
        try:
            # send get request with headers
            response = requests.get(url, headers=self.HEADERS)
            # raise an exception for bad status codes (4xx or 5xx)
            response.raise_for_status()
            # parse and return the json data
            return response.json()
        except requests.exceptions.RequestException as e:
            # log error (optional) and return none
            print(f"request failed: {e}")
            return None

    def rank(self, touid, key):
        """
        fetches player rank info from the malody api.

        args:
            touid = id to search
            key = api key

        returns:
        returns dict if found, if not return none
        """
        
        # build the api url with parameters
        url = f"{self.BASE_URL}/api/ranking/player/all?touid={touid}&key={key}&uid=1"
        
        try:
            # send get request with headers
            response = requests.get(url, headers=self.HEADERS)
            # raise an exception for bad status codes (4xx or 5xx)
            response.raise_for_status()
            # parse and return the json data
            return response.json()
        except requests.exceptions.RequestException as e:
            # log error (optional) and return none
            print(f"request failed: {e}")
            return None

    def wiki(self, touid, key, lang=0, raw=1):
        """
        fetches player wiki info from the malody api.

        args:
            touid = id to search
            key = api key
            lang = language code (default: 0)
            raw = raw format flag (default: 1)

        returns:
        returns dict if found, if not return none
        """
        
        # build the api url with parameters
        url = f"{self.BASE_URL}/api/community/wiki?touid={touid}&lang={lang}&raw={raw}&key={key}&uid=1"
        
        try:
            # send get request with headers
            response = requests.get(url, headers=self.HEADERS)
            # raise an exception for bad status codes (4xx or 5xx)
            response.raise_for_status()
            # parse and return the json data
            return response.json()
        except requests.exceptions.RequestException as e:
            # log error (optional) and return none
            print(f"request failed: {e}")
            return None