import { Global } from "../Global";
import { DispatcherManager } from "../utils/DispatcherManager";

/*
* name;
*/
export class ServerHttps extends Laya.EventDispatcher
{
    private static ins:ServerHttps;
    private hr:Laya.HttpRequest;
    private _callbackList:Array<any>;
    private _isrequesting:boolean = false;
    constructor(){
        super();
        this.init();
        ServerHttps.ins = this;
    }

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

    public init():void
    {
        this._callbackList = [];
        this.hr = new Laya.HttpRequest();
        // if(this.hr.http)
        // {
        //     this.hr.http.timeout = 5000;//设置超时时间；
        // }
        this.hr.on( Laya.Event.PROGRESS,this,this.onHttpRequestProgress );
        this.hr.on(Laya.Event.COMPLETE, this, this.onHttpRequestComplete);
        this.hr.on(Laya.Event.ERROR, this, this.onHttpRequestError);
    }

    public sendPost(act:string, data:any,callback:Function, callbackobj:any,isShowWait:boolean = false ):void
    {
        var itemdata:any = {};
        itemdata.act = act;
        itemdata.data = data;
        itemdata.callback = callback;
        itemdata.callbackobj = callbackobj;
        itemdata.isShowWait = isShowWait;
        itemdata.sendtype = "post";
        itemdata.httpurl = Global.httpurl;
        this._callbackList.push( itemdata );
        this.sendByPost( act,data,callback,callbackobj,isShowWait );
    }

   

    private sendByPost(act:string, data:any,callbackobj:any,callback:Function,isShowWait:boolean = false):void
    {
        if( this._isrequesting )
        {
            return;
        }
        this._isrequesting = true;
        if( isShowWait )
        {
            fairygui.GRoot.inst.showModalWait();
        }

        this.hr.send(Global.httpurl + act,data,"post","json",["Content-Type", "application/json"]);
    }


    private onHttpRequestError(e: any): void {
      
        if(e)
        {
            
        }
        fairygui.GRoot.inst.closeModalWait();
        this._isrequesting = false;
    }

    private onHttpRequestProgress(e: any): void {
    }

    private onHttpRequestComplete(e: any): void {
        fairygui.GRoot.inst.closeModalWait();

        this._isrequesting = false;
        if( this._callbackList.length > 0 )
        {
            var itemData:any = this._callbackList.shift();
            this.checkMessage();
            var callback:Function = itemData.callback;
            var callbackobj:any = itemData.callbackobj;
            var data:any = JSON.parse( this.hr.data );
            if( this.hr.data != "" )
            {
                if( data.hasOwnProperty( "ACTIONCOM" ) )
                {
                    if( data.ACTIONCOM==0 )//succeed
                    {
                        if( callback && callbackobj )
                        {
                            callback.call( callbackobj,data.JSDATA );
                        }
                    }
                    else//faild
                    {

                    }
                }
            }
        }
    }

    private checkMessage():void
    {
        if( this._callbackList.length > 0 )
        {
            var nextItemData:any = this._callbackList[0];
            if( nextItemData.sendtype == "post" )
            {
                this.sendByPost( nextItemData.act,nextItemData.data,nextItemData.callback,nextItemData.callbackobj,nextItemData.isShowWait );
            }
        }
    }
}