import { useCallback, useEffect, useState } from "react";
import { ReactComponent as StarTargetIcon } from "../assets/star-target.svg";
import styles from "./styles.module.css";
import { getConnectedCog } from "../utils/CogManager";
import Cog from "@robotical/webapp-types/dist-types/src/application/RAFTs/Cog/Cog";

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

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

    const onCogButtonClick = useCallback(() => {
        // setStarTargetState("animation");
    }, []);

    useEffect(() => {
        const connectedCog = getConnectedCog();
        if (connectedCog) {
            const eventName = (connectedCog as Cog).publishedDataAnalyser.eventsMap.buttonClick.release;
            (connectedCog as Cog).publishedDataAnalyser.addListener(eventName, onCogButtonClick);

            return () => {
                (connectedCog as Cog).publishedDataAnalyser.removeListener(eventName, onCogButtonClick);
            };
        }
    }, []);

    useEffect(() => {
        setTimeout(() => {
            setStarTargetState("animation");
        }, 1500);
    }, []);

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

    const cogIconClassNames = [
        styles.bareBlockIconFullScreen,
        starTargetState === "animation" ? styles.fadeOutAnimation : "",
        starTargetState === "hidden" ? styles.hide : "",
        starTargetState === "visible" ? styles.show : "",

    ].join(" ");

    return (
        <div className={styles.singleColumnContainer}>
            <StarTargetIcon className={cogIconClassNames} onClick={onCogButtonClick} onAnimationEnd={onStarTargetAnimationEnded} />
        </div>
    );
}