import { ui } from "../ui/layaMaxUI";
import GuangGao from "../Net/GuangGao";
import { GameTools } from "../Tools/GameTools";
import { AccountManager } from "../Net/AccountManager";
import { ShopScene } from "./ShopView";
import { WXSDK } from "../Net/WXSDK";
import { ViewManager, ViewType } from "./ViewManager";
import { ServerManager, ArcherWorrior } from "../Net/ServerManager";
import { AudioManager, MusicType } from "../Tools/AudioManager";
import { EventManager,StageState } from "../Tools/EventManager";
import MainGameView from "./MainGameView";
import { GameManager } from "../Game/GameManager";

export default class TrialView extends ui.TrialViewUI{
    private static ins:TrialView;
    private data:any;
    private count:number;
    private arr;
    constructor(){
        super();
    }
    public static get Instance():TrialView{
        if(TrialView.ins){
            return TrialView.ins
        }
        else{
            return new TrialView();
        }
    }
    Show(){
        WXSDK.AldEvent("打开皮肤试用界面");
        GuangGao.OpenBanner();
        this.Init();
        this.TrialBtn.clickHandler  = Laya.Handler.create(this,this.Trial);
        this.NoTrialBtn.visible = false;
        Laya.timer.once(2000,this,()=>{
            this.NoTrialBtn.visible = true;
            this.NoTrialBtn.clickHandler = Laya.Handler.create(this,this.ClickClose);
        })
        this.height = Laya.stage.height;
        Laya.stage.on(Laya.Event.RESIZE,this,()=>
        {
            this.height = Laya.stage.height;
        })
    }
    Close(){
        WXSDK.AldEvent("关闭皮肤试用界面");
    }
    ClickClose(){
        var agrs = [{ "t": AccountManager.Instance.ServerTime.toString() }];
        ServerManager.Instance.SendHttp(ArcherWorrior.KaiShi, agrs, Laya.Handler.create(this, this.Complete), null);
    }
    Trial(){
        GuangGao.Instance.loadVideo(isLoaded => {
            if (isLoaded) {
                GuangGao.Instance.showVideo(isOk => {
                    if (isOk) {
                        WXSDK.AldEvent("观看视频试用皮肤成功");
                        var agrs = [{ "t": AccountManager.Instance.ServerTime.toString() }];
                        ServerManager.Instance.SendHttp(ArcherWorrior.KaiShi, agrs, Laya.Handler.create(this, this.Complete), null);
                        GameManager.Instance.SetHorse(this.arr.id);
                    }
                    else {
                        WXSDK.AldEvent("观看视频试用皮肤失败");
                        var agrs = [{ "t": AccountManager.Instance.ServerTime.toString() }];
                        ServerManager.Instance.SendHttp(ArcherWorrior.KaiShi, agrs, Laya.Handler.create(this, this.Complete), null);
                    }
                });
            }
            else {
                WXSDK.fengxiangtupian(isOk => {
                    if (isOk) {
                        WXSDK.AldEvent("分享好友试用皮肤成功");
                        var agrs = [{ "t": AccountManager.Instance.ServerTime.toString() }];
                        ServerManager.Instance.SendHttp(ArcherWorrior.KaiShi, agrs, Laya.Handler.create(this, this.Complete), null);
                        GameManager.Instance.SetHorse(this.arr.id);
                    }
                    else {
                        WXSDK.AldEvent("分享好友试用皮肤失败");
                        var agrs = [{ "t": AccountManager.Instance.ServerTime.toString() }];
                        ServerManager.Instance.SendHttp(ArcherWorrior.KaiShi, agrs, Laya.Handler.create(this, this.Complete), null);
                    }
                });
            }
        })
    }
    Complete(_data){
        
        ShopScene.Instance.CloseShop();
        ViewManager.Instance.ShowView(ViewType.IngameView);

        let stage = AccountManager.Instance.curplayerData.GainAccount("stage");
        WXSDK.AldStart(stage);
        AudioManager.playMusic(MusicType.click);
        MainGameView.Instance.play_id = _data.play_id;
        EventManager.StageTrigger(StageState.Start);
        ViewManager.Instance.ShowView(ViewType.IngameView);
    }
    Init(){
        this.data = this.GetTryOutTheme();
        this.count = this.data.length;
        if(this.count == 1){
            this.arr = this.data[0];
        }
        else{
            let index = GameTools.RandomANumber(0,this.count);
            this.arr = this.data[index];
        }
        ShopScene.Instance.CreateMoto(this.arr.id,this.SceneViewPort,1);
    }
    //获取人物
    public  GetTryOutTheme(): Array<any> {
        let arr = [];
        let themeCfg = AccountManager.Instance.curplayerData.GainCollocation("theme");
        let temp: any;
        if (themeCfg) {
            for (let index = 5; index < themeCfg.length; index++) {
                const element = themeCfg[index];
                let _id = element.id;
                if (!AccountManager.Instance.curplayerData.HasEquip(_id)) {
                    arr.push(element);
                }
                if (index == themeCfg.length-2) {
                    temp = element;
                }
            }
        }
        if (arr.length == 0) arr.push(temp);
        return arr;
    }
}
