// 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

import MapConroler from "../Map";
import NumberToImage from "../NumberToImage";

const { ccclass, property } = cc._decorator;



@ccclass
export default class Adhesive extends cc.Component {
    static _instance: any;
    time: number = 60;
    lockTime: number = 0;
    pos: any = { x: 0, y: 0 };
    target: any;

    onLoad() {
        this.pos.x = this.pos.y = 0;
        this.lockTime = -1;
        this.target = null;
    }

    start() {
    }

    init(node, lockTime) {
        this.pos.x = node.x - this.node.x;
        this.pos.y = node.y - this.node.y;
        this.target = node;
        this.lockTime = lockTime;
        if (this.lockTime > 0) {
            NumberToImage.numberToImageNodes(this.lockTime, 28, 8, "time_", this.node.getChildByName("heng").getChildByName("num"), false);
            NumberToImage.numberToImageNodes(this.lockTime, 28, 8, "time_", this.node.getChildByName("shu").getChildByName("num"), false);
        }
    }

    remove() {
        this.target = null;
        for (let j = 0; j < this.node.children.length; j++) {
            if (this.node.children[j].active == true) {
                this.node.children[j].getComponent(sp.Skeleton).setAnimation(0, "animation", false);
            }
        }
        if (this.lockTime != -1) {
            cc.tween(this.node.getChildByName("heng").getChildByName("num"))
                .to(0.2, { opacity: 0 })
                .start();
            cc.tween(this.node.getChildByName("shu").getChildByName("num"))
                .to(0.2, { opacity: 0 })
                .start();
        }


        setTimeout(() => {
            if (this.node) {
                this.node.active = false;
                this.node.removeFromParent();
                this.node = null;
            }
        }, 2000);
    }

    reduce() {
        if (this.lockTime > 0 && this.target != null && this.node.active == true) {
            this.lockTime--;
            NumberToImage.numberToImageNodes(this.lockTime, 20, 8, "time_", this.node.getChildByName("heng").getChildByName("num"), false);
            NumberToImage.numberToImageNodes(this.lockTime, 20, 8, "time_", this.node.getChildByName("shu").getChildByName("num"), false);
            cc.fx.GameTool.numberScaleAction(this.node.getChildByName("heng").getChildByName("num"));
            cc.fx.GameTool.numberScaleAction(this.node.getChildByName("shu").getChildByName("num"));
            if (this.lockTime == 0) {
                this.target.getComponent("Block").removeSpecailAdhesive(false);
            }
        }
    }


    update(dt) {
        if (this.target != null) {
            this.node.x = this.target.x - this.pos.x;
            this.node.y = this.target.y - this.pos.y;
        }
    }
}
