import style from "./style.module.css";
import Carousel from "./carousel";
import { PosterItem } from "../types";
import WheelFrame from "./wheel-frame";
import { IRefPhaserGame } from "../game/PhaserGame";
import { useEffect, useState } from "react";
import { IRefPhaserGame as IRefPhaserGameTutorial } from "../tutorial-game/PhaserGame";

interface BlockSelectorProps {
    gameRef: React.RefObject<IRefPhaserGame | IRefPhaserGameTutorial>;
    staticBlock: "left" | "right";
    posterItems: PosterItem[];
    staticBlockItems: PosterItem[];
}

export default function BlockSelector({ gameRef, staticBlock, staticBlockItems, posterItems }: BlockSelectorProps) {
    const [hasBeenInteracted, setHasBeenInteracted] = useState(false);

    useEffect(() => {
        if (!gameRef.current || !gameRef.current.scene) return;
        if (gameRef.current.scene.selectedCogTiltLeftBlock) return;
        if (gameRef.current.scene.selectedCogTiltRightBlock) return;
        const item = posterItems[0];
        switch (staticBlock) {
            case "left":
                gameRef.current.scene.selectedCogTiltLeftBlock = item;
                break;
            case "right":
                gameRef.current.scene.selectedCogTiltRightBlock = item;
                break;
        }
        // gameRef.current.scene.registerCogEvents();
    }, [gameRef.current]);

    const onBlockChange = (item: PosterItem) => {
        if (!gameRef.current || !gameRef.current.scene) return;
        switch (staticBlock) {
            case "left":
                gameRef.current.scene.selectedCogTiltLeftBlock = item;
                break;
            case "right":
                gameRef.current.scene.selectedCogTiltRightBlock = item;
                break;
        }
        // gameRef.current.scene.registerCogEvents();
    };

    const clickHandler = () => {
        setHasBeenInteracted(true);
    }


    return (
        <div className={[style.blockSelector, hasBeenInteracted ? "" : style.uninteracted].join(" ")} onClick={clickHandler}>
            <div className={style.carouselContainer}>
                <WheelFrame
                    Carusel={Carousel}
                    onFrontPosterChange={(item) => { }}
                    posterItems={staticBlockItems}
                    isStatic={true}
                />
            </div>
            <div className={style.carouselContainer}>
                <WheelFrame
                    Carusel={Carousel}
                    onFrontPosterChange={onBlockChange}
                    posterItems={posterItems}
                />
            </div>
        </div>
    );
}
