const { ccclass, property } = cc._decorator;

@ccclass
export default class AnimInAndOut extends cc.Component {

    @property(cc.Node)
    popNode: cc.Node = null;

    @property
    adjustPopNodeScale: number = 1;

    private isDestroying: boolean = false;

    onLoad() {
        // onLoad要执行的全都在onEnable中进行了
    }

    onEnable() {
        this.popNode.opacity = 0;
        this.popNode.setScale(0.1);
        var fadeIn = cc.fadeIn(0.2);
        var scaleTo = cc.scaleTo(0.2, this.adjustPopNodeScale);
        this.popNode.runAction(cc.spawn(fadeIn, scaleTo));
    }

    onQuit() {
        this.animateAndDestroy();
    }

    animateAndDestroy(callback: Function = null) {
        if (this.isDestroying) {
            return;
        }
        this.isDestroying = true;
        this.popNode.setScale(this.adjustPopNodeScale);
        var fadeOut = cc.fadeOut(0.2);
        var scaleTo = cc.scaleTo(0.2, 0);
        var callFunc = cc.callFunc(() => {
            if (this.node) {
                this.node.destroy();
            }
            callback && callback();
        });
        this.popNode.runAction(cc.sequence(cc.spawn(fadeOut, scaleTo), callFunc));
    }

    animateAndDisable() {
        if (this.isDestroying) {
            return;
        }
        this.isDestroying = true;

        var fadeOut = cc.fadeOut(0.2);
        var scaleTo = cc.scaleTo(0.2, 0);
        var callFunc = cc.callFunc(() => {
            this.node.active = false;
        });
        this.node.runAction(cc.sequence(cc.spawn(fadeOut, scaleTo), callFunc));
    }
}
