import { useCallback, useEffect, useState } from "react";
import { ReactComponent as FlagsTargetIcon } from "../assets/flags-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 Phase4({ nextPhase }: Props) {
    const [flagTargetState, setFlagsTargetState] = useState<"hidden" | "visible" | "animation">("visible");

    const onCogButtonClick = useCallback(() => {
        // setFlagsTargetState("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(() => {
            setFlagsTargetState("animation");
        }, 2500);
    }, []);

    const onFlagsTargetAnimationEnded = useCallback(() => {
        setFlagsTargetState("hidden");
        nextPhase();
    }, []);

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

    ].join(" ");

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