import 'package:flutter/material.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'core/network/dio_client.dart';
import 'core/storage/local_storage.dart';
import 'data/services/user_service.dart';
import 'data/services/market_service.dart';
import 'data/services/trade_service.dart';
import 'data/services/asset_service.dart';
import 'data/services/fund_service.dart';
import 'providers/auth_provider.dart';
import 'providers/market_provider.dart';
import 'providers/asset_provider.dart';
import 'ui/pages/auth/login_page.dart';
import 'ui/pages/main/main_page.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 禁用 Provider 类型检查
  Provider.debugCheckInvalidValueType = null;

  // 初始化本地存储
  final prefs = await SharedPreferences.getInstance();
  await LocalStorage.init();

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        // 服务
        Provider<DioClient>(create: (_) => DioClient()),
        ProxyProvider<DioClient, UserService>(
          create: (_) => UserService(DioClient()),
          update: (_, client, previous) => previous ?? UserService(client),
        ),
        ProxyProvider<DioClient, MarketService>(
          create: (_) => MarketService(DioClient()),
          update: (_, client, previous) => previous ?? MarketService(client),
        ),
        ProxyProvider<DioClient, TradeService>(
          create: (_) => TradeService(DioClient()),
          update: (_, client, previous) => previous ?? TradeService(client),
        ),
        ProxyProvider<DioClient, AssetService>(
          create: (_) => AssetService(DioClient()),
          update: (_, client, previous) => previous ?? AssetService(client),
        ),
        ProxyProvider<DioClient, FundService>(
          create: (_) => FundService(DioClient()),
          update: (_, client, previous) => previous ?? FundService(client),
        ),
        // 状态管理
        ProxyProvider2<UserService, DioClient, AuthProvider>(
          create: (_) => AuthProvider(UserService(DioClient())),
          update: (_, userService, __, previous) =>
              previous ?? AuthProvider(userService),
        ),
        ProxyProvider<MarketService, MarketProvider>(
          create: (_) => MarketProvider(MarketService(DioClient())),
          update: (_, service, previous) =>
              previous ?? MarketProvider(service),
        ),
        ProxyProvider2<AssetService, FundService, AssetProvider>(
          create: (_) =>
              AssetProvider(AssetService(DioClient()), FundService(DioClient())),
          update: (_, assetService, fundService, previous) =>
              previous ?? AssetProvider(assetService, fundService),
        ),
      ],
      child: ShadApp.custom(
        themeMode: ThemeMode.dark,
        darkTheme: ShadThemeData(
          brightness: Brightness.dark,
          colorScheme: const ShadSlateColorScheme.dark(),
        ),
        appBuilder: (context) {
          return MaterialApp(
            debugShowCheckedModeBanner: false,
            theme: Theme.of(context),
            localizationsDelegates: const [
              GlobalShadLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
            ],
            builder: (context, child) {
              return ShadAppBuilder(child: child!);
            },
            home: Consumer<AuthProvider>(
              builder: (context, auth, _) {
                if (auth.isLoading) {
                  return const Scaffold(
                    body: Center(
                      child: CircularProgressIndicator(),
                    ),
                  );
                }
                if (auth.isLoggedIn) {
                  return const MainPage();
                }
                return const LoginPage();
              },
            ),
          );
        },
      ),
    );
  }
}
