import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'core/l10n/app_localizations.dart';
import 'core/router/app_router.dart';
import 'core/theme/app_theme.dart';
import 'providers/app_provider.dart';
import 'providers/coin_cache_provider.dart';
import 'providers/currency_provider.dart';
import 'providers/market_provider.dart';

class CexApp extends ConsumerWidget {
  const CexApp({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final themeMode = ref.watch(themeProvider);
    final locale = ref.watch(localeProvider);
    // 初始化法币汇率，确保全局 formatFiatPrice 使用正确汇率
    ref.watch(currencyProvider);
    // 提前初始化行情数据，减少进入行情页面时的加载等待
    ref.read(coinCacheProvider);
    ref.read(marketProvider);
    final router = ref.watch(appRouterProvider);

    // ScreenUtil 基于 375px 设计稿宽度
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child) {
        return MediaQuery(
          // 锁定字体缩放为 1.0，不跟随系统字体大小变化
          // 避免行情数字/布局在用户调大系统字体时溢出或截断
          data: MediaQuery.of(context).copyWith(
            textScaler: TextScaler.noScaling,
          ),
          child: MaterialApp.router(
          title: 'iBit',
          debugShowCheckedModeBanner: false,
          theme: AppTheme.light,
          darkTheme: AppTheme.dark,
          themeMode: themeMode,
          locale: locale,
          supportedLocales: const [
            Locale('zh'),
            Locale('zh', 'TW'),
            Locale('en'),
            Locale('hi'),
            Locale('id'),
            Locale('ja'),
            Locale('ko'),
          ],
          localizationsDelegates: AppLocalizations.localizationsDelegates,
          routerConfig: router,
          ),
        );
      },
    );
  }
}
