// 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;
    controlArray:any;
    canTouch:boolean;
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

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

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

    removeAllTip(){
        if(!this.canTouch) return;
        for(let i=0; i<this.tipArray.length;i++){
            let tip = this.tipArray[i];
            tip.active = false;
            tip.removeFromParent(this.Map);
            tip = null;
        }
        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();
            this.controlArray.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;
      

        if(this.controlArray[this.controlArray.length-1] == "reinforce" || 
            this.controlArray[this.controlArray.length-1] == "soil"){
                if(data == "reinforce" || data == "soil"){
                    this.tipArray[this.tipArray.length-1].removeFromParent();
                    this.tipArray[this.tipArray.length-1] = null;
                    this.tipArray.pop();
                    this.controlArray.pop();
                }
        }
        this.setPosition(tip);

        this.tipArray.push(tip);
        this.controlArray.push(data);
        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,this.controlArray);
    }

    // update (dt) {}
}
