/*
 * @Author: wangyang 1147192855@qq.com
 * @Date: 2022-06-15 17:58:30
 * @LastEditors: wangyang 1147192855@qq.com
 * @LastEditTime: 2022-08-03 11:53:08
 * @FilePath: \marking_app\lib\main.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'package:flutter/services.dart';
import 'package:marking_app/utils/index.dart';
import 'package:marking_app/utils/the_global.dart';
import 'package:marking_app/routes/RouterManager.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  RouterManager.initRouter();

  SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
      overlays: [SystemUiOverlay.top, SystemUiOverlay.bottom]); // 屏幕刘海
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]); // 屏幕强制竖屏
  // OrientationHelper.setEnabledSystemUIOverlays(SystemUiOverlay.values);

  runApp(ProviderScope(child: MyApp()));
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  late final StreamSubscription<ConnectivityResult> subscription;
  late bool networkSituation = true; // 网络情况
  @override
  void initState() {
    networkMonitoring();
    super.initState();
  }

  @override
  void dispose() {
    subscription.cancel();
    super.dispose();
  }

  /// 网络监控
  void networkMonitoring() async {
    subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
      if (ConnectivityResult.none == result) {
        ToastUtils.showInfo('无法连接网络');
        networkSituation = false;
      } else {
        if (!networkSituation) {
          if (ConnectivityResult.mobile == result) {
            ToastUtils.showInfo('当前为移动网络，请注意网络使用情况', duration: const Duration(milliseconds: 500));
          } else {
            ToastUtils.showSuccess('网络恢复正常');
          }
        }
        networkSituation = true;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context1, child) {
        return MaterialApp(
          title: '远轩阅卷系统',
          navigatorKey: TheGlobal.navigatorKey,
          debugShowCheckedModeBanner: false,
          // locale: const Locale('zh', 'CN'), // 中文简体  ,
          // supportedLocales: [
          //   const Locale('zh', 'CN'), // 中文简体
          //   // 其他支持的locale可以在这里添加
          // ],
          // 这里是国际化支持，确保添加flutter_localizations依赖
          supportedLocales: [
            const Locale('zh', 'CN'), // 中文简体
            // 其他支持的locale可以在这里添加
          ],
          localizationsDelegates: [
            // ...其他delegates
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate, // 如果你使用了Cupertino风格的组件
            // ...添加其他必要的delegates
          ],
          localeResolutionCallback: (locale, supportedLocales) {
            // 在这里可以实现自定义的locale解析逻辑
            // 如果需要，返回你想要的Locale对象
            return locale;
          },
          theme: ThemeData(
            primarySwatch: createMaterialColor(const Color.fromRGBO(46, 91, 255, 1)),
            // textTheme: Typography.englishLike2018.apply(fontSizeFactor: 1.sp,),
            textTheme: TextTheme(
              bodyLarge: TextStyle(fontSize: 14.sp, color: Colors.black87),
            ),
          ),
          onGenerateRoute: RouterManager.generator, //全局注册
          builder: EasyLoading.init(),
        );
      },
    );
  }
}
