import { ui } from "../ui/layaMaxUI";
import { SC, ViewManager, ViewType } from "./ViewManager";
import { EventManager, StageState } from "../Tools/EventManager";
import { StageControl } from "../Game/StageControl";
import { AudioManager, MusicType, BGMType } from "../Tools/AudioManager";
import { WXSDK } from "../Net/WXSDK";
import { AccountManager } from "../Net/AccountManager";
import { ServerManager, ArcherWorrior } from "../Net/ServerManager";
import { GameManager } from "../Game/GameManager";
import { SDKZS } from "../Net/SDKZS";
// import { ZSSdk } from "../Net/ZSSDK";

export default class MainGameView extends ui.MainGameViewUI implements SC{
    private static ins:MainGameView;
    public play_id:any ;
    public goaltext:Laya.Text;
    constructor()
    {
        super();
        MainGameView.ins = this;
    }
    public static get Instance():MainGameView{
        if(MainGameView.ins){
            return MainGameView.ins
        }
        else{
            return new MainGameView();
        }
    }
    public shake:Laya.Image;
    public sound:Laya.Image;
    Show(){
        WXSDK.AldEvent("打开主界面");
        AudioManager.PlayBGM(BGMType.BGM);
        this.BaoKuan.visible = false;
        this.StartBtn.clickHandler = Laya.Handler.create(this,this.ClickStart);
        this.SkinBtn.clickHandler = Laya.Handler.create(this,this.ClickSkin);
        this.MoreFunBtn.clickHandler =new Laya.Handler(this,this.ClickMoreFun);
        this.BaoKuanBtn.clickHandler = new Laya.Handler(this,this.OpenChouTi);
        this.goaltext = this.GoalProgress.getChildByName("goal") as Laya.Text;
        this.goaltext.text = GameManager.Instance.KillNum.toString();
        this.GoldNum.text = AccountManager.Instance.curplayerData.GainAccount("money");
        this.LevelNum.text = AccountManager.Instance.curplayerData.GainAccount("stage");
        this.SoundBox.on(Laya.Event.CLICK,this,this.ClickSound);
        this.sound = this.SoundBox.getChildByName("sound") as Laya.Image;
        this.shake = this.SoundBox.getChildByName("shake") as Laya.Image;
        this.sound.visible = AudioManager.open;
        this.shake.visible = WXSDK.shake;
        this.SetData();
        this.SetTopAd();
        this.height = Laya.stage.height;
        Laya.stage.on(Laya.Event.RESIZE,this,()=>
        {
            this.height = Laya.stage.height;
        })
    }
    Close(){
        Laya.stage.off(Laya.Event.MOUSE_DOWN,this,this.ClickStart);
    }
    ClickStart(){
        let stage = Number(AccountManager.Instance.curplayerData.GainAccount("stage"));
        if (stage > 2) {
            AudioManager.playMusic(MusicType.click);
            ViewManager.Instance.ShowView(ViewType.TrialView);
        }
        else {
            var agrs = [{ "t": AccountManager.Instance.ServerTime.toString() }];
            ServerManager.Instance.SendHttp(ArcherWorrior.KaiShi, agrs, Laya.Handler.create(this, this.GameStart), null);
        } 
    }
    ClickSkin(){
        AudioManager.playMusic(MusicType.click);
        ViewManager.Instance.ShowView(ViewType.ShopView);
    }
    ClickMoreFun(){
        ViewManager.Instance.OpenPopView(ViewType.ExportView);
    }
    ClickSound(){
        if(this.sound.visible){
            this.sound.visible = false;
            this.shake.visible = true;
            AudioManager.open = false;
            WXSDK.shake = true;
            AudioManager.StopMusic();
            console.log("关闭声音，打开震动---");
        }
        else{
            this.sound.visible = true;
            this.shake.visible = false;
            AudioManager.open = true;
            WXSDK.shake = true;
            AudioManager.PlayBGM(BGMType.BGM);
            console.log("打开声音，关闭震动");
        }
    }
    GameStart(_data:any){
        let stage = AccountManager.Instance.curplayerData.GainAccount("stage");
        WXSDK.AldStart(stage);
        AudioManager.playMusic(MusicType.click);
        this.play_id = _data.play_id;
        EventManager.StageTrigger(StageState.Start);
        ViewManager.Instance.ShowView(ViewType.IngameView);
    }
    OpenChouTi(){
        this.BaoKuanBtn.visible = false;
        this.BaoKuan.visible = true;
        this.CloseBaoKuan.clickHandler = Laya.Handler.create(this,this.CloseChouTi);
        this.chouti.play(null,false);
        this.InitAd();
    }
    CloseChouTi(){
        this.BaoKuan.visible = false;
        this.BaoKuanBtn.visible = true;
        this.CloseBaoKuan.clickHandler = null;
    }
    InitAd() {
        SDKZS.SetHuTuiList(this.BaoKuanList, 1, "");
        this.GunGun2();
    }
    GunGun2() {
        this.BaoKuanList.scrollBar.value = 0;
        var maxTop = this.BaoKuanList.scrollBar.max;
        var tempTop = 1;
        Laya.timer.frameLoop(1, this, () => {
            this.BaoKuanList.scrollBar.value += tempTop;
            if (this.BaoKuanList.scrollBar.value >= maxTop || this.BaoKuanList.scrollBar.value <= 0) {
                tempTop = -tempTop;
            }
        });
    }

    private SetData() {
        SDKZS.SetHuTuiList(this.BottomList, 2, "");
        this.GunGun();
    }
    GunGun() {
        var maxBtm;
        try {
            this.BottomList.scrollBar.value = 0;
            maxBtm = this.BottomList.scrollBar.max;
        } catch (error) {
            console.error(error);
            return;
        }
        var tempBtm = 1;
        Laya.timer.frameLoop(1, this, () => {
            this.BottomList.scrollBar.value += tempBtm;
            if (this.BottomList.scrollBar.value >= maxBtm || this.BottomList.scrollBar.value <= 0) {
                tempBtm = -tempBtm;
            }
        });
    }

    topAdData: any;
    topLeftAdIndex: any;
    topRightAdIndex: any;
    //左右抖动互推
    SetTopAd() {
        this.topAdData = SDKZS.data.promotion || [];
        this.LeftPushIcon.visible = this.RightPushIcon.visible = this.topAdData.length >= 2;
        if (this.topAdData.length < 2) return;
        this.topLeftAdIndex = { index: 0 };
        this.topRightAdIndex = { index: 1 };
        this._setTopAd(this.LeftPushIcon, this.topLeftAdIndex);
        this._setTopAd(this.RightPushIcon, this.topRightAdIndex);
    }
    private _setTopAd(topAd: Laya.Button, adIndex: any) {
        let _icon = topAd.getChildByName("icon") as Laya.Image;
        let _name = topAd.getChildByName("name") as Laya.Text;
        topAd.clickHandler =new Laya.Handler(this, () => {
            SDKZS.navigate2Mini(this.topAdData[adIndex.index])
        });
        this._setIcon(_icon,_name, adIndex);
        Laya.timer.loop(5000, this, () => {
            this._setIcon(_icon,_name, adIndex);
            this.lefticon.play(null, false);
            this.righticon.play(null, false);
        });
    }
    private _setIcon(_icon: Laya.Image, _name:Laya.Text, adIndex: any) {
        adIndex.index = (adIndex.index + 2) % this.topAdData.length;
        _icon.skin = this.topAdData[adIndex.index].app_icon;
        _name.text = this.topAdData[adIndex.index].app_title;
    }

}