import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:news_getx/data/services/config.dart';
import 'package:news_getx/global.dart';
import 'package:news_getx/routes/app_pages.dart';
import 'package:news_getx/theme/app_theme.dart';
import 'package:news_getx/translations/app_translations.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

Future<void> main() async {
  await Global.init();
  runApp(const MyApp());
}

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(375, 812),
      // 如果不把这歌参数设为true 那么column就不会随着软键盘而变化
      // useInheritedMediaQuery: true,
      builder: (BuildContext context, Widget? child) {

        var app = GetMaterialApp(
          title: 'News',
          debugShowCheckedModeBanner: false,
          theme: AppTheme.light,
          unknownRoute: AppPages.notFoundRoute,
          initialRoute: AppPages.Initial,
          getPages: AppPages.pages,
          navigatorObservers: [AppPages.observer],
          translations: AppTranslations(),
          // 设置本地化
          localizationsDelegates: [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: ConfigService.to.languages,
          locale: ConfigService.to.locale,
          fallbackLocale: Locale('en', 'US'),
          builder: EasyLoading.init(),
        );

        return RefreshConfiguration(
          headerBuilder: () => ClassicHeader(),
          footerBuilder: () => ClassicFooter(),
          hideFooterWhenNotFull: true,
          headerTriggerDistance: 80,
          maxOverScrollExtent: 100,
          footerTriggerDistance: 150,
          child: Obx(() {
            // todo 有没有更好的解决方案
            return ConfigService.to.isGrayFilter.isTrue ? ColorFiltered(
              colorFilter: ColorFilter.mode(Colors.grey, BlendMode.color),
              child: app,
            ) : app;
          }),
        );
      },
    );
  }
}
