const RPC = require("discord-rpc")

class DiscordRPC {
    constructor() {
        readonly: this.activity = {
            details: "Actif dans le launcher",
            assets: {
                large_image: "logo",
                large_text: "CatBoat Minecraft Launcher"
            },
            buttons: [
                {
                    label: "Discord",
                    url: "https://discord.com/invite/catboat"
                },
                {
                    label: "CatBoat",
                    url: "https://catboat.fr/"
                }
            ],
            instance: true
        }
        readonly: this.client = new RPC.Client({ transport: "ipc" })
        this.isEnabled = false
    }

    startRichPresence() {
        if (!this.isEnabled) {
            this.client.on("ready", () => {
                this.client.request("SET_ACTIVITY", { pid: process.pid, activity: this.activity })
                console.log("The Discord Rich Presence has been set successfully.")
            })
            this.client.login({ clientId: "1365563093157154868" }).catch(e => {
                console.log(e)
                console.log("Silent client detected: the activity status has been disabled.")
                this.isEnabled = false
            }).then(r => this.isEnabled = true)
            .catch(err => console.log)
        }
    }

    stopRichPresence() {
        if (this.isEnabled) {
            this.client.destroy()
            this.isEnabled = false
        }
    }
}

module.exports = new DiscordRPC()