
import { IWindow } from "./Component/IWindow";
import { ui } from "../ui/layaMaxUI";
import { GameUtils } from "../GameUtils/GameUtils";
import { GameManager, GameState } from "../Scripts/Game/GameManager";
import { NetworkManager, PROTOCOLS } from "../Scripts/Network/NetworkManager";
import { PlayerAccount } from "../Scripts/Network/PlayerAccount";
import WxApi from "../WXSDK/wxsdk";
import { Main } from "../Main";
import { LevelManager } from "../Scripts/Game/LevelManager";


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

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

    queueAtlas = 
	[
		"res/atlas/local.atlas",
		"res/atlas/local.png",
		"res/atlas/load.atlas",
		"res/atlas/load.png",
	];

    OnOpen(_data = null)
    {
        WxApi.Instance.recordAldEvent("进入排队界面","1");
        var agrs = [{"t":PlayerAccount.CurrentAccount.ServerTime.toString()},{"n":"5"}];
        NetworkManager.Instance.HttpPostRequest(PROTOCOLS.game,agrs,Laya.Handler.create(this,this.OnStarcomplete),null);
        //this.OnResLoad();
        this.height = Laya.stage.height;
    }

    indess:number[] = [];
    OnResLoad()
    {
        Laya.timer.once(1000,this,this.OnQueueEnd);
        for(var i = 1;i < 30;i++)
        {
            this.indess.push(i);
        }
        for(var i = 0;i < this.quelist.numChildren;i++)
        {
            var cursprite = this.quelist.getChildAt(i) as Laya.Image;
            
            this.ShowIcon(cursprite);
        }
    }

    OnStarcomplete(e)
    {
        this.OnResLoad();
    }

    ShowIcon(_sprite:Laya.Image)
    {
        var time = GameUtils.getRandomInt(0,900);
        Laya.timer.once(time,this,()=>
        {
            var index = GameUtils.getRandomInt(0,this.indess.length);
            var num = this.indess[index];
            this.indess.splice(index,1);
            _sprite.skin = "icon/" + num + ".png";
        },null,false)
    }

    OnQueueEnd()
    {
        WxApi.Instance.recordAldEvent("排队结束开始游戏","1");
        var stage = LevelManager.Instance.curLevel;
        WxApi.Instance.levelStart(stage.levelid.toString(),stage.levelid.toString(),PlayerAccount.CurrentAccount.CurAccountData.UseId);
        GameManager.Instance.ChangeState(GameState.Playing);
    }

    OnHide()
    {

    }
}