import { SOCKET_PATH, IpcMethod } from "@autoplex-api/manager";
import { IMovie } from "@autoplex-api/search";
import { IpcServerService } from "@autoplex/ipc";
import Supervisor from "./Supervisor";
import TicketManager from "./TicketManager/TicketManager";

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;

    /**
     * Store a reference to the torrent client IPC
     */
    protected supervisor!: Supervisor;

    /**
     * Store a reference to the ticket manager
     */
    protected ticketManager!: TicketManager;

    /**
     * Link the required services
     */
    public override link() {
        this.supervisor    = this.app.service<Supervisor>("Supervisor");
        this.ticketManager = this.app.service<TicketManager>("Ticket Manager");
    }

    /**
     * Install the the event handlers
     */
    protected override installMessageHandlers() {
        this.addMessageHandler(IpcMethod.ActiveMovieRequests, this.activeMovieRequests);
        this.addMessageHandler(IpcMethod.ActiveMovieRequestsForUser, this.activeMovieRequestsForUser);
        this.addMessageHandler(IpcMethod.MapMovieRequests, this.mapMovieRequests);
        this.addMessageHandler(IpcMethod.MovieRequestStatus, this.movieRequestStatus);
        this.addMessageHandler(IpcMethod.CreateMovieRequest, this.createMovieRequest);
        this.addMessageHandler(IpcMethod.CancelMovieRequest, this.cancelMovieRequest);
    }

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

    public async activeMovieRequests() {

    }

    public async activeMovieRequestsForUser() {

    }

    public async mapMovieRequests() {

    }

    public async movieRequestStatus() {

    }

    public async createMovieRequest(userId: number, tmdbId: number) {
        return await this.ticketManager.createMovieTicket(userId, tmdbId);
    }

    public async cancelMovieRequest() {

    }

	/**
	 * 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);
	// }

    /**
     * Get the states of the provided movie tickets
     */
    // protected async getMovieTicketStates(ticketIds: number[]) {
    //     let tickets = await MovieTicket.findByIds(ticketIds, { relations: ["torrents"] });
    //     let torrents = this.torrentManager.cachedTorrents;
    //     let results: any = {};
    //     for (let ticket of tickets) {
    //         let result: any = {};
    //         if (ticket.isCanceled) {
    //             continue;
    //         }
    //         if (ticket.torrents.length > 0) {
    //             let progressList = ticket.torrents.map(torrent => torrents[torrent.infoHash].progress);
    //             result["progress"] = Math.max(...progressList);
    //         }
    //         results[ticket.id] = result;
    //     }
    //     return results;
    // }
}
