import { ui } from "../ui/layaMaxUI";
import { GameTools } from "../Tool/GameTools";
import { SC } from "./ViewManager";
import { EventManager, PropEffecf,StageMode } from "../Tool/EventManager";
import { JsonManager } from "../Tool/JsonManager";
import { AccountManager } from "../Net/AccountManager";
import Ball from "../Main/Ball";
import { GameManager } from "../Main/GameManager";
import { GamePool } from "../Tool/GamePool";

export default class InGameView extends ui.InGameSceneUI implements SC{
    private static ins:InGameView;
    // public FallDownLine:number = 500;
    //本关卡
    public STAGE:number;
    //是否弹出失败界面
    public LoseView = false;
    constructor(){
        super();
        InGameView.ins = this;
        EventManager.StageOn(StageMode.Clone,this,this.AddPrefab);
        EventManager.StageOn(StageMode.Win,this,this.Win);
        EventManager.StageOn(StageMode.Lose,this,this.Lose);
        EventManager.StageOn(StageMode.ReStart,this,this.ReStart);
        EventManager.StageOn(StageMode.Over,this,this.Over)
        EventManager.StageOn(StageMode.Revive,this,this.Revive);
        // EventManager.StageOn(StageMode.Lose,this,this.)
    }
    public static get Instance():InGameView{
        if(InGameView.ins){
            return InGameView.ins
        }
        else{
            return new InGameView();
        }
    }
    Show(){
        let stage = AccountManager.Instance.curplayerData.GainAccount("stage");
        this.STAGE = Number(stage);

        Laya.timer.once(200,this,this.SkillRefresh)
        Laya.timer.frameLoop(2,this,this.CheckHeight);
        Laya.timer.frameLoop(1,this,this.Progress);
        Laya.timer.frameLoop(100,this,this.CheckLose);
        Laya.timer.frameLoop(500,this,this.SkillAnimation);
        this.ScoreRefresh();
        
        this.arrowbtn.clickHandler = new Laya.Handler(this,this.ClickArrow);
        this.zhunxin1btn.clickHandler = new Laya.Handler(this,this.ClickZhunXin1);
        this.zhunxin2btn.clickHandler = new Laya.Handler(this,this.ClickZhunXin2);
        this.doublebtn.clickHandler = new Laya.Handler(this,this.ClickDouble);
        this.hammerbtn.clickHandler = new Laya.Handler(this,this.ClickHammer);
        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;
        })
    }
    //添加预设
    AddPrefab(ball){
        this.prefabbox.addChild(ball);
    }
    //检测高度
    CheckHeight(){
        let down = false;
        for (let i = 0; i < this.prefabbox.numChildren; i++) {
            let child = this.prefabbox.getChildAt(i) as Laya.Image;
            if(child.y<GameManager.Instance.FallDownLine){
                down = true;
            }
        }
        if(!down){
            let num = GameTools.RandomANumber(2,4);
            GamePool.Instance.FallDown(num);
        }
    }
    //检测是否游戏失败
    CheckLose(){
        if(!this.LoseView){
            let lose = true;
            for (let i = 0; i < this.prefabbox.numChildren; i++) {
                let ball = this.prefabbox.getChildAt(i) as Laya.Image;
                let light = ball.getChildByName("light") as Laya.Image;
                if(light.visible){
                    lose = false;
                }
            }
            if(lose){
                this.LoseView = true;
                EventManager.StageTrigger(StageMode.Lose);
                
            }
        }
    }
    //开局刷新一下过关要求
    ScoreRefresh(){
        let level = JsonManager.Level;
        let score;
        let stage = level[this.STAGE-1];
        score = Number(stage.goal); 
        this.NowLv.value = this.STAGE.toString();
        this.NextLv.value = (this.STAGE+1).toString();
        this.NowScore.text = GameManager.Instance.ScoreProgress.toString();
        this.RequireScore.text = score.toString();
        // let stage = level[GameManager.Instance.STAGE];
        // score = Number(stage.goal); 
        // let id =Number(stage.id) ;
        // this.NowLv.value = id.toString();
        // this.NextLv.value = (id+1).toString();
        // this.NowScore.text = GameManager.Instance.ScoreProgress.toString();
        // this.RequireScore.text = score.toString();

    }
    //进入游戏时刷新一下技能icon
    SkillRefresh(){
        this.ani1.play(null,false);
        this.ani2.play(null,false);
        this.ani3.play(null,false);
        this.ani4.play(null,false);
        this.ani5.play(null,false);
    }
    //技能图标动画效果
    SkillAnimation() {
        let random = GameTools.RandomANumber(1, 6);
        switch (random) {
            case 1:
                this.ani1.play(null, false);
                break;
            case 2:
                this.ani2.play(null, false);
                break;
            case 3:
                this.ani3.play(null, false);
                break;
            case 4:
                this.ani4.play(null, false);
                break;
            case 5:
                this.ani5.play(null, false);
                break;
        }
    }
    //比例
    private get Ratio():number{
        let level = JsonManager.Level;
        let score;
        let stage = level[this.STAGE-1];
        score = Number(stage.goal); 
        let value = GameManager.Instance.ScoreProgress/score;
        return value;
    }
    //进度条
    Progress(){
        this.LevelProgress.value = this.Ratio;
        if(GameManager.Instance.ScoreProgress>=Number(this.RequireScore.text)){
            GameManager.Instance.ScoreProgress = Number(this.RequireScore.text);
            this.NowScore.text = GameManager.Instance.ScoreProgress.toString();
            EventManager.StageTrigger(StageMode.Win);
            return
        }
        this.NowScore.text = GameManager.Instance.ScoreProgress.toString();
    }
    //推球道具
    ClickArrow(){
        console.log("点到了箭头道具--");
        let num = this.arrowbtn.getChildByName("num") as Laya.Text;
        if(Number(num.text)>0){
            for (let i = 0; i < this.prefabbox.numChildren; i++) {
                let ball = this.prefabbox.getChildAt(i) as Laya.Image;
                if(ball.destroyed){
                    continue
                }
                else{
                    let _rig = ball.getComponent(Laya.RigidBody) as Laya.RigidBody;
                    _rig.applyLinearImpulseToCenter({x:0,y:-500});
                }
            }
            let num = this.arrowbtn.getChildByName("num") as Laya.Text;
            let propnum =Number(num.text); 
            num.text = (propnum-1).toString();
        }
    }
    //双倍道具
    ClickDouble(){
        if(EventManager.PropState == PropEffecf.DOUBLE){
            EventManager.EventTrigger(PropEffecf.NONE);
            console.log("切换为普通模式");
            return;
        }
        console.log("点到了X2道具--");
        let num = this.doublebtn.getChildByName("num") as Laya.Text;
        if(Number(num.text)>0){
            EventManager.EventTrigger(PropEffecf.DOUBLE);
            console.log("切换为双倍模式");
        }
    }
    //消除最小的球道具
    ClickZhunXin1(){
        console.log("点到了准心1道具--");
        let num = this.zhunxin1btn.getChildByName("num") as Laya.Text;
        if(Number(num.text)>0){
            if(EventManager.PropState == PropEffecf.NONE){
                this.EffectMode(true,PropEffecf.CLEARLAST);
            }
            else{
                this.EffectMode(false);
            }
        }
    }
    //消除次小的球道具
    ClickZhunXin2(){
        console.log("点到了准心2道具--");
        let num = this.zhunxin2btn.getChildByName("num") as Laya.Text;
        if(Number(num.text)>0){
            if(EventManager.PropState == PropEffecf.NONE){
                this.EffectMode(true,PropEffecf.CLEARTWO);
            }
            else{
                this.EffectMode(false);
            }
        }
    }
    //锤子道具
    ClickHammer(){
        console.log("点到了锤子道具--");
        let num = this.hammerbtn.getChildByName("num") as Laya.Text;
        if(Number(num.text)>0){
            if(EventManager.PropState == PropEffecf.NONE){
                this.EffectMode(true,PropEffecf.HAMMER);
            }
            else{
                this.EffectMode(false);
            }
        }
    }
    //使用道具
    EffectMode(_open:boolean,_type:PropEffecf = null){
        this.Effect.visible = _open;
        if (_open) {
            EventManager.PropState = _type;
            let describe2 = this.Effect.getChildByName("describe2") as Laya.Text;
            let describe = this.Effect.getChildByName("describe") as Laya.Text;
            switch (_type) {
                case PropEffecf.CLEARLAST:
                    this.EffectBtn.visible = true;
                    describe.visible = true;
                    describe2.visible = false;
                    this.ClearNum.text = GameManager.Instance.MinBall.toString();
                    this.EffectBtn.clickHandler = Laya.Handler.create(this, this.ClearLast);
                    break;
                case PropEffecf.CLEARTWO:
                    this.EffectBtn.visible = true;
                    describe.visible = true;
                    describe2.visible = false;
                    this.ClearNum.text = GameManager.Instance.MinNextBall.toString();
                    this.EffectBtn.clickHandler = Laya.Handler.create(this, this.ClearTwo);
                    break;
                case PropEffecf.HAMMER:
                    this.EffectBtn.visible = false;
                    describe.visible = false;
                    describe2.visible = true;
                    EventManager.EventTrigger(PropEffecf.HAMMER);
                    break;
            }
        }
        else{
            EventManager.PropState = PropEffecf.NONE;
            this.EffectBtn.clickHandler = null;
        }
    }
    //清除场景中最小的球
    ClearLast(){
        for (let i = this.prefabbox.numChildren-1; i >-1 ; i--) {
            let prefab = this.prefabbox.getChildAt(i) as Laya.Image;
            let ball = prefab.getComponent(Ball) as Ball;
            if(ball.score == GameManager.Instance.MinBall){
                GameManager.Instance.ScoreProgress += ball.score;
                prefab.removeSelf();
            }
        }
        let num = this.zhunxin1btn.getChildByName("num") as Laya.Text;
        let propnum =Number(num.text); 
        num.text = (propnum-1).toString();
        this.Effect.visible = false;
        this.EffectBtn.clickHandler = null;
        EventManager.PropState = PropEffecf.NONE;
    }
    //清除场景中第二小的球
    ClearTwo(){
        for (let i = this.prefabbox.numChildren-1; i >-1 ; i--) {
            let prefab = this.prefabbox.getChildAt(i) as Laya.Image;
            let ball = prefab.getComponent(Ball) as Ball;
            if(ball.score == GameManager.Instance.MinNextBall){
                GameManager.Instance.ScoreProgress += ball.score;
                prefab.removeSelf();
            }
        }
        let num = this.zhunxin2btn.getChildByName("num") as Laya.Text;
        let propnum =Number(num.text); 
        num.text = (propnum-1).toString();
        this.Effect.visible = false;
        this.EffectBtn.clickHandler = null;
        EventManager.PropState = PropEffecf.NONE;
    }
    //清除舞台上的球
    ClearStage(){
        if(this.prefabbox.destroyed)return
        for (let i = this.prefabbox.numChildren-1; i > -1; i--) {
            let ball = this.prefabbox.getChildAt(i) as Laya.Image;
            let _rig = ball.getComponent(Laya.RigidBody) as Laya.RigidBody;
            let _collider = ball.getComponent(Laya.CircleCollider) as Laya.CircleCollider;
            _collider.enabled = false;
            _rig.enabled = false;
            Laya.timer.clearAll(ball);
            ball.removeSelf();
            ball.destroy();
        }
    }
    //游戏结束
    Over(){
        this.ClearStage();
        EventManager.StageOff(StageMode.Clone,this,this.AddPrefab);
        EventManager.StageOff(StageMode.Win,this,this.Win);
        EventManager.StageOff(StageMode.Lose,this,this.Lose);
        EventManager.StageOff(StageMode.ReStart,this,this.ReStart);
        EventManager.StageOff(StageMode.Over,this,this.Over);
        EventManager.StageOff(StageMode.Revive,this,this.Revive);
        //清除碰撞器
        this.ClearCollider(this.BottomCollider);
        this.ClearCollider(this.RightCollider);
        this.ClearCollider(this.LeftCollider);
    }
    //复活
    Revive(){
        for (let i = this.prefabbox.numChildren-1; i >-1 ; i--) {
            let prefab = this.prefabbox.getChildAt(i) as Laya.Image;
            let ball = prefab.getComponent(Ball) as Ball;
            if(ball.score == GameManager.Instance.MinBall){
                GameManager.Instance.ScoreProgress += ball.score;
                prefab.removeSelf();
            }
        }
        Laya.timer.once(1000,this,()=>{
            this.LoseView = true;
            Laya.timer.frameLoop(2,this,this.CheckHeight);
        })
    }
    //游戏胜利
    Win(){
        Laya.timer.clear(this,this.CheckHeight);
    }
    //游戏失败
    Lose(){
        Laya.timer.clear(this,this.CheckHeight);
    }
    //再来一局
    ReStart(){
        this.ClearStage();
        this.ScoreRefresh();
        GamePool.Instance.FallDown(30);
    }
    //清除碰撞器
    ClearCollider(_node:Laya.Image){
        let collier = _node.getComponent(Laya.ChainCollider) as Laya.ChainCollider;
        let rig = _node.getComponent(Laya.RigidBody) as Laya.RigidBody;
        collier.enabled = false;
        rig.enabled = false;
        _node.removeSelf();
        _node.destroy();
    }
    Close(){
        Laya.timer.clearAll(this);
    }
    
    
}