import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';

import 'core/locator/locator.dart';
import 'core/theme/app_theme.dart';
import 'core/widgets/offline_gate.dart';
import 'feature/auth/presentation/bloc/auth_bloc.dart';
import 'feature/auth/presentation/screen/mobile_screen.dart';
import 'feature/auth/presentation/screen/otp_screen.dart';
import 'feature/auth/presentation/screen/profile_setup_screen.dart';
import 'feature/game/presentation/bloc/game_bloc.dart';
import 'feature/game/presentation/bloc/game_event.dart';
import 'feature/game/presentation/bloc/private_info_bloc.dart';
import 'feature/game/presentation/bloc/tier_bloc.dart';
import 'feature/game/presentation/screen/game_screen.dart';
import 'feature/game/presentation/screen/private_entry_screen.dart';
import 'feature/game/presentation/screen/private_table_screen.dart';
import 'feature/game/presentation/screen/tier_list_screen.dart';
import 'feature/legal/terms_screen.dart';
import 'feature/tournament/presentation/screen/tournaments_screen.dart';
import 'feature/profile/presentation/bloc/profile_bloc.dart';
import 'feature/profile/presentation/bloc/profile_event.dart';
import 'feature/profile/presentation/screen/profile_screen.dart';
import 'feature/ranked/presentation/bloc/leaderboard_bloc.dart';
import 'feature/ranked/presentation/screen/leaderboard_screen.dart';
import 'feature/shop/presentation/bloc/shop_bloc.dart';
import 'feature/shop/presentation/bloc/shop_event.dart';
import 'feature/shop/presentation/screen/shop_screen.dart';
import 'feature/shop/presentation/screen/vip_screen.dart';
import 'feature/wallet/presentation/bloc/wallet_bloc.dart';
import 'feature/wallet/presentation/screen/lobby_screen.dart';

class HakemApp extends StatelessWidget {
  final bool loggedIn;
  const HakemApp({super.key, required this.loggedIn});

  @override
  Widget build(BuildContext context) {
    final router = GoRouter(
      initialLocation: loggedIn ? '/lobby' : '/login',
      routes: [
        GoRoute(path: '/login', builder: (_, __) => const MobileScreen()),
        GoRoute(path: '/otp', builder: (_, __) => const OtpScreen()),
        GoRoute(path: '/setup', builder: (_, __) => const ProfileSetupScreen()),
        GoRoute(path: '/lobby', builder: (_, __) => const LobbyScreen()),
        GoRoute(path: '/terms', builder: (_, __) => const TermsScreen()),
        GoRoute(
            path: '/tournaments',
            builder: (_, __) => const TournamentsScreen()),
        GoRoute(
          path: '/profile',
          builder: (_, __) => BlocProvider(
            create: (_) => locator<ProfileBloc>()..add(LoadProfileEvent()),
            child: const ProfileScreen(),
          ),
        ),
        GoRoute(
          path: '/leaderboard',
          builder: (_, __) => BlocProvider(
            create: (_) =>
                locator<LeaderboardBloc>()..add(LoadLeaderboardEvent()),
            child: const LeaderboardScreen(),
          ),
        ),
        GoRoute(
          path: '/shop',
          builder: (_, __) => BlocProvider(
            create: (_) => locator<ShopBloc>()..add(LoadShopEvent()),
            child: const ShopScreen(),
          ),
        ),
        GoRoute(
          path: '/vip',
          builder: (_, __) => BlocProvider(
            create: (_) => locator<ShopBloc>()..add(LoadShopEvent()),
            child: const VipScreen(),
          ),
        ),
        GoRoute(
          path: '/private',
          builder: (_, __) => BlocProvider(
            create: (_) => locator<PrivateInfoBloc>(),
            child: const PrivateEntryScreen(),
          ),
        ),
        GoRoute(
          path: '/private/room',
          builder: (_, st) {
            final create = st.uri.queryParameters['create'] == '1';
            final joinCode = st.uri.queryParameters['join'];
            final action = create
                ? {'type': 'create_table'}
                : {'type': 'join_table', 'code': joinCode ?? ''};
            return BlocProvider(
              create: (_) =>
                  locator<GameBloc>()..add(ConnectGameEvent(action)),
              child: const PrivateTableScreen(),
            );
          },
        ),
        GoRoute(
          path: '/game/tiers',
          builder: (_, __) => BlocProvider(
            create: (_) => locator<TierBloc>()..add(LoadTiersEvent()),
            child: const TierListScreen(),
          ),
        ),
        GoRoute(
          path: '/game/:tier',
          builder: (_, st) {
            final tier = st.pathParameters['tier']!;
            final prize =
                int.tryParse(st.uri.queryParameters['prize'] ?? '') ?? 0;
            return BlocProvider(
              create: (_) => locator<GameBloc>()
                ..add(ConnectGameEvent({'type': 'join_queue', 'tier': tier})),
              child: GameScreen(prize: prize),
            );
          },
        ),
      ],
    );

    return MultiBlocProvider(
      providers: [
        BlocProvider(create: (_) => locator<AuthBloc>()),
        BlocProvider(create: (_) => locator<WalletBloc>()),
      ],
      child: MaterialApp.router(
        title: 'حاکم ایرانی',
        debugShowCheckedModeBanner: false,
        theme: AppTheme.build(),
        routerConfig: router,
        builder: (context, child) => Directionality(
          textDirection: TextDirection.rtl,
          child: OfflineGate(child: child!),
        ),
      ),
    );
  }
}
