package com.example.fitbot.pepper;

import com.aldebaran.qi.sdk.QiContext;
import com.aldebaran.qi.sdk.builder.AnimateBuilder;
import com.aldebaran.qi.sdk.builder.AnimationBuilder;
import com.aldebaran.qi.sdk.object.actuation.Animate;
import com.aldebaran.qi.sdk.object.actuation.Animation;

public class PepperAnimationEvent extends AbstractPepperActionEvent {

    public final String animationName;
    private final IAnimationCompletedListener onLabelReachedListener;


    /**
     * Constructor for PepperAnimationEvent
     *
     * @param animationName The name of the animation to play
     * @param onLabelReachedListener The listener to call when the animation is completed
     */
    public PepperAnimationEvent(String animationName, IAnimationCompletedListener onLabelReachedListener) {
        this.animationName = animationName;
        this.onLabelReachedListener = onLabelReachedListener;
    }

    /**
     * Constructor for PepperAnimationEvent
     *
     * @param animationName The name of the animation to play
     */
    public PepperAnimationEvent(String animationName) {
        this(animationName, null);
    }

    @Override
    public EPepperAction getAction() {
        return null;
    }

    /**
     * Returns an animation object, which can be used to play the animation
     * in the Pepper class.
     */
    public Animate getAnimation(QiContext context) {
        Animation animation = AnimationBuilder.with(context)
                .withResources(context.getResources().getIdentifier(animationName, "raw", context.getPackageName()))
                .build();

        Animate animate = AnimateBuilder.with(context)
                .withAnimation(animation)
                .build();

        // Add a listener for when a label is reached
        animate.addOnLabelReachedListener((label, time) -> {
            if (onLabelReachedListener != null && "end".equals(label)) {
                onLabelReachedListener.onComplete();
            }
        });
        return animate;
    }

    public interface IAnimationCompletedListener {
        void onComplete();
    }
}
