// 主游戏控制类
const {ccclass, property} = cc._decorator;
@ccclass
export default class GameManager extends cc.Component {
    countTime: number;  //游戏总daojishi
    tube1: cc.Node;     //管子1
    tube3: cc.Node;     //管子2
    tube5: cc.Node;     //管子3
    ball1: cc.Node;     //球1
    ball2: cc.Node;     //球2
    ball3: cc.Node;     //球3
    tube_Array: number[][];//管子数组

    onLoad () {}
    start () {
        this.fit();
        this.init();
    }
    //初始化数据
    init(){
        this.tube1 = this.node.getChildByName("tube1");
        this.tube3 = this.node.getChildByName("tube3");
        this.tube5 = this.node.getChildByName("tube5");
        this.ball1 = this.node.getChildByName("ball1");
        this.ball2 = this.node.getChildByName("ball2");
        this.ball3 = this.node.getChildByName("ball3");
        this.tube_Array = [[1,3],[2],[]];

        this.initBall();
    }

    initBall(){
        for(let i=0; i<this.tube_Array.length; i++){
            if(this.tube_Array[i].length > 0 ){
                for(let j =0; j<this.tube_Array[i].length; j++){
                    let name = "ball" + this.tube_Array[i][j];
                    this[name].active = true;
                    this[name].setPosition((i-1)*210,50+j*100);
                    this[name].getComponent("Ball").init(j==(this.tube_Array[i].length-1)?true:false,this.tube_Array[i][j]);
                }
            }
        }
    }

    moveTube(data){
        this.tube_Array[data.tube].push(data.id);
        var tube_Arr = this.tube_Array[1];
        if(data.pos.x == -210){
            tube_Arr = this.tube_Array[0];
        }
        else if(data.pos.x == 210){
            tube_Arr = this.tube_Array[2];
        }
        tube_Arr.pop();
        if(data.pos.y == 150){
          
        }
    }


    //根据是否全面屏，做独立适配方面
    fit(){
        var jg = this.setFit();
        if(!jg){

        }
    }
    //判断全面屏
    getSetScreenResolutionFlag () {
        let size = cc.winSize;
        let width = size.width;
        let height = size.height;
        if ((height / width) > (16.2 / 9)) return false;
        return true;
    }
    //判断全面屏适配
    setFit () {
        let flag = this.getSetScreenResolutionFlag();
        if (flag) {
        } else {
        }
        return flag;
    }
    //返回首页
    backScene(){
        cc.director.loadScene("LoadScene");
    }
    //下一关，或者重新开始，或者返回上一关，根据level决定
    reStart(type){

    }
    //获取时间戳
    getTime(){
        const timestamp = new Date().getTime();
        return timestamp;
    }
    //获胜
    passLevel(){

    }
    //失败
    loseLevel(type){
  
    }

    //开始游戏
    startGame(){

    }


    //如果是倒计时 调用此方法
    updateCountDownTime () {
        if (this.countTime > 0) {
            this.countTime -= 1;
            // this.time.string =  cc.fx.GameTool.getTimeMargin(this.countTime);
            if(this.countTime < 5){
                // cc.tween(this.time.node)
                // .to(0.25,{scale:1.5,color:cc.color(255,0,0)})
                // .to(0.25,{scale:1,color:cc.color(255,255,255)})
                // .start()
                let over = this.node.getChildByName("Over");
                cc.tween(over)
                .to(0.2,{opacity:255})
                .delay(0.1)
                .to(0.2,{opacity:0})
                .start();
            }
            if(this.countTime <= 0){
                this.unschedule(this.updateCountDownTime);
                var time = 0;
                this.gameOver(time);
            }
        }
    }
    //上传每次操作数据
    setData(){
         cc.fx.GameTool.setGameData();
    }
    //上传排行榜数据
    gameOver(time){
         cc.fx.GameTool.setRank(time);
        this.node.getChildByName("GameOver").active = true;
        this.node.getChildByName("GameOver").opacity = 0;
        cc.tween(this.node.getChildByName("GameOver"))
        .to(0.4,{opacity:255})
        .delay(2)
        .to(0.4,{opacity:50})
        .call(() =>{
            cc.director.loadScene("OverScene");
        })
        .start()
    }

    onEnable () {
        setTimeout(() => {
            cc.fx.Notifications.on("moveTube", this.moveTube, this);
        }, 100);

    }
    onDisable () {
        cc.fx.Notifications.off("moveTube", this.moveTube);
    }
    update (dt) {
    }
}
