
import { IWindow } from "./Component/IWindow";
import { ui } from "../ui/layaMaxUI";
import { GameUtils } from "../GameUtils/GameUtils";
import { PlayerAccount } from "../Scripts/Network/PlayerAccount";
import { HeroData } from "../Scripts/Game/LevelData";
import ADPlayer from "../WXSDK/ADPlayer";
import WxApi from "../WXSDK/wxsdk";
import { WindowManager } from "./WindowManager";
import { GameManager, GameState } from "../Scripts/Game/GameManager";
import { Player } from "../Scripts/Game/Player";


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

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

    static trial = true;

    OnOpen(_data = null)
    {
        this.hidebtn.clickHandler = Laya.Handler.create(this,this.OnTrialHide,null,false);

        var trialid = [2,3,4,5,6,8,9,10,11];
        var curids = [];

        for(var i = 0;i < trialid.length;i++)
        {
            var id = trialid[i];
            if(!PlayerAccount.CurrentAccount.CurAccountData.HasHero(id))
            {
                curids.push(id);
            }
        }

        var useids = [];
        for(var i = curids.length - 1;i > -1;i--)
        {
            if(useids.length < 4)
            {
                var index = GameUtils.getRandomInt(0,curids.length - 1);
                useids.push(curids[index]);
                curids.splice(index,1);
            }
        }

        if(curids.length)

        var skins = [];
        if(useids.length > 0)
        {
            for(var i = 0;i < useids.length;i++)
            {
                var heroid = useids[i];
                skins.push(PlayerAccount.CurrentAccount.CurAccountData.m_heroDic[heroid]);
            }
        }
        else
        {
            this.Pass();
            return;
        }

        this.skinlist.array = skins;

        for(var i = 0; i < this.skinlist.cells.length;i++)
        {
            var cell = this.skinlist.cells[i];
            var config = skins[i];

            var icon = cell.getChildAt(0).getChildAt(0).getChildAt(0) as Laya.Image;
            icon.skin = config.id > 6?"trial/"+config.curicon+".png":"shop/"+config.curicon+".png";
            var btn = cell.getChildAt(0).getChildAt(1) as Laya.Button;
            btn.clickHandler = Laya.Handler.create(this,this.OnTrialHero,[config],false);
        }

        this.passbtn.clickHandler = Laya.Handler.create(this,this.Pass);
        this.height = Laya.stage.height;
        Laya.stage.on(Laya.Event.RESIZE,this,()=>
        {
            this.height = Laya.stage.height;
        })
    }

    OnTrialHero(_skin:HeroData)
    {
        ADPlayer.showVideoAd((flag)=>
        {
            if(flag)
            {
                Player.Instance.RessetPlayer(_skin);
                GameManager.Instance.ChangeState(GameState.Queue);
            }
        })
    }

    Pass()
    {
        GameManager.Instance.ChangeState(GameState.Queue);
    }

    OnTrialHide()
    {
        TrialWindow.trial = !TrialWindow.trial;
        this.ppp.visible = !TrialWindow.trial;
    }

    OnHide()
    {

    }
}