import PIFS from "../../PIFS";
import MatchView from "../../Views/MatchView";
import GoSocketClient from "../Core/Network/GoSocketClient";

export default class SocketPlayer extends GoSocketClient {

    constructor() {
        super();
        // this.connect("ws://192.168.101.3:11815/DumplingServer");
        this.connect("wss://www.9527fun.cn/DumplingServer");
    }

    protected onLoad() {
        console.warn("onLoad");
        this.requestEnterRoom();
    }

    protected onReload() {
        console.warn("onReload");
    }

    protected onDestroy() {
        console.warn("onDestroy");
    }

    protected onMiss() {
        console.warn("onMiss");
    }

    /**通知来自服务端-匹配完成 */
    protected onMatchComplete(id, playerInfos, uuid, timestamp) {
        console.log("onMatchComplete", {
            id: id,
            playerInfos: playerInfos,
            uuid: uuid,
            timestamp: timestamp
        });
        window.gameSystem.onMatchComplete(id, playerInfos, uuid, timestamp);
        PIFS.setMatchPlayerInfos(playerInfos, timestamp);
        MatchView.handleMatchSuccess();
    }

    /**通知来自服务端-游戏开始 */
    protected onGameStart() {
        console.log("onGameStart");
        window.gameSystem.onGameStart();
        if (MatchView.Instance) MatchView.Instance.close();
        cc.find("Canvas/BgMusic").getComponent(cc.AudioSource).enabled = true;
    }

    /**通知来自服务端-帧同步 */
    protected onFrameSync(frames: any[]) {
        window.gameSystem.onFrameSync(frames);
    }

    private _hasRequestEnterRoom = false;
    /**请求匹配进入房间 */
    public requestEnterRoom() {
        if (!MatchView.isGameInited) return;
        if (!this.isValid) return;
        if (this._hasRequestEnterRoom) return;
        this._hasRequestEnterRoom = true;
        console.log("RequestEnterRoom", PIFS.myPlayerInfo.nickname);
        this.call("RequestEnterRoom", PIFS.myPlayerInfo)
    }

    /**上传帧数据 */
    public uploadFrames(...frames) {
        if (!window.gameSystem.isGameStart) return;
        this.call("UploadFrames", frames);
    }
}
