import { SOCKET_PATH } from "@autoplex-api/seeker";
import { IpcServerService } from "@autoplex/ipc";
import { MovieTicket } from "@autoplex/database";
import Supervisor from "./Supervisor";

export default class IpcInterface extends IpcServerService
{
    /**
     * The service name
     */
    public readonly NAME = "IPC";

    /**
     * The path to the socket file
     */
    public readonly SOCKET_PATH = SOCKET_PATH;

    /**
     * Install the the event handlers
     */
    protected installMessageHandlers() {
        this.addMessageHandler("movie_ticket_added", this.onMovieTicketAdded);
    }

    // Interface Methods ---------------------------------------------------------------------------

	/**
	 * Invoked when a new Movie ticket has been created
	 */
	protected async onMovieTicketAdded(ticketId: number) {
        let movie = await MovieTicket.findOne(ticketId);
        if (movie === undefined) {
            return null;
        }
        this.app.service<Supervisor>("Supervisor").onMovieTicketNeedsTorrent(movie);
	}
}
