import { WebSocketClient as WSClient } from "@autoplex/websocket-client";
import { IMovieProgressResponse, WebSocketMethod } from "@autoplex-api/interface";
import store, { Action } from "./store";

class WebSocketClient extends WSClient
{
	/**
	 * Setup the socket
	 */
	protected setup() {
		store.watch(state => state.user, () => this.authStateChanged());
	}

	/**
	 * Get the app's current auth JWT
	 */
	protected jwt() {
		return store.state.user?.token ?? null;
	}

	/**
	 * Forget the JWT and log out of the app
	 */
	protected forgetJwt() {
		store.dispatch(Action.AuthForget, undefined);
	}

	// Public Interface ----------------------------------------------------------------------------

	/**
	 * Get the progress of the given movies
	 */
	public async movieProgress(ticketIds: number[]) {
		return await this.request<IMovieProgressResponse>(WebSocketMethod.TicketProgress, ticketIds);
	}
}

export default new WebSocketClient(`ws://${document.domain}:3250/`);
