/**
 * 延迟播放/暂停.
 */


import { SpriteFrames } from "./SpriteFrames";

 
const {ccclass, property, disallowMultiple} = cc._decorator;

@ccclass
@disallowMultiple
export class DelayPlay extends cc.Component {

    @property({type: cc.Enum({"粒子": 0, "动画": 1, "龙骨": 2, "spine": 3, "帧动画": 4}), displayName: "类型."})
    type: number = 0;

    @property({displayName: "延迟播放."})
    isDelayPlay: boolean = false;

    @property({displayName: "延迟时间.", min: 0, visible() { return this.isDelayPlay; }})
    delay: number = 0;

    @property({displayName: "动画名称.", visible() { return this.isDelayPlay && (this.type === 2 || this.type === 3) }})
    aniName: string = "";

    @property({displayName: "循环.", visible() { return this.isDelayPlay && (this.type === 2 || this.type === 3) }})
    isLoop: boolean = false;

    @property({displayName: "延迟暂停."})
    isDelayStop: boolean = false;

    @property({displayName: "延迟时间.", min: 0.1, visible() { return this.isDelayStop; }})
    duration: number = 1;

    start() {
        this.scheduleOnce(() => {
            this.onDelayPlay();
        }, 0);
    }

    private onDelayPlay() {
        switch (this.type) {
            case 0:
                this.onDelayPlayParticle();
                break;
            case 1:
                this.onDelayPlayAnimation();
                break;
            case 2:
                this.onDelayPlayDragonBones();
                break;
            case 3:
                this.onDelayPlaySpine();
                break;
            case 4:
                this.onDelayPlaySpriteFrame();
                break;
        }
    }

    private onDelayPlayParticle() {
        const particle = this.node.getComponent(cc.ParticleSystem);
        if (!particle) { return; }
        
        if (this.isDelayPlay && this.delay > 0) {
            particle.stopSystem();
            this.scheduleOnce(() => {
                particle.resetSystem();
            }, this.delay);
        }

        if (this.isDelayStop && this.duration > this.delay) {
            this.scheduleOnce(() => {
                particle.stopSystem();
            }, this.duration);
        }
    }

    private onDelayPlayAnimation() {
        const animation = this.node.getComponent(cc.Animation);
        if (!animation) { return; }
        
        if (this.isDelayPlay && this.delay > 0) {
            animation.stop();
            this.scheduleOnce(() => {
                animation.play();
            }, this.delay);
        }

        if (this.isDelayStop && this.duration > this.delay) {
            this.scheduleOnce(() => {
                animation.stop();
            }, this.duration);
        }
    }

    private onDelayPlayDragonBones() {
        const dragon = this.node.getComponent(dragonBones.ArmatureDisplay);
        if (!dragon) { return; }
        
        if (this.isDelayPlay && this.delay > 0) {
            (dragon.armature() as dragonBones.Armature).animation.stop();
            this.scheduleOnce(() => {
                dragon.playAnimation(this.aniName, this.isLoop ? 0 : 1);
            }, this.delay);
        }
        
        if (this.isDelayStop && this.duration > this.delay) {
            this.scheduleOnce(() => {
                (dragon.armature() as dragonBones.Armature).animation.stop();
            }, this.duration);
        }
    }

    private onDelayPlaySpine() {
        const skeleton = this.node.getComponent(sp.Skeleton);
        if (!skeleton) { return; }
        
        if (this.isDelayPlay && this.delay > 0) {
            skeleton.paused = true;
            this.scheduleOnce(() => {
                skeleton.setAnimation(0, this.aniName, this.isLoop);
            }, this.delay);
        }
        
        if (this.isDelayStop && this.duration > this.delay) {
            this.scheduleOnce(() => {
                skeleton.paused = true;
            }, this.duration);
        }
    }

    private onDelayPlaySpriteFrame() {
        const spriteFrame = this.node.getComponent(SpriteFrames);
        if (!spriteFrame) { return; }
        const animation = this.node.getComponent(cc.Animation);
        if (this.isDelayPlay && this.delay > 0) {
            animation.stop();
            this.scheduleOnce(() => {
                animation.play();
            }, this.delay);
        }
        
        if (this.isDelayStop && this.duration > this.delay) {
            this.scheduleOnce(() => {
                animation.stop();
            }, this.duration);
        }
    }

}
