import React, { useEffect } from 'react';
import { Head, router, usePage } from '@inertiajs/react';
import { UserCheck, Sparkles } from 'lucide-react';
import GameLayout from '@/layouts/GameLayout';
import Echo from 'laravel-echo';
import Pusher from 'pusher-js';
import { toast } from 'sonner';
export default function LiveWait({ game, student }: any) {
    const { pusher } = usePage().props as any;

    useEffect(() => {
        (window as any).Pusher = Pusher;
        const echo = new Echo({
            broadcaster: 'pusher',
            key: pusher.key,
            cluster: pusher.cluster,
            forceTLS: true,
        });

        // Listen to the specific game lobby channel for GameStarted
        const channel = echo.channel(`lobby.${game.id}`);
        
        channel.listen('GameStarted', (e: any) => {
            console.log('Game is starting!', e);
            // Redirect to start endpoint or directly to play UI
            router.get(route('arena.live.play', game.id));
        });

        channel.listen('GameReset', (e: any) => {
            console.log('Game was reset by host');
            toast.error('Sesi game di-reset oleh guru. Silakan masukkan PIN yang baru.');
            router.get(route('arena.lobby', game.id));
        });

        channel.listen('GameEnded', (e: any) => {
            console.log('Game was ended by host');
            toast.error('Sesi Live Arena telah dibatalkan/diakhiri oleh guru.');
            router.get(route('dashboard'));
        });

        return () => {
            echo.leaveChannel(`lobby.${game.id}`);
        };
    }, [game.id, pusher]);

    return (
        <GameLayout title={`Menunggu - ${game.title}`}>
            <Head title={`Menunggu - ${game.title}`} />

            <div className="h-full flex flex-col items-center justify-center p-6 relative">                <div className="z-10 w-full max-w-md flex flex-col items-center gap-8 text-center animate-fade-in-up">
                    <div className="bg-surface-hover/50 p-6 rounded-full border border-primary-500/30 mb-4 animate-bounce-slow">
                        <UserCheck className="w-16 h-16 text-primary-400" />
                    </div>
                    
                    <div className="space-y-4">
                        <h2 className="text-2xl font-bold text-text-light">
                            Kamu berhasil bergabung!
                        </h2>
                        <div className="inline-flex items-center gap-2 bg-primary-500/20 text-primary-300 px-4 py-2 rounded-full font-medium border border-primary-500/30">
                            <Sparkles className="w-4 h-4" />
                            {student.full_name}
                        </div>
                    </div>

                    <div className="glass p-6 rounded-2xl border border-border/50 w-full mt-4">
                        <div className="flex flex-col items-center gap-4">
                            <div className="flex gap-2">
                                <div className="w-3 h-3 rounded-full bg-primary-500 animate-bounce"></div>
                                <div className="w-3 h-3 rounded-full bg-primary-500 animate-bounce" style={{ animationDelay: '0.2s' }}></div>
                                <div className="w-3 h-3 rounded-full bg-primary-500 animate-bounce" style={{ animationDelay: '0.4s' }}></div>
                            </div>
                            <p className="text-text-muted">
                                Menunggu Kakak Guru memulai permainan...
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </GameLayout>
    );
}
