import { ui } from "../ui/layaMaxUI";
import { SC, ViewType, ViewManager } from "./ViewManager";
import { ServerManager, BlastToMerge } from "../Net/ServerManager";
import { AccountManager } from "../Net/AccountManager";
import MainView from "./MainView";
import { EventManager, StageMode } from "../Tool/EventManager";
import { GameManager } from "../Main/GameManager";
import { DropRule } from "../Tool/DropRule";
import { WXSDK } from "../Net/WXSDK";

export default class WinView extends ui.WinViewUI implements SC{
    private static ins:WinView;
    constructor(){
        super();
    }
    public static get Instance():WinView{
        if(WinView.ins){
            return WinView.ins
        }
        else{
            return new WinView();
        }
    }
    Show(){
        this.BackBtn.clickHandler = Laya.Handler.create(this,this.ClickBack);
        this.NextBtn.clickHandler = Laya.Handler.create(this,this.ClickNextLevel);
        this.height = Laya.stage.height; 
        this.BG.height = Laya.stage.height;
        Laya.stage.on(Laya.Event.RESIZE,this,()=>
        {
            this.height = Laya.stage.height;
            this.BG.height = Laya.stage.height;
        })
    }
    Close(){
        Laya.timer.clearAll(this);
    }
    //返回主界面
    ClickBack(){
        var args = [{"t":AccountManager.Instance.ServerTime.toString()},{"play_id":GameManager.Instance.play_id},{"win":"1"}];
        ServerManager.Instance.SendHttp(BlastToMerge.JieShu,args,Laya.Handler.create(this,this.BackMain),null);
    }
    //下一关
    ClickNextLevel(){
        var args = [{"t":AccountManager.Instance.ServerTime.toString()},{"play_id":GameManager.Instance.play_id},{"win":"1"}];
        ServerManager.Instance.SendHttp(BlastToMerge.JieShu,args,Laya.Handler.create(this,this.NextLevel),null);
    }
    //返回主界面
    BackMain(){
        
        EventManager.StageTrigger(StageMode.Over);
        EventManager.StageTrigger(StageMode.Refresh);
        ViewManager.Instance.ShowView(ViewType.MainView);
        let level = DropRule.Instance.STAGE;
        WXSDK.UpdateScore(level);
    }
    //下一关
    NextLevel(){
        var agrs = [{ "t": AccountManager.Instance.ServerTime.toString() }];
        ServerManager.Instance.SendHttp(BlastToMerge.KaiShi, agrs, Laya.Handler.create(this, this.StartNextLevel), null);
    }
    //开始下一关
    StartNextLevel(_data:any){
        ViewManager.Instance.ClosePopView(ViewType.WinView);
        let stage = AccountManager.Instance.curplayerData.GainAccount("stage");
        GameManager.Instance.play_id = _data.play_id;
        DropRule.Instance.STAGE = Number(AccountManager.Instance.curplayerData.GainAccount("stage"));
        // DropRule.Instance.Init();
        //提前更新关卡信息
        EventManager.StageTrigger(StageMode.Refresh,[true]);
        EventManager.StageTrigger(StageMode.ReStart,[true]);
        let level = DropRule.Instance.STAGE;
        WXSDK.UpdateScore(level);
    }

} 