import 'package:bot_toast/bot_toast.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_translate/ads/interstitial_ad_manage.dart';
import 'package:flutter_translate/common/hive/hive.dart';
import 'package:flutter_translate/common/utils/log_utils.dart';
import 'package:flutter_translate/common/utils/shared_util.dart';
import 'package:flutter_translate/facebook/facebook_manager.dart';
import 'package:flutter_translate/firebase/firebase_analytics_manager.dart';
import 'package:flutter_translate/firebase/firebase_crashlytics_manager.dart';
import 'package:flutter_translate/firebase/firebase_options.dart';
import 'package:flutter_translate/global/app_themes.dart';
import 'package:flutter_translate/manager/att.dart';
import 'package:flutter_translate/manager/translate.dart';
import 'package:flutter_translate/manager/tts.dart';
import 'package:flutter_translate/router/get_gages.dart';
import 'package:get/get.dart';

import 'global/app_config.dart';

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

  await initHive();

  // 初始化Firebase
  try {
    await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
    await FirebaseCrashlyticsManager.setEnabled();
    FirebaseCrashlyticsManager.recordFlutterError();
    FirebaseCrashlyticsManager.recordError();
    FirebaseAnalyticsManager.logLaunch();
  } catch (e) {
    Log.d("Firebase initialization error: $e");
  }

  await FacebookManager.init();

  await InterstitialAdManager().init();

  SharedUtil.initShared();

  await Translate().init();
  await Tts().init();

  Att().attRequest();

  // 竖屏
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);

  runApp(const App());
}

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

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      builder: (context, child) {
        final botToastBuilder = BotToastInit();
        return KeyboardDismissOnTap(
          child: GetMaterialApp(
            debugShowCheckedModeBanner: false,
            title: appName,
            theme: lightTheme,
            themeMode: ThemeMode.light,
            initialRoute: GetPages.launch,
            getPages: GetPages.routers,
            builder: (context, widget) {
              widget = botToastBuilder(context, widget);
              // 设置文字大小不随系统设置改变
              return MediaQuery.withNoTextScaling(child: widget);
            },
            routingCallback: (routing) {
              Log.d("当前路由 => ${routing?.current}");
              if (routing?.current == GetPages.launch) {
                FirebaseAnalyticsManager.logLoadingPage();
              }
              if (routing?.current == GetPages.home) {
                FirebaseAnalyticsManager.logHomepage();
              }
            },
          ),
        );
      },
    );
  }
}
