
import 'package:app/global/event_bus.dart';
import 'package:app/providers/user_store.dart';
import 'package:app/router/route_paths.dart';
import 'package:app/widgets/version/version_dialog.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:app/router/routes.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

import 'global/theme/theme.dart';

void main() {
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => UserStore()),
      ],
      child: const MyApp(),
    ),
  );
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    // 1. 监听 401
    EventBus().stream.listen((event) {
      if (event == GlobalEvent.unauthorized) {
        final ctx = navigatorKey.currentState?.context;
        ctx?.go(RoutePaths.login);
      }
    });

    ///延迟
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final ctx = navigatorKey.currentState?.context;
      showUpdateDialog(ctx!);
    });

  }

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 694),
      useInheritedMediaQuery: true,
      child: MaterialApp.router(
        locale: const Locale('zh'),
        supportedLocales: const [
          Locale('zh'),
          Locale('en'),
        ],
        localizationsDelegates: GlobalMaterialLocalizations.delegates,
        debugShowCheckedModeBanner: false,
        routerConfig: goRouter,
        theme: AppTheme.createTheme(LightTheme()),
        builder: EasyLoading.init(),
      ),
    );
  }
}
