import 'dart:io';

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

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

  //全局配置smartDialog
  smartConfig();
  //获取包信息
  await appInfoUtil.init();
  //mmkv
  await KVUtil.init();

  runApp(const MyApp());
}

void smartConfig() {
  SmartDialog.config.custom =
      SmartConfigCustom(animationType: SmartAnimationType.fade);
}

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

  // This widget is the root of your application.
  @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.tr,
        idleText: StringName.loadPullUp.tr,
        loadingText: StringName.loadingTxt.tr,
        noDataText: StringName.loadNoData.tr,
        failedText: StringName.loadFailed.tr,
      ),
      child: GetMaterialApp(
        onGenerateTitle: (_) => StringName.appName.tr,
        getPages: AppPage.pages,
        initialRoute: Platform.isAndroid ? RoutePath.mainTab : RoutePath.splash,
        initialBinding: AppBinding(),
        theme: ThemeData(
          useMaterial3: true,
        ),
        navigatorObservers: [FlutterSmartDialog.observer],
        builder: FlutterSmartDialog.init(),
        translations: StringResource(),
        // 你的翻译
        locale: const Locale('zh', 'CN'),
        // 将会按照此处指定的语言翻译
        fallbackLocale: const Locale('zh', 'CN'), // 添加一个回调语言选项，以备上面指定的语言翻译不存在
      ),
    );
  }
}
