import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:get/get.dart';
import 'package:trans_lark/global/flutter_tts_manager.dart';
import 'package:trans_lark/global/global_config.dart';
import 'package:trans_lark/global/translate_manager.dart';
import 'package:trans_lark/router/router.dart';
import 'package:trans_lark/storage/hive_storage.dart';
import 'package:trans_lark/widget/base_easyloading.dart';

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

  // 初始化Hive
  await initHive();

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

  // 设置安卓状态栏背景色透明
  if (Platform.isAndroid) {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
    ));
  }

  await TranslateManager().init();
  await FlutterTtsManager().init();

  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    final easyLoading = EasyLoading.init();
    return KeyboardDismissOnTap(
      child: GetMaterialApp(
        debugShowCheckedModeBanner: false,
        title: GlobalConfig.appName,
        theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(
            seedColor: const Color.fromARGB(255, 185, 239, 200),
          ),
          useMaterial3: true,
        ),
        initialRoute: GetRouter.home,
        getPages: GetRouter.routers,
        defaultTransition: Transition.rightToLeft,
        builder: (context, widget) {
          BaseEasyLoading.configLoading();
          widget = easyLoading(context, widget);
          // 设置文字大小不随系统设置改变
          return MediaQuery.withNoTextScaling(child: widget);
        },
      ),
    );
  }
}
