// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Node)
    Map: cc.Node = null;
    @property(cc.Prefab)
    tip: cc.Prefab = null;
    @property(cc.Prefab)
    reinforce: cc.Prefab = null;
    @property(cc.Prefab)
    soil: cc.Prefab = null;
    

    tipArray:any;
    canTouch:boolean;
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {
        this.tipArray = [];
        this.canTouch = true;
    }

    setPosition(tip){
        tip.setPosition(45,-35);
        if(this.tipArray.length > 0){
            let length = this.tipArray.length+1;
            let posY = Math.ceil(length/5) - 1;
            let posX = length - Math.floor(posY)*5 - 1;
            tip.setPosition(45 + 65*posX,-35 -60*posY);
        }
    }

    removeAllTip(){
        if(!this.canTouch) return;
        this.tipArray = [];
    }

    back_Click(){
        if(!this.canTouch) return;
        if(this.tipArray.length > 0){
            let tip = this.tipArray[this.tipArray.length-1];
            tip.active = false;
            tip.removeFromParent(this.Map);
            tip = null;
            this.tipArray.pop();
        }
    }


    btn_Click(target,data){
        if(!this.canTouch) return;
        let prefab = this.tip;
        if(data == "reinforce" || data == "soil") prefab = this[data];
        let tip = cc.instantiate(prefab);
        if(data == "up") tip.angle = 180;
        if(data == "left") tip.angle = -90;
        if(data == "right") tip.angle = 90;
        tip.parent = this.Map;
        this.setPosition(tip);
        this.tipArray.push(tip);
        cc.fx.Notifications.emit(cc.fx.Message.control,data);
    }


    start_Click(){
        if(!this.canTouch) return;
        this.canTouch = false;
        cc.fx.Notifications.emit(cc.fx.Message.startGame,null);
    }

    // update (dt) {}
}
