
import { ui } from "../ui/layaMaxUI";
import { IWindow } from "./Component/IWindow";
import { WindowManager, WindowType } from "./WindowManager";
import { RecommendWindow } from "./RecommendWindow";
import { HomeWindow } from "./HomeWindow";
import { NetworkManager, PROTOCOLS } from "../Scripts/Network/NetworkManager";
import { GameUtils } from "../GameUtils/GameUtils";
import { GameManager } from "../Scripts/Game/GameManager";
import ADPlayer from "../WXSDK/ADPlayer";
import { Player } from "../Scripts/Game/Player";
import { PlayerAccount, AccountUser, ReData } from "../Scripts/Network/PlayerAccount";
import WxApi from "../WXSDK/wxsdk";
import { Main } from "../Main";
export  class OffLineWindow extends ui.Window.OffLineWindowUI implements IWindow{
    static instance:OffLineWindow;
    constructor() { super(); OffLineWindow.instance = this}
    public num:number = 0;
    left:any[] = [];
    right:any[] = [];

    OnOpen(){
        
       
        for (var i = 0; i < PlayerAccount.CurrentAccount.CurAccountData.redata.length; i++) {
           if(i<=5){
               this.left.push(PlayerAccount.CurrentAccount.CurAccountData.redata[i]);
           }
           else if(i>=6 && i<=10){
               this.right.push(PlayerAccount.CurrentAccount.CurAccountData.redata[i])
           }
        }
        this.GameListLeft.array = this.left;
        this.GameListLeft.renderHandler = Laya.Handler.create(this,this.RenderList,null,false);

        this.GameListRight.array = this.right;
        this.GameListRight.renderHandler = Laya.Handler.create(this,this.RenderList,null,false);

        this.GetBtn.clickHandler = Laya.Handler.create(this,this.GetCoin);
        this.DoubleBtn.clickHandler = Laya.Handler.create(this,this.DoubleGetCoin);

       var Gold:Laya.Text =  this.panel.getChildByName("Box").getChildByName("Num") as Laya.Text;
       Gold.text = this.RandomGold().toString();
       
       this.num = this.RandomGold();
       Gold.text = this.num.toString();
        
    //    ADPlayer.showBannerAd();
     ADPlayer.MoveButton(this.BtnBox);

        this.height = Laya.stage.height;
    };
    RenderList(_box:Laya.Box,index:number)
    {
        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=>
                        {
                            WindowManager.Instance.ClosePopView(WindowType.OffLineWindow);
                            WindowManager.Instance.OpenPopView(WindowType.RecommendWindow)
                            RecommendWindow.Instance.OpenChouTiView(true);
                            RecommendWindow.Instance.closeCt.clickHandler = Laya.Handler.create(this,()=>
                            {
                                WindowManager.Instance.ClosePopView(WindowType.RecommendWindow);
                                WindowManager.Instance.OpenPopView(WindowType.OffLineWindow);
                            });             
                            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");
                    }
                });
            }
        }
    }
    ShowOffLine(leftList:boolean,Right:boolean,panel:boolean,DoubleBtn:boolean,GetBtn:boolean){
        this.GameListLeft.visible = leftList;
        this.GameListRight.visible = Right;
        this.panel.visible = panel;
        this.DoubleBtn.visible = DoubleBtn;
        this.GetBtn.visible = GetBtn;
        //this.bg.visible = bg;

    }
    DoubleGetCoin(){
        ADPlayer.showVideoAd((flag)=>
        {
            if(flag)
            {
                var awardmoney = this.num * 2;
                var agrs = [{"reward_item":"1000"},{"reward_num":awardmoney.toString()},{"t":PlayerAccount.CurrentAccount.ServerTime.toString()}];
                NetworkManager.Instance.HttpPostRequest(PROTOCOLS.vd,agrs,Laya.Handler.create(this,()=>
                {
                    GameManager.Instance.videogetmoney -= 1;
                   // GameUtils.GetMoney(awardmoney);
                }),null)
               
            }
            else{
                var awardmoney = this.num;
                var agrs = [{"reward_item":"1000"},{"reward_num":awardmoney.toString()},{"t":PlayerAccount.CurrentAccount.ServerTime.toString()}];
                NetworkManager.Instance.HttpPostRequest(PROTOCOLS.vd,agrs,Laya.Handler.create(this,()=>
                {
                    GameManager.Instance.videogetmoney -= 1;
                    //GameUtils.GetMoney(awardmoney);
                }),null)
            }
        WindowManager.Instance.ClosePopView(WindowType.OffLineWindow);
        GameManager.Instance.GetGoin = true;
        HomeWindow.Instance. goldTex.value = PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.money);
         // HomeWindow.Instance.ShowGuide();
       }
    
        )}
    GetCoin(){
        var awardmoney = this.RandomGold();
        var agrs = [{"reward_item":"1000"},{"reward_num":awardmoney.toString()},{"t":PlayerAccount.CurrentAccount.ServerTime.toString()}];
        NetworkManager.Instance.HttpPostRequest(PROTOCOLS.vd,agrs,Laya.Handler.create(this,()=>
        {
            GameManager.Instance.videogetmoney -= 1;
           // GameUtils.GetMoney(awardmoney);
        }),null)
        WindowManager.Instance.ClosePopView(WindowType.OffLineWindow);
        WindowManager.Instance.OpenPopView(WindowType.RecommendWindow);
        GameManager.Instance.GetGoin = true;
      // HomeWindow.Instance.ShowGuide();
      HomeWindow.Instance. goldTex.value = PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.money);

    }
    
    RandomGold():number{
        var goldnum =  GameUtils.getRandomInt(100,1000);
        return goldnum;
    }
   
    ShowList(){

    }
    OnHide(){
        //this.bg.mouseEnabled = false;
       // this.ShowOffLine(false,false,false,false,false);
        
        RecommendWindow.Instance.ShowRecommendWindow(true,false,true,false,true);

        //HomeWindow.Instance.ShowGuide();
        
    };
}