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

import 'core/network/api_client.dart';
import 'core/network/ws_client.dart';
import 'core/storage/token_storage.dart';
import 'core/theme/app_theme.dart';
import 'features/auth/auth_cubit.dart';
import 'features/auth/auth_repository.dart';
import 'features/auth/mobile_screen.dart';
import 'features/auth/otp_screen.dart';
import 'features/game/game_cubit.dart';
import 'features/game/game_repository.dart';
import 'features/game/game_screen.dart';
import 'features/game/tier_list_screen.dart';
import 'features/lobby/lobby_screen.dart';
import 'features/lobby/wallet_cubit.dart';
import 'features/shop/shop_cubit.dart';
import 'features/shop/shop_repository.dart';
import 'features/shop/shop_screen.dart';

class HakemApp extends StatelessWidget {
  final ApiClient api;
  final AuthRepository authRepo;
  final TokenStorage tokenStorage;
  final bool loggedIn;

  const HakemApp({
    super.key,
    required this.api,
    required this.authRepo,
    required this.tokenStorage,
    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: '/lobby', builder: (_, __) => const LobbyScreen()),
        GoRoute(
          path: '/shop',
          builder: (_, __) => BlocProvider(
            create: (_) => ShopCubit(ShopRepository(api))..load(),
            child: const ShopScreen(),
          ),
        ),
        GoRoute(
          path: '/game/tiers',
          builder: (_, __) => TierListScreen(repo: GameRepository(api)),
        ),
        GoRoute(
          path: '/game/:tier',
          builder: (_, st) {
            final tier = st.pathParameters['tier']!;
            final prize = int.tryParse(st.uri.queryParameters['prize'] ?? '') ?? 0;
            return FutureBuilder<String?>(
              future: tokenStorage.read(),
              builder: (context, snap) {
                if (!snap.hasData) {
                  return const Scaffold(
                      body: Center(child: CircularProgressIndicator()));
                }
                return BlocProvider(
                  create: (_) => GameCubit(WsClient(snap.data!), tier),
                  child: GameScreen(prize: prize),
                );
              },
            );
          },
        ),
      ],
    );

    return MultiBlocProvider(
      providers: [
        BlocProvider(create: (_) => AuthCubit(authRepo)),
        BlocProvider(create: (_) => WalletCubit(api)),
      ],
      child: MaterialApp.router(
        title: 'سلطان حکم',
        debugShowCheckedModeBanner: false,
        theme: AppTheme.build(),
        routerConfig: router,
        // اعمال راست‌به‌چپ برای کل اپ.
        builder: (context, child) => Directionality(
          textDirection: TextDirection.rtl,
          child: child!,
        ),
      ),
    );
  }
}
