import { _decorator, Button, Component,director,game,Node,Sprite, SpriteFrame } from 'cc';
import { MainScene } from './MainScene';
const { ccclass, property } = _decorator;

@ccclass('Menu')
export class Menu extends Component {
    //加速Buton
    @property(Button)
    btnSpeed:Button = null;
    @property(SpriteFrame)
    imgSpeed_1:SpriteFrame = null;
    @property(SpriteFrame)
    imgSpeed_2:SpriteFrame = null;
    //暂停Button
    @property(Button)
    btnPause:Button = null;
    @property(SpriteFrame)
    imgPause_1:SpriteFrame = null;
    @property(SpriteFrame)
    imgPause_2:SpriteFrame = null;

    @property(SpriteFrame)
    imgMenuCenter_1:SpriteFrame = null;//第几波怪物
    @property(SpriteFrame)
    imgMenuCenter_2:SpriteFrame = null;//暂停中

    //弹出框
    //@property(Node)
    dialog:Node = null;

    isAccelerate:boolean = false;//是否加速
    currentState:boolean = null;//当前状态

    isPause:boolean = false;//是否暂停
    currentPauseState:boolean = null;//当前状态

    currentSpeed:number = null;//当前速度

    start(){
        this.dialog = this.node.getChildByName("Dialog");
        this.dialog.active = false;

        this.currentSpeed = 100;//将初始速度赋值给当前速度

        // let btnQuit = this.dialog.getChildByName("BtnQuit");
        // btnQuit.on(Button.EventType.CLICK,this.onBtnQuitDialog,this)

        // let btnSelect = this.dialog.getChildByName("BtnSelect");
        // btnSelect.on(Button.EventType.CLICK,this.onBtnSelectDialog,this);

        // let btnRestart = this.dialog.getChildByName("BtnRestart");
        // btnRestart.on(Button.EventType.CLICK,this.onBtnRestartDialog,this);

        // let btnResume = this.dialog.getChildByName("BtnResume");
        // btnResume.on(Button.EventType.CLICK,this.onBtnQuitDialog,this);

        const buttonConfigs = [
            {name : "BtnResume",handler : this.onBtnQuitDialog},
            {name : "BtnRestart",handler : this.onBtnRestartDialog},
            {name : "BtnSelect",handler : this.onBtnSelectDialog},
            {name : "BtnQuit",handler : this.onBtnQuitDialog}
        ]
        
        buttonConfigs.forEach(config =>{
            let button = this.dialog.getChildByName(config.name);
            button.on(Button.EventType.CLICK,config.handler,this);
        });
        
        this.currentState = this.isAccelerate; //当前状态没有加速
        this.btnSpeed.getComponent(Sprite).spriteFrame = this.imgSpeed_1;

        this.currentPauseState = this.isPause;
        this.btnPause.getComponent(Sprite).spriteFrame = this.imgPause_1;

        this.node.getChildByName("MenuCenter_01_CN").getComponent(Sprite).spriteFrame = this.imgMenuCenter_1;
    }

    //加速
    onBtnSpeed(){
        let btnSpeedComponent = this.btnSpeed.getComponent(Sprite);
        //获取父节点MainScene的脚本组件
        let mainSceneTS = this.node.parent.getComponent(MainScene);

        this.isAccelerate = !this.isAccelerate;//加速
        this.currentState = this.isAccelerate;//当前状态为加速
        if(!this.isAccelerate){
            //一倍速
            mainSceneTS.speed = this.currentSpeed;
            btnSpeedComponent.spriteFrame  = this.imgSpeed_1;
        }else{
            //二倍速
            mainSceneTS.speed = mainSceneTS.speed * 2
            btnSpeedComponent.spriteFrame  = this.imgSpeed_2;
        }
    }

    //暂停游戏
    onBtnPause(){
        let menuCenterComponent = this.node.getChildByName("MenuCenter_01_CN").getComponent(Sprite);
        let btnPauseComponent = this.btnPause.getComponent(Sprite);
        this.isPause = !this.isPause;//暂停
        this.currentPauseState = this.isPause;//当前状态为暂停
        if(!this.isPause){
            menuCenterComponent.spriteFrame = this.imgMenuCenter_1;
            btnPauseComponent.spriteFrame  = this.imgPause_1;
            //恢复暂停
            if(director.isPaused){
                director.resume();
                return;
            }
        }else{
            //暂停
            director.pause();
            menuCenterComponent.spriteFrame = this.imgMenuCenter_2;
            btnPauseComponent.spriteFrame  = this.imgPause_2;
        }
    }

    //弹出dialog
    onBtnMore(){
        console.log("More")
        director.pause();
        this.dialog.active = true;
    }
    //退出 恢复暂停
    onBtnQuitDialog(){
        console.log("quitDialog");
        if(director.isPaused()){
            director.resume();
            return;
        }
        this.dialog.active = false;
    }

    //重新选择关卡
    onBtnSelectDialog(){
        director.loadScene("SelectLevel");
        console.log("SelectLevel");
    }

    //重新开始游戏
    onBtnRestartDialog(){
        console.log("Restart");
    }

    update(deltaTime: number) {
        
    }
}


