import { IpcServerService } from "@autoplex/ipc";
import { SOCKET_PATH } from "@autoplex-api/search";
import MovieSearch from "./MovieSearch";

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

	/**
	 * The path to the scoket file
	 */
	protected readonly SOCKET_PATH = SOCKET_PATH;

	/**
	 * Store the Movie Search service instance
	 */
	protected searchService!: MovieSearch;

	/**
     * Boot the IPC interface
     */
	public async boot() {
		this.searchService = this.app.service<MovieSearch>("Movie Search");
		await super.boot();
	}

	/**
     * Install the the event handlers
     */
	protected installMessageHandlers() {
		this.addMessageHandler("details",        this.details);
		this.addMessageHandler("find",           this.find);
		this.addMessageHandler("search",         this.search);
		this.addMessageHandler("verify_imdb_id", this.verifyImdbId);
	}

	// Interface -----------------------------------------------------------------------------------

	/**
	 * Get the details of a movie
	 */
	protected async details(tmdbId: number) {
		return await this.searchService.details(tmdbId);
	}

	/**
	 * Find a movie by its IMDb ID
	 */
	protected async find(imdbId: string) {
		return await this.searchService.findFromImdb(imdbId);
	}

	/**
	 * Search for a movie
	 */
	protected async search(payload: { query: string, year?: number, page?: number }) {
		return await this.searchService.search(payload.query, payload.year, payload.page);
	}

	/**
	 * Verify an IMDb ID
	 */
	protected async verifyImdbId(imdbId: string) {
		return await this.searchService.verifyImdbId(imdbId);
	}
}
