import {WXGamePlatform} from "../WXSDK/WXGamePlatform";
import {PlatfromType} from "./PlatfromType";
import {WECHATEVENT} from "../WXSDK/WECHATEVENT";
import { Global } from "../Global";
import { DispatcherManager } from "../utils/DispatcherManager";
import { TimesTamper } from "../Time/TimesTamper";
import {DateControl} from "../Control/DateControl";
import {AudioControl} from "../Control/AudioControl";
import { GlobalTip } from "../utils/GlobalTip";

/*
* name;
*/ 
export class pingtaiutils
{
    private _isenable:boolean = false;
    /**
     * 是否合规
     */
    private static islegal:number = 0;


    constructor() {
        pingtaiutils.ins = this;
    }

    private static ins: pingtaiutils;

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

    /**
     * 初始化平台
     */
    public initPlatfrom():void
    {
        if( Laya.Browser.window.tt )
        {
            Global.nowplatfrom = PlatfromType.tt;
        }
        else if (Laya.Browser.window.wx) //微信小游戏
        {
            Global.nowplatfrom = PlatfromType.wx;
        } 
        else
        {
            Global.nowplatfrom = PlatfromType.pc;
        }
    }

    public init():void 
    {
        this.initPlatfrom();
        // switch( Global.nowplatfrom )
        // {
        //     case PlatfromType.wx:
        //         WXGamePlatform.Instance.init();
        //     break;
        //     case PlatfromType.qq:
        //     break;
        //     default:
        //     break;
        // }
        // this.onShow( this,this.showcallback );
        // this.onHide(this,this.hidecallback);
    }

