import { IpcClient } from "@autoplex-api/torrent";

interface IResponse {
    response?: any,
    error?: string | Error
}

/**
 * Declare EventEmitter types
 */
interface TorrentIpcEvents {
    "connected": () => void
}

/**
 * Torrent IPC events
 */
declare interface TorrentIpc {
    on<U extends keyof TorrentIpcEvents>(event: U, listener: TorrentIpcEvents[U]): this,
    emit<U extends keyof TorrentIpcEvents>(event: U, ...args: Parameters<TorrentIpcEvents[U]>): boolean
}

/**
 * The torrent client IPC service
 */
class TorrentIpc extends IpcClient
{
    /**
     * Install the message event handlers
     */
    protected installMessageHandlers() {
        this.addListener("torrent_finished", this.onTorrentFinished);
    }

    /**
     * Invoked when a torrent has finished downloading
     */
    protected async onTorrentFinished(infoHash: string) {}

    /**
     * Emit an event when connected
     */
    protected onConnect() {
        super.onConnect();
        this.emit("connected");
    }
}

export default TorrentIpc;
