import { useCallback, useEffect, useState } from "react";
import { ReactComponent as StarTargetIcon } from "../assets/star-target.svg";
import { ReactComponent as TickIcon } from "../assets/green-tick.svg";
import styles from "./styles.module.css";
import { SuccessBadge, TutorialFrame } from "./TutorialFrame";

type Props = {
    nextPhase: () => void;
};

export function Phase5({ nextPhase }: Props) {
    const [starTargetState, setStarTargetState] = useState<"hidden" | "visible" | "animation">("visible");
    const [isComplete, setIsComplete] = useState(false);

    useEffect(() => {
        const timeout = window.setTimeout(() => {
            setStarTargetState("animation");
            setIsComplete(true);
        }, 1300);

        return () => window.clearTimeout(timeout);
    }, []);

    const onStarTargetAnimationEnded = useCallback(() => {
        setStarTargetState("hidden");
        nextPhase();
    }, [nextPhase]);

    const targetClassNames = [
        styles.bareBlockIconFullScreen,
        starTargetState === "animation" ? styles.fadeOutAnimation : "",
        starTargetState === "hidden" ? styles.hide : "",
        starTargetState === "visible" ? styles.show : "",
    ].join(" ");

    return (
        <TutorialFrame
            stepIndex={1}
            title="Collect 10 Stars"
            hint="Your mission is to collect 10 stars while avoiding the cogs."
            badge={isComplete ? <SuccessBadge TickIcon={TickIcon} /> : null}
        >
            <div className={styles.singleColumnContainer}>
                <div className={styles.targetFocusAnchor}>
                    <StarTargetIcon className={targetClassNames} onAnimationEnd={onStarTargetAnimationEnded} />
                </div>
            </div>
        </TutorialFrame>
    );
}