    private initData():void
    {
        DateControl.Instance.SetSelf();
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.initData();
            break;
            default:
            break;
        }
    }

    /**
     * 合违规判断 true：合规，false:违规
     */
    public static checkEnable():boolean
    {
        if (pingtaiutils.islegal == 1)
        {
            return true;
        }
        return false;
    }

    private _logincallobj:any;
    private _loginsucceedcallback:Function;
    public login( callobj:any=null,succeedcallback:Function=null,progressCallback:Function=null,failcallback:Function = null ):void
    {
        fairygui.GRoot.inst.showModalWait();
        this._logincallobj = callobj;
        this._loginsucceedcallback = succeedcallback;
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.createUserAndLogin(this,this.loginSucceed,this.loginprogressCallback,this.loginfailcallback);
            break;
            default:
                this.loginSucceed(null);
            break;
        }
    }

    private loginprogressCallback( value:number ):void
    {
        
    }

    private loginSucceed( data:any ):void
    { 
        fairygui.GRoot.inst.closeModalWait();
    }

    private loginfailcallback():void
    {

    }

    public onShow(callobj: any=null, succeedcallback: Function=null, failCallback: Function = null): void {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.onShow(callobj, succeedcallback, failCallback);
            break;
            case PlatfromType.qq:
            break;
        }
    }

    private showcallback():void
    {
        Laya.timer.clear( this,this.againplaybgSound );
        Laya.timer.once( 500,this,this.againplaybgSound );
        DispatcherManager.dispatchEvent( WECHATEVENT.WECHATEVENT_onshow );
        DispatcherManager.dispatchEvent( WECHATEVENT.WECHATEVENT_getfocus );
        var fromscene:number = pingtaiutils.fromscene();
        if( fromscene == 1089 || fromscene == 1131 )
        {
            DispatcherManager.dispatchEvent( WECHATEVENT.WECHATEVENT_fromscorfk );
        }
    }

    private againplaybgSound():void
    {
        DispatcherManager.dispatchEvent( WECHATEVENT.WECHATEVENT_onAudioInterruptionEnd );
    }

    public onHide(callobj: any=null, succeedcallback: Function=null): void {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.onHide(callobj, succeedcallback);
            break;
            case PlatfromType.qq:
            break;
        }
    }
    private hidecallback():void
    {
        TimesTamper.Instance.sendonlineTongji();
        AudioControl.Instance.ClearAllSOUND();
        DispatcherManager.dispatchEvent( WECHATEVENT.WECHATEVENT_onhide );
        DispatcherManager.dispatchEvent( WECHATEVENT.WECHATEVENT_losefocus );
    }

    /**
     * 当前游戏是否在显示中
     */
    public get isshow():boolean
    {
        var bo:boolean = true;
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                if( WXGamePlatform.isShowing )
                {
                    bo = true;
                }
                bo = false;
            break;
        }
        return bo;
    }

    /**
     * 分包加载
     */
    public loadSubpackage( typename:string,callobj:any,callback:Function,callprogress:Function,failcallback:Function=null ):void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.loadSubpackage( typename,callobj,callback,callprogress,failcallback );
            break;
            default:
                if( callobj && callback )
                {
                    callback.call( callobj );
                }
            break;
        }
    }

    /**
     * 获取是否已经授权
     * @param callobj 
     * @param callback 
     */
    public isaccredit( callobj:any,accreditCallback:Function ):void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.getaccredit( callobj,accreditCallback );
            break;
            case PlatfromType.qq:
                WXGamePlatform.Instance.getaccredit( callobj,accreditCallback );
            break;
            default:
                if(accreditCallback&&callobj)
                {
                    accreditCallback.call( callobj,true );
                }
            break;
        }
    }

    /**
     * 拿角色信息授权
     */
    public createaccredit( callobj:any,callback:Function,failcallback:Function ):void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.createUserInfoButton(callobj,callback,failcallback);
            break;
        }
    }

    public closeuserinfoButton():void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.closeuserinfobUtton();
            break;
        }
    }

    /**
     * 退出游戏
     */
    public exitGame():void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.exitGame();
            break;
        }
    }

    /**
     * 打开客服对话框
     */
    public openCustomerService():void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.openCustomerService();
            break;
        }
    }

    /**
     * 短震动
     */
    public vibrateShort():void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.vibrateShort();
            break;
            default:
            break;
        }
    }

    public static isCanVideo():boolean
    {
        var bo:boolean = false;
        if( pingtaiutils.checkEnable() )
        {
            bo = true;
        }
        else
        {
            switch( Global.nowplatfrom )
            {
                case PlatfromType.wx:
                    bo = WXGamePlatform.Instance.isCanVideo();
                break;
                default:
                    bo = true;
                break;
            }
        }
        return bo;
    }

    public startShareApp(key:string,callobj:any=null,succeedCallback:Function=null,failCallback:Function = null):void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.startShareApp( key,callobj,succeedCallback,failCallback );
            break;
            default:
                if( callobj && succeedCallback )
                {
                    succeedCallback.call( callobj );
                }
            break;
        }
    }

    public playVideo( key:string, callobj:any=null, succeedcallback:Function=null,failCallback:Function=null,istongjiNum:boolean = true )
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.playVideo( key,callobj,succeedcallback,failCallback,istongjiNum );
            break;
            default:
                if( callobj && succeedcallback )
                {
                    succeedcallback.call( callobj );
                }
            break;
        }
    }

    public createBanner(bannertype:string,style:any={x:0,y:0,scale:1}):void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.createBanner( bannertype,style );
            break;
            default:
            break;
        }
    }

    public hideBanner( bannertype:string ):void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.hideBanner( bannertype );
            break;
            default:
            break;
        }
    }

    /**
     * 数据上传（用于排行榜数据显示）
     * @param param1 上报数据1
     * @param param2 上报数据2
     */
    public storeToCloud(param1:number,param2:number):void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.storeToCloud( param1,param2 );
            break;
            default:
            break;
        }
    }

    /**
     * 打开好友排行榜
     */
    public openFriendRank( type:string,viewType:string,rect:Laya.Rectangle,parent:Laya.Sprite = null,data:any = null ):void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.openFriendRank( type,viewType,rect,parent,data );
            break;
            default:
            break;
        }
    }

    /**
     * 排行榜打开状态时候，切换显示类型
     * @param type 
     */
    public changeFriendRank( type:string ):void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.changeFriendRank(type);
            break;
            default:
            break;
        }
    }

    /**
     * 排行榜上一页
     */
    public prePageRank():void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.prePageRank();
            break;
            default:
            break;
        }
    }

    /**
     * 排行榜下一页
     */
    public nextPageRank():void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.nextPageRank();
            break;
            default:
            break;
        }
    }

    /**
     * 关闭好友排行榜
     */
    public closeFriendRank( viewType:string ):void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.closeFriendRank(viewType);
            break;
            default:
            break;
        }
    }

    public lockVideoLeftTime():number
    {
        var time:number = 0;
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                time = WXGamePlatform.Instance.lockVideoLeftTime();
            break;
            default:
            break;
        }
        return time;
    }

    public static dayVideoNums():number
    {
        var time:number = 0;
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                time = WXGamePlatform.dayVideoNum;
            break;
            default:
            break;
        }
        return time;
    }

    /**
     * 1089:添加到我的小程序，1131:添加到浮窗
     */
    public static fromscene():number
    {
        var scene:number = 0;
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
            scene = WXGamePlatform.fromscene;
            break;
            default:
            break;
        }
        return scene;
    }

    public static createGameClubButton( style:any,icon:string = "green" ):void
    {
        switch( Global.nowplatfrom )
        {
            case PlatfromType.wx:
                WXGamePlatform.Instance.createGameClubButton( style,icon );
            break;
            default:
            break;
        }
    }
}