import { AnimationClip } from "three";

import { ScaleClipType } from "../../engine/engine_animation.js";
import { AnimationUtils } from "../../engine/engine_animation.js";
import { serializable } from "../../engine/engine_serialization_decorator.js";
import { registerType } from "../../engine/engine_typestore.js";
import { Animation } from "../Animation.js";
import { Behaviour } from "../Component.js";


/**
 * Plays a hover animation on the object this component was added to when the mouse pointer (or touch) is over the object or any of it's children.  
 * 
 * By default, a simple scale-up animation is used. You can customize the hover and idle animations by providing your own animation clips.
 * 
 * @summary Hover Animation on Pointer Enter/Exit
 * @category Web
 * @group Components
 */
@registerType
export class HoverAnimation extends Behaviour {

    /**
     * Default hover animation type if no custom clip is provided.  
     * **Node**: This is only used if no custom hover animation clip is provided.  
     * @default "linear"
     */
    @serializable()
    type: ScaleClipType = "linear";

    /**
     * Duration of the hover animation in seconds.  
     * **Node**: This is only used if no custom hover animation clip is provided.
     * @default 0.1
     */
    @serializable()
    duration: number = 0.1;

    /**
     * Scale factor to apply when hovering.  
     * **Node**: This is only used if no custom hover animation clip is provided.
     * @default 1.1
     */
    @serializable()
    scaleFactor: number = 1.1;


    /**
     * Animation clip to play when hovering. If null, a default scale-up animation is used.
     */
    @serializable(AnimationClip)
    hovered: AnimationClip | null = null;

    /**
     * Animation clip to play when not hovering. If null, an empty clip is used.
     */
    @serializable(AnimationClip)
    idle: AnimationClip | null = null;

    private animation: Animation | null = null;

    start() {
        if (!this.idle) this.idle = AnimationUtils.emptyClip();

        if (!this.hovered || !(this.hovered instanceof AnimationClip)) {
            this.hovered = AnimationUtils.createScaleClip({
                type: "linear",
                duration: this.duration || 0.1,
                scale: this.gameObject.scale,
                scaleFactor: this.scaleFactor || 1.1,
            });
        }

        this.animation ??= this.gameObject.addComponent(Animation);
        this.animation.playAutomatically = false;
        this.playIdle();
    }

    onEnable() {
        if (this.animation) this.animation.enabled = true;
        this.playIdle();
    }
    onDisable() {
        if (this.animation) this.animation.enabled = false;
        this.playIdle();
    }

    onPointerEnter() {
        this.playHover();
    }

    onPointerExit() {
        this.playIdle();
    }

    private playIdle() {
        if (this.idle) this.animation?.play(this.idle, { exclusive: true, fadeDuration: .1, loop: true });
    }
    private playHover() {
        if (this.hovered) this.animation?.play(this.hovered, { exclusive: true, fadeDuration: .1, loop: false, clampWhenFinished: true });
    }

}