import { _decorator, Component, Animation } from 'cc';
const { ccclass, requireComponent } = _decorator;

@ccclass('AnimEvent')
@requireComponent([Animation])
export class AnimEvent extends Component {
    private _animation: Animation = null;

    playCallback: Function = null;              //开始播放时触发
    stopCallback: Function = null;              //停止播放时触发
    pauseCallback: Function = null;             //暂停播放时触发
    resumeCallback: Function = null;            //恢复播放时触发
    lastFrameCallback: Function = null;         //假如动画循环次数大于1,当动画播放到最后一帧时触发
    finishedCallback: Function = null;          //动画完成播放时触发

    onLoad() {
        this._animation = this.getComponent(Animation);
        this._animation.on(Animation.EventType.PLAY, () => {
            this.playCallback && this.playCallback(this);
        }, this);
        this._animation.on(Animation.EventType.STOP, () => {
            this.stopCallback && this.stopCallback(this);
        }, this);
        this._animation.on(Animation.EventType.PAUSE, () => {
            this.pauseCallback && this.pauseCallback(this);
        }, this);
        this._animation.on(Animation.EventType.RESUME, () => {
            this.resumeCallback && this.resumeCallback(this);
        }, this);
        this._animation.on(Animation.EventType.LASTFRAME, () => {
            this.lastFrameCallback && this.lastFrameCallback(this);
        }, this);
        this._animation.on(Animation.EventType.FINISHED, () => {
            this.finishedCallback && this.finishedCallback(this);
        }, this);
    }

    play(name?: string) {
        this.node.active = true;
        this._animation.play(name);
    }

    stop() {
        this._animation.stop();
        this.node.active = false;
    }

    crossFade(name: string, duration?: number) {
        this._animation.crossFade(name, duration);
    }

    pause() {
        this._animation.pause();
    }

    resume() {
        this._animation.resume();
    }

    getState(name: string) {
        return this._animation.getState(name);
    }
}