import { DispatcherManager } from "../utils/DispatcherManager";
import {SocketNofify} from "../WXSDK/SocketNofify";

export class ServerSocket
{
    private sock: Laya.Socket;

    private static ins: ServerSocket;

    /**
     * 1:自动断线，2：主动断线
     */
    private _offlinestate:number = 0;

    /**
     * 断线重连次数
     */
    private _connetNums:number = 0;

    /**
     * socket连接中
     */
    constructor(){
        ServerSocket.ins = this;
    }
    private _socketconneting:boolean = false;

    public static get Instance(): ServerSocket {
        if (ServerSocket.ins == null) {
            return new ServerSocket();
        }
        return ServerSocket.ins;
    }

    //连接服务器
    public connectServer(gameHost:string) {
        this._socketconneting = true;
        this.removeSocket();
        if (!this.sock || !this.sock.connected) 
        {
            this.openwaitpanel();
            if (!this.sock) {
                this.sock = new Laya.Socket();
                this.sock.on(Laya.Event.OPEN, this, this.onSocketOpen);
                this.sock.on(Laya.Event.MESSAGE, this, this.onReceiveMessage);
                this.sock.on(Laya.Event.CLOSE, this, this.onSocketClose);
                this.sock.on(Laya.Event.ERROR, this, this.ioErro);
            }
            this.sock.connectByUrl(gameHost);
        }
    }

    public isConnect(): boolean {
        if (this.sock && this.sock.connected) {
            return true;
        }
        return false;
    }

    private ioErro(e: any = null): void {
        this._socketconneting = false;
        this._connetNums++;
        this.closewaitpanel();
        this.removeSocket();
        this._offlinestate=1;
    }

    private removeSocket():void
    {
        this._socketconneting = false;
        if(this.sock)
        {
            this.sock.off(Laya.Event.OPEN, this, this.onSocketOpen);
            this.sock.off(Laya.Event.MESSAGE, this, this.onReceiveMessage);
            this.sock.off(Laya.Event.CLOSE, this, this.onSocketClose);
            this.sock.off(Laya.Event.ERROR, this, this.ioErro);
            this.sock.cleanSocket();
            this.sock.close();
            this.sock = null;
        }
    }

    /**
     * 手动关闭游戏socket
     */
    public colseSocket( offlinestate:number = 1 ):void
    {
        if( this.isConnect() )
        {
            this.closewaitpanel();
            this.removeSocket();
            if( offlinestate != -1 )
            {
                this._offlinestate= offlinestate;
            }
            DispatcherManager.dispatchEvent( SocketNofify.SOCKET_CLOSE );
        }
    }

    private onSocketClose(e: any = null): void {
        this._socketconneting = false;
        this.closewaitpanel();
        this._offlinestate=1;
        DispatcherManager.dispatchEvent( SocketNofify.SOCKET_CLOSE );
    }


    private closewaitpanel(): void 
    {
        fairygui.GRoot.inst.closeModalWait();
    }

    private openwaitpanel(): void 
    {
        fairygui.GRoot.inst.showModalWait();
    }


    //连接成功返回
    private onSocketOpen(event: any = null): void {
        this._connetNums = 0;
        this.closewaitpanel();
        this._offlinestate = 0;
        DispatcherManager.dispatchEvent(SocketNofify.SOCKET_CONNECT_SUCCEED, null, false);
    }

    //向服务端发送消息
    public sendMessage(act: string, data: any, isShowWait: boolean = true): void {
        if( this.sock && this.sock.connected )
        {
            if( isShowWait )
            {
                this.openwaitpanel();
            }
            if( !data )
            {
                data={};
            }
            data.ACTION = act;
            this.sock.send(JSON.stringify(data));
        }
        else
        {

        }
    }

    //消息返回后，会广播出去
    //Global.addEventListener("你发送的cmd名称",监听方法,this)
    private onReceiveMessage(msg: any = null): void {
        var socketdata: any = JSON.parse(msg);

    }
}