import { ui } from "../ui/layaMaxUI";
import { IWindow } from "./Component/IWindow";
import { EventManager, EventType } from "../Scripts/Game/EventManager";
import { GameManager, GameState } from "../Scripts/Game/GameManager";
import { Player } from "../Scripts/Game/Player";
import { Runner } from "../Scripts/Game/Runner";
import { PlayerAccount, AccountUser, ReData } from "../Scripts/Network/PlayerAccount";
import WxApi from "../WXSDK/wxsdk";
import { RecommendWindow } from "./RecommendWindow";
import { Main } from "../Main";
import { WindowManager, WindowType } from "./WindowManager";
import { GameUtils } from "../GameUtils/GameUtils";

export class InGameWindow extends ui.Window.InGameWindowUI implements IWindow
{
    private static instance:InGameWindow;
    constructor()
    {
        super();
        InGameWindow.instance = this;
    }

    public static get Instance()
    {
        return InGameWindow.instance;
    }

    OnOpen(_data = null)
    {
       
        EventManager.EventOn(EventType.MoneyChange,this,this.OnHover);
        // EventManager.EventOn(EventType.extraspeedtime,this,this.OnExtraspeedtime);
        // EventManager.EventOn(EventType.speed,this,this.OnSpeed);
        // EventManager.EventOn(EventType.flytime,this,this.OnFly);
        // EventManager.EventOn(EventType.GetRoadPoint,this,this.OnGetRoadPoint);
        //EventManager.EventOn(EventType.PlayerRank,this,this.OnPlayerRank);
        this.RefreshBaoKuanYouXi();
        Laya.timer.loop(3000,this,this.LoopBaoKuan);

        this.height = Laya.stage.height;
    }
   

    OnPlayerRank(_playerList:Runner[])
    {
        this.ranklist.array = _playerList;
        this.ranklist.renderHandler = Laya.Handler.create(this,this.OnRank,null,false);
    }

    OnRank(_box:Laya.Box)
    {
        var data = _box.dataSource as Runner;
        var name = _box.getChildAt(0).getChildByName("name") as Laya.Text;
        name.text = data.name;
        var score = _box.getChildAt(0).getChildByName("score") as Laya.Text;
        score.text = Math.round(data.m_sprite.transform.position.z).toString();
    }

    OnHover(_time:number)
    {
        //console.log(_time);
        this.score.value = _time.toString();
        // this.score.text = Math.round(_time).toString();
        // if(Math.floor(_time) > 10)
        // {
        //     this.score.text = "You Die!";
        //     //GameManager.Instance.ChangeState(GameState.Die);
            
        // }
    }

    OnExtraspeedtime(_time:number)
    {
        this.extraspeedtime.text = Math.round(_time).toString();
    }

    OnSpeed(_time:string)
    {
        this.speed.text = _time;
    }

    OnFly(_time:number)
    {
        this.flytime.text = Math.round(_time).toString();
    }

    nextpoint = [];
    oldpoint = [];
    OnGetRoadPoint(_ps:number)
    {
        this.road.text = Math.floor(_ps).toString();
    }
    
    LoopBaoKuan()
    {
        this.index ++;
        if(this.index > PlayerAccount.CurrentAccount.CurAccountData.redata.length - 1)
        {
            this.index = 0;
        }
        this.RefreshBaoKuanYouXi();
    }

    index = 0;
    RefreshBaoKuanYouXi()
    {
        var data = PlayerAccount.CurrentAccount.CurAccountData.redata[this.index];
        var icon = this.shiwan.getChildByName("icon") as Laya.Image;
        icon.skin = data.icon;
       
        var btn:Laya.Button = this.shiwan.getChildByName("btn") as Laya.Button;
        
        btn.clickHandler = Laya.Handler.create(this,this.ClickBNavigate,[data],false);
    }

    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 btn:Laya.Button = _box.getChildAt(0).getChildByName("btn") as Laya.Button;

        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.ClosePopView(WindowType.RecommendWindow);
                            });
                            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");
                    }
                });
            }
        }
    }

    OnHide()
    {
        EventManager.EventOff(EventType.Hover,this,this.OnHover);
        EventManager.EventOff(EventType.extraspeedtime,this,this.OnExtraspeedtime);
        EventManager.EventOff(EventType.speed,this,this.OnSpeed);
        EventManager.EventOff(EventType.flytime,this,this.OnFly);
        EventManager.EventOff(EventType.GetRoadPoint,this,this.OnGetRoadPoint);
        EventManager.EventOff(EventType.PlayerRank,this,this.OnPlayerRank);
    }
}