import Channel from "../channel.js";
import { cancelGame, checkTimeout, gameReady, isValidUpdateKey, isValidUpdateValue, putStone, updateSettings } from "../../../../services/reversi/index.js";
export default class extends Channel {
    chName = "reversiGame";
    static shouldShare = false;
    static requireCredential = false;
    gameId = null;
    constructor(id, connection){
        super(id, connection);
        this.onEvent = this.onEvent.bind(this);
    }
    async init(params) {
        if (typeof params.gameId !== "string") return;
        this.gameId = params.gameId;
        this.subscriber.on(`reversiGameStream:${this.gameId}`, this.onEvent);
    }
    onEvent(data) {
        this.send(data);
    }
    onMessage(type, body) {
        if (!this.gameId) return;
        switch(type){
            case "ready":
                if (this.user && typeof body === "boolean") gameReady(this.gameId, this.user, body);
                break;
            case "updateSettings":
                if (this.user && body && isValidUpdateKey(body.key) && isValidUpdateValue(body.key, body.value)) {
                    updateSettings(this.gameId, this.user, body.key, body.value);
                }
                break;
            case "cancel":
                if (this.user) cancelGame(this.gameId, this.user);
                break;
            case "putStone":
                if (this.user && body && typeof body.pos === "number") {
                    putStone(this.gameId, this.user, body.pos, typeof body.id === "string" ? body.id : null);
                }
                break;
            case "claimTimeIsUp":
                checkTimeout(this.gameId);
                break;
        }
    }
    dispose() {
        if (this.gameId) {
            this.subscriber.off(`reversiGameStream:${this.gameId}`, this.onEvent);
        }
    }
}
