import GameData from "./GameData";
import GameManager from "./GameManager";
import { Notification } from './tool/Notification';
const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';
    tween: cc.Tween<cc.Node>;
    speed: number;  //移动速度，时间参数，位移个固定
    move: boolean;  //是否在移动状态。
    num: number;
    difficulty: number;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.node.getComponent(cc.PolygonCollider).enabled = true;
        this.speed = 10;
        this.num = 0;
        this.move = false;
        if(this.node.name.length > 6){
            this.num = parseInt(this.node.name.substring(5,7))
        }else  this.num = parseInt(this.node.name.substring(5,6))
        this.speed = GameData._instance.BLOCK_INFO[this.num-1].speed;
        this.init();
    }

    start () {
    }

    init(){
        let speedTemp = this.speed;
        this.getSpeed();
        if(this.num > GameData._instance.GM_INFO.blockScale){
            if(this.speed < GameData._instance.LEVEL_INFO.slowSpeed)this.speed = GameData._instance.LEVEL_INFO.slowSpeed;
        }
        else {
            if(this.speed < GameData._instance.LEVEL_INFO.fastSpeed)this.speed = GameData._instance.LEVEL_INFO.fastSpeed;
        }
        if(this.speed > speedTemp) this.speed = speedTemp;

        if(this.num != 13 && !GameData._instance.GM_INFO.probation){
            GameData._instance.CLICK_DATA.block = this.num;
            GameData._instance.CLICK_DATA.speed = this.speed;
            this.getDifficulty();
            GameManager._instance.Air2.push(this.speed);
            if(GameManager._instance.Air2.length > 99){
                console.log("被限制后",GameManager._instance.Air2);
            }
        }
        else{
            if(GameManager._instance.block){
                let random = parseInt(Math.random()*3+"") + 1
                this.speed = GameManager._instance.block.getComponent("Block").speed + random;
            }
            this.node.getComponent(cc.PolygonCollider).enabled = false;
            this.node.getComponent(cc.BoxCollider).enabled = false;
            GameData._instance.CLICK_DATA.fakeSpeed = this.speed;
        }
        this.move = true;
        this.tween = 
        cc.tween(this.node)
        .to(this.speed,{x:-this.node.x})
        .call(() =>{
            this.move = false;
        })
        .start();
    }

    getSpeed(){
        //先加范围  厚的
        let reduceSpeed = GameData._instance.LEVEL_INFO.reduceSpeed;
        if(reduceSpeed > 35) reduceSpeed = 35
        reduceSpeed = parseInt(Math.random()*reduceSpeed + "");
        let random = Math.random()*10;
        let max = 1;
        if(GameData._instance.LEVEL_INFO.speed < 1.3) max = 9;
        else if(GameData._instance.LEVEL_INFO.speed < 1.5) max = 7;
        else if(GameData._instance.LEVEL_INFO.speed < 1.8) max = 5;
        else if(GameData._instance.LEVEL_INFO.speed < 2) max = 3;
        if(random > max) reduceSpeed = -reduceSpeed;
        //先乘以系数
        this.speed = this.speed/GameData._instance.LEVEL_INFO.speed;
        this.speed = parseInt(this.speed*10/10 + "");
        //后减去随机值 
        reduceSpeed = parseInt(reduceSpeed*10/10 + "");
        this.speed = this.speed - reduceSpeed/10;
        if(this.num != 13 ){
            GameManager._instance.Air.push(this.speed);
            if(GameManager._instance.Air.length == 99){
                console.log("初始数据",GameManager._instance.Air);
            }
        }
    }

    getDifficulty(){
        var difficulty1 = 0;
        var sudu = Math.abs(this.speed - GameManager._instance.oldSpeed);
        difficulty1 = (sudu/10+1)*(11-this.speed);
        if(difficulty1 <= 1) difficulty1 = 1;
        else if(difficulty1 >= 7) difficulty1 = 7;
        var difficulty2 = 0;
        if(this.node.width < 180) difficulty2 = 0;
        else if(this.node.width < 199) difficulty2 = 0.5;
        else difficulty2 = 1;
        var difficulty3 = 0;
        if(GameManager._instance.interfere == true){
            difficulty3 = 1;
        } 
        var difficulty4 = 0;
        if(this.num > 6){
            difficulty4 = 1;
        }
        GameManager._instance.oldSpeed = this.speed;
        this.difficulty  = 0;
        this.difficulty = difficulty1 + difficulty2 + difficulty3 + difficulty4;
        this.difficulty  = parseInt(this.difficulty *10 + "");
        GameData._instance.CLICK_DATA.difficulty = this.difficulty/10;
        GameManager._instance.difficultyArray +=  GameData._instance.CLICK_DATA.difficulty;
        if(GameData._instance.CLICK_DATA.difficulty > GameData._instance.GM_INFO.difficultyMax)
            GameData._instance.GM_INFO.difficultyMax = GameData._instance.CLICK_DATA.difficulty;
    }

    setScore(fen){
        let score = 0;
        let difficulty = this.difficulty / 10;
        let layer = GameData._instance.LEVEL_INFO.layer;
        let doubleHit = GameData._instance.LEVEL_INFO.doubleHit;
        score = parseInt(fen * difficulty * layer * doubleHit + "");
        Notification.emit("addScore",score);
    }

    hide(){
        cc.tween(this.node)
        .to(0.2,{opacity:0})
        .call(() =>{
            this.node.active = false;
            this.node.removeFromParent();
            this.node = null;
        })
        .start();
    }

    onCollisionEnter(other: cc.Collider, self: cc.Collider) {
        if(GameManager._instance){
            if(GameManager._instance.over == true){
                return;
            }
        }
        if(other.node.name == "Player"){
            let max = self.name.length;
            let ColliderName = other.name.substring(6,max);
            if(this.move == true){
                if(this.tween){
                    this.tween.stop(); 
                }
            } 
        }
    }

    update (dt) {
    }
}
