import { ui } from "../ui/layaMaxUI";
import { IWindow } from "./Component/IWindow";
import { Main } from "../Main";
import { WindowManager, WindowType } from "./WindowManager";
import { RecommendWindow } from "./RecommendWindow";
import { PlayerAccount, ReData } from "../Scripts/Network/PlayerAccount";
import { GameUtils } from "../GameUtils/GameUtils";
import { GameManager, GameState } from "../Scripts/Game/GameManager";
import WxApi from "../WXSDK/wxsdk";
export class FriendPlyaingWindow extends ui.Window.FriendPlayingWindowUI implements IWindow{
   
    static instance:FriendPlyaingWindow;
    public return = 0;
    constructor() { super(); FriendPlyaingWindow.instance = this}
    
    OnOpen(){
        
       
            this.CloseBtn.clickHandler = Laya.Handler.create(this,this.OnClickClose);
            
           // this.CloseBtn.clickHandler = Laya.Handler.create(this,this.SpecialClose);
           
        
        this.GameList.vScrollBarSkin = "";
        this.GameList.array = PlayerAccount.CurrentAccount.CurAccountData.redata;
        this.GameList.renderHandler = Laya.Handler.create(this,this.RenderList,null,false);
            
        
        
        this.height = Laya.stage.height;
    };
    RenderList(_box:Laya.Box,index:number)
    {
       
       if( index>5 ){
        var ne:Laya.Sprite = _box.getChildAt(0).getChildByName("new") as Laya.Sprite;
        ne.visible = false;
       }
        var data:ReData = _box.dataSource as ReData;
        var icon:Laya.Image = _box.getChildAt(0).getChildByName("icon") as Laya.Image;
        icon.skin = data.icon;
        var name:Laya.Text = _box.getChildAt(0).getChildByName("name") as Laya.Text;
        name.text = data.title;
        
        var btn:Laya.Button = _box.getChildAt(0).getChildByName("btn") as Laya.Button;

        var num:Laya.Text = _box.getChildAt(0).getChildByName("num") as Laya.Text;
        if(num)num.text = GameUtils.getRandomInt(24841,654878).toString() + "人玩";
       
        btn.clickHandler = Laya.Handler.create(this,this.ClickBNavigate,[data],false);
        
    }
    ClickBNavigate(data:ReData)
    {
        var appid = data.appid;
        var path = data.path;
        let param = {"appId":appid.toString(),"path":path.toString()};

        if(Main.OnWx)
        {
            WxApi.Instance.recordAldEvent("导出-总点击数","1");
            
            if(data.jump)
            {
                Laya.Browser.window.wx.navigateToMiniProgram(
                    {
                        appId:appid.toString(),
                        path:path.toString(),
    
                        extraData:
                        {
                            next_appid:data.next_appid,
                            next_path:data.next_path
                        },
                        envVersion:'develop',
                        success:res=>
                        {
                            WxApi.Instance.recordAldEvent("导出成功-总用户数","1");
                            WxApi.Instance.recordAldEvent("导出成功-" + data.title,"1");
                            console.log("跳转成功...");
                        },
                        fail:res=>
                        {
                            //WxApi.Instance.recordAldEvent("跳转到游戏失败:"+ data.title,"1");
                            WindowManager.Instance.OpenPopView(WindowType.RecommendWindow)
                            RecommendWindow.Instance.OpenChouTiView(true);
                            RecommendWindow.Instance.closeCt.clickHandler = Laya.Handler.create(this,()=>
                            {
                                WindowManager.Instance.ShowWindow(WindowType.HomeWindow);
                                //WindowManager.Instance.ClosePopView(WindowType.RecommendWindow);
                                //WindowManager.Instance.ShowWindow(WindowType.FriendPlayingWindow);
                            });
                            console.log("跳转失败...");
                        }
                    });
            }
            else
            {
                var marray = [data.image];
                Laya.Browser.window.wx.previewImage({
                    current: data.image,
                    urls: marray,
                    success: res=> {
                        WxApi.Instance.recordAldEvent("显示游戏二维码成功","1");
                    },
                    fail: res=> {
                        //WxApi.Instance.recordAldEvent("显示游戏二维码失败","1");
                    }
                });
            }
        }
    }
    
    OnClickClose(){
        WindowManager.Instance.ShowWindow(WindowType.HomeWindow);
       // RecommendWindow.Instance.OpenChouTiView(true);
    }
    
   
    
    OnHide(){
    };
}