import psutil

from pypresence import Presence
from fivemserver.get_server_token import GetServerTokenForDiscord
from config.constants import Urls
from discord.discord_oauth import CLIENT_ID


class DiscordToken:
    """
    Décode le token discord
    """
    @staticmethod
    def decode_discord_token():
        discord_token = GetServerTokenForDiscord.get_token(
            GetServerTokenForDiscord.authenticate(Urls.API_URL.value)
        )
        return discord_token


class CheckDiscord:
    @staticmethod
    def isdiscordrunning() -> bool:
        """
        Vérifie si Discord est en cours d'exécution sur l'ordinateur. (Vérifie aussi pour Linux)
        """
        for process in psutil.process_iter(["name"]):
            if (
                process.info["name"].lower() == "discord.exe"
                or process.info["name"].lower() == "discordcanary.exe"
                or process.info["name"].lower() == "discord"
                or process.info["name"].lower() == "discord canary"
            ):
                return True
        return False

    @staticmethod
    def isuserconnected() -> bool:
        """
        Vérifie si l'utilisateur Discord est connecté.
        ⚠️ne vérifie pas le user id discord.
        """
        rpc = Presence(CLIENT_ID)
        try:
            rpc.connect()
            return True
        except Exception as e:
            return False
        finally:
            try:
                rpc.close()
            except Exception as e:
                pass
