import { ui } from "../ui/layaMaxUI";
import { ViewManager, ViewType } from "./ViewManager";
import { DispatcherManager, StageState } from "../GameUtils/EventManager";
import { PlayerManager } from "../Manager/PlayerManager";
import { AccountManager } from "../NetWork/AccountManager";
import { ServerManager, TeamWave } from "../NetWork/ServerManager";
import { GameManager } from "../Manager/GameManager";
import { AudioManager, BGMType } from "../Manager/AudioManager";
import { WXSDK } from "../NetWork/WXSDK";
export class HomeView extends ui.HomeViewUI{
    private static ins:HomeView;
    constructor(){
        super();
        HomeView.ins = this;
    }
    public static get Instance():HomeView{
        if(HomeView.ins == null){
            return new HomeView();
        }
        return HomeView.ins;
    }
    OnShow(){
        console.log("主界面");
        this.RefreshUI();
        this.RegButton();
        this.handclick.play(null,true);
    }
    RegButton(){
        this.StartBtn.clickHandler = new Laya.Handler(this,this.Start);
        this.SignBtn.clickHandler = new Laya.Handler(this,this.ClickSign);
        this.LotteryBtn.clickHandler = new Laya.Handler(this,this.ClickLottery);
        this.SkinBtn.clickHandler = new Laya.Handler(this,this.ClickSKin);
        this.SetBtn.clickHandler = new Laya.Handler(this,this.ShowSetPanel);
        this.CloseSetBtn.clickHandler = new Laya.Handler(this,this.HideSetPanel);
        this.shakebtn.clickHandler = new Laya.Handler(this,this.ClickShake);
        this.soundbtn.clickHandler = new Laya.Handler(this,this.ClickSound);
        

        this.height = Laya.stage.height;
        Laya.stage.on(Laya.Event.RESIZE,this,()=>
        {
            this.height = Laya.stage.height;
        })
    }
    RefreshUI(){
        let stage = Number(AccountManager.Instance.curplayerData.GainAccount("stage")) +1  ;
        this.NowLevelNum.text = stage + "";

        this.GoldNum.text = AccountManager.Instance.curplayerData.GainAccount("money");
    }
    Start(){
        var agrs = [{ "t": AccountManager.Instance.ServerTime.toString() }];
        ServerManager.Instance.SendHttp(TeamWave.KaiShi, agrs, Laya.Handler.create(this,(data)=>{
            GameManager.play_id = data.play_id;
            PlayerManager.Instance.EnablePlayer();
            this.handclick.stop();
            this.shou.visible = false;
            ViewManager.Instance.ShowView(ViewType.InGameView);
            DispatcherManager.StageTrigger(StageState.Start);
        }), null);
    }
    ShowSetPanel(){
        this.SetPanel.visible = true;

        let shakeon = this.SetPanel.getChildAt(0).getChildByName("shake").getChildByName("on") as Laya.Image;
        let shakeoff = this.SetPanel.getChildAt(0).getChildByName("shake").getChildByName("off") as Laya.Image;
        let soundon = this.SetPanel.getChildAt(0).getChildByName("sound").getChildByName("on") as Laya.Image;
        let soundoff = this.SetPanel.getChildAt(0).getChildByName("sound").getChildByName("off") as Laya.Image;
        soundon.visible = AudioManager.play;
        soundoff.visible = !soundon.visible;

        shakeon.visible = WXSDK.shake;
        shakeoff.visible = !shakeon.visible;
    }
    HideSetPanel(){
        this.SetPanel.visible = false;
    }
    ClickSound(){
        let soundon = this.SetPanel.getChildAt(0).getChildByName("sound").getChildByName("on") as Laya.Image;
        let soundoff = this.SetPanel.getChildAt(0).getChildByName("sound").getChildByName("off") as Laya.Image;
        AudioManager.play = !AudioManager.play;
        soundon.visible = AudioManager.play;
        soundoff.visible = !soundon.visible;
        if(AudioManager.play){
            AudioManager.PlayBGM(BGMType.bgm);
        }
        else{
            AudioManager.StopMusic();
        }
    }
    ClickShake(){
        let shakeon = this.SetPanel.getChildAt(0).getChildByName("shake").getChildByName("on") as Laya.Image;
        let shakeoff = this.SetPanel.getChildAt(0).getChildByName("shake").getChildByName("off") as Laya.Image;
        WXSDK.shake = !WXSDK.shake;
        shakeon.visible = WXSDK.shake;
        shakeoff.visible = !shakeon.visible;
    }
    ClickSign(){
        WXSDK.ShowTxt("暂未开放");
        // ViewManager.Instance.ShowView(ViewType.SignView);
    }
    ClickLottery(){
        WXSDK.ShowTxt("暂未开放");
        // ViewManager.Instance.ShowView(ViewType.LotteryView);
    }
    ClickSKin(){
        WXSDK.ShowTxt("暂未开放");
    }
    OnHide(){
        
    }
}