import 'package:clean/data/consts/constants.dart';
import 'package:clean/resource/colors.gen.dart';
import 'package:clean/resource/string.gen.dart';
import 'package:clean/resource/string_source.dart';
import 'package:clean/router/app_pages.dart';
import 'package:clean/utils/app_info_util.dart';
import 'package:clean/utils/mmkv_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'device/device_info_util.dart';

Future<void> main() async {

  await KVUtil.init();

  //初始化
  await initAfterGrant();

  runApp(const MyApp());
}

Future<void> initAfterGrant() async {
  if (!isAgreePrivacyPolicy()) {
    return;
  }
  //获取包信息
  await appInfoUtil.init();
  //获取设备信息
  await deviceInfoUtil.init();
}

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

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(360, 800),
      builder: (_, child) {
        return _buildMaterialApp();
      },
    );
  }

  _buildMaterialApp() {
    return RefreshConfiguration(
      headerBuilder: () =>
      const MaterialClassicHeader(color: ColorName.colorPrimary),
      // footerBuilder: () => ClassicFooter(
      //   canLoadingText: StringName.loadingMore,
      //   idleText: StringName.loadPullUp,
      //   loadingText: StringName.loadingTxt,
      //   noDataText: StringName.loadNoData,
      //   failedText: StringName.loadFailed,
      // ),
      child: GetMaterialApp(
        onGenerateTitle: (_) => StringName.appName,
        getPages: AppPage.pages,
        initialRoute: RoutePath.splash,
        initialBinding: AppBinding(),
        theme: ThemeData(
          useMaterial3: true,
          textSelectionTheme: const TextSelectionThemeData(
            cursorColor: ColorName.colorPrimary, // 设置默认光标颜色
            selectionHandleColor: ColorName.colorPrimary, // 设置光标下面水滴的颜色
          ),
        ),
        navigatorObservers: [FlutterSmartDialog.observer],
        builder: FlutterSmartDialog.init(),
        translations: StringResource(),
        localizationsDelegates: const [
          GlobalMaterialLocalizations.delegate,
          //是Flutter的一个本地化委托，用于提供Material组件库的本地化支持
          GlobalWidgetsLocalizations.delegate,
          //用于提供通用部件（Widgets）的本地化支持
          GlobalCupertinoLocalizations.delegate,
          //用于提供Cupertino风格的组件的本地化支持
        ],
        supportedLocales: const [
          Locale('zh', 'CN'), // 支持的语言和地区
        ],
        // 你的翻译
        locale: const Locale('zh', 'CN'),
        // 将会按照此处指定的语言翻译 添加一个回调语言选项，以备上面指定的语言翻译不存在
        fallbackLocale: const Locale('zh', 'CN'),
      ),
    );
  }
}