import 'dart:ui';
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:provider/single_child_widget.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'core/network/dio_client.dart';
import 'core/storage/local_storage.dart';
import 'core/theme/app_color_scheme.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 'providers/theme_provider.dart';
import 'ui/pages/auth/login_page.dart';
import 'ui/pages/main/main_page.dart';

void main() async {
  // 确保 Flutter 绑定初始化
  WidgetsFlutterBinding.ensureInitialized();

  // 全局错误处理 - Flutter 框架错误
  FlutterError.onError = (FlutterErrorDetails details) {
    FlutterError.presentError(details);
    debugPrint('Flutter Error: ${details.exception}');
    debugPrint('Stack trace: ${details.stack}');
  };

  // 全局错误处理 - 异步未捕获错误
  PlatformDispatcher.instance.onError = (error, stack) {
    debugPrint('Uncaught error: $error');
    debugPrint('Stack: $stack');
    return true;
  };

  Provider.debugCheckInvalidValueType = null;

  try {
    await SharedPreferences.getInstance();
    await LocalStorage.init();
    debugPrint('App initialized successfully');
  } catch (e, stack) {
    debugPrint('Initialization error: $e');
    debugPrint('Stack: $stack');
  }

  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: _buildProviders(),
      child: Consumer<ThemeProvider>(
        builder: (context, themeProvider, _) {
          return AuthNavigator(
            child: ShadApp.custom(
              themeMode: themeProvider.themeMode,
              theme: createLightShadTheme(),
              darkTheme: createDarkShadTheme(),
              appBuilder: _buildMaterialApp,
            ),
          );
        },
      ),
    );
  }

  List<SingleChildWidget> _buildProviders() {
    final dioClient = DioClient();

    return [
      // Theme Provider (必须放在最前面)
      ChangeNotifierProvider<ThemeProvider>(
        create: (_) => ThemeProvider()..init(),
      ),
      // Services
      Provider<DioClient>.value(value: dioClient),
      Provider<UserService>(create: (_) => UserService(dioClient)),
      Provider<MarketService>(create: (_) => MarketService(dioClient)),
      Provider<TradeService>(create: (_) => TradeService(dioClient)),
      Provider<AssetService>(create: (_) => AssetService(dioClient)),
      Provider<FundService>(create: (_) => FundService(dioClient)),
      // State Management
      ChangeNotifierProvider<AuthProvider>(
        create: (ctx) => AuthProvider(ctx.read<UserService>()),
      ),
      ChangeNotifierProvider<MarketProvider>(
        create: (ctx) => MarketProvider(ctx.read<MarketService>()),
      ),
      ChangeNotifierProvider<AssetProvider>(
        create: (ctx) => AssetProvider(
          ctx.read<AssetService>(),
          ctx.read<FundService>(),
        ),
      ),
    ];
  }

  Widget _buildMaterialApp(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: Theme.of(context),
      localizationsDelegates: const [
        GlobalShadLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      builder: (context, child) => ShadAppBuilder(child: child!),
      home: _buildHome(),
    );
  }

  Widget _buildHome() {
    return Consumer<AuthProvider>(
      builder: (context, auth, _) {
        if (auth.isLoading) {
          return const Scaffold(
            body: Center(child: CircularProgressIndicator()),
          );
        }
        return auth.isLoggedIn ? const MainPage() : const LoginPage();
      },
    );
  }
}

/// 认证路由守卫 - 监听认证状态并自动导航
class AuthNavigator extends StatefulWidget {
  final Widget child;

  const AuthNavigator({super.key, required this.child});

  @override
  State<AuthNavigator> createState() => _AuthNavigatorState();
}

class _AuthNavigatorState extends State<AuthNavigator> {
  bool? _wasLoggedIn;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final isLoggedIn = context.watch<AuthProvider>().isLoggedIn;

    if (_wasLoggedIn == null) {
      _wasLoggedIn = isLoggedIn;
      return;
    }

    if (_wasLoggedIn != isLoggedIn) {
      _wasLoggedIn = isLoggedIn;
      _navigateToAuthPage(isLoggedIn);
    }
  }

  void _navigateToAuthPage(bool isLoggedIn) {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (!mounted) return;

      // 退出登录时重置其他 Provider 的状态
      if (!isLoggedIn) {
        context.read<AssetProvider>().resetLoadState();
        context.read<MarketProvider>().resetLoadState();
      }

      Navigator.of(context).pushAndRemoveUntil(
        MaterialPageRoute(
          builder: (_) => isLoggedIn ? const MainPage() : const LoginPage(),
        ),
        (route) => false,
      );
    });
  }

  @override
  Widget build(BuildContext context) => widget.child;
}
