import 'package:cpt_auth/router/auth_service_impl.dart';
import 'package:cpt_auth/router/page_router.dart';
import 'package:cpt_ms/router/ms_router.dart';
import 'package:cpt_ms/router/ms_service_impl.dart';
import 'package:cpt_nl/router/nl_router.dart';
import 'package:cpt_nl/router/nl_service_impl.dart';
import 'package:cpt_sg/router/sg_router.dart';
import 'package:cpt_sg/router/sg_service_impl.dart';
import 'package:cpt_th/router/th_router.dart';
import 'package:cpt_th/router/th_service_impl.dart';
import 'package:cpt_uk/router/uk_router.dart';
import 'package:cpt_vn/router/vn_router.dart';
import 'package:cpt_uk/router/uk_service_impl.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:initializer/global_services_injection.dart';
import 'package:initializer/app_initializer.dart';
import 'package:plugin_basic/basic_export.dart';
import 'package:router/componentRouter/auth_service.dart';
import 'package:router/componentRouter/ms_service.dart';
import 'package:router/componentRouter/nl_service.dart';
import 'package:router/componentRouter/sg_service.dart';
import 'package:router/componentRouter/th_service.dart';
import 'package:router/componentRouter/vn_service.dart';
import 'package:cpt_vn/router/vn_service_impl.dart';
import 'package:cs_resources/local/theme/theme_config.dart';
import 'package:cs_resources/local/language/translation_service.dart';
import 'package:router/componentRouter/uk_service.dart';
import 'package:router/path/router_path.dart';
import 'package:router/observer/getx_router_observer.dart';
import 'package:plugin_basic/router/basic_page_router.dart';
import 'package:widgets/dialog/custom_toast_widget.dart';
import 'package:widgets/dialog/custom_error_widget.dart';
import 'package:widgets/dialog/custom_failure_widget.dart';
import 'package:widgets/dialog/custom_success_widget.dart';
import 'package:widgets/dialog/custom_loading_widget.dart';
import 'package:widgets/widget_export.dart';
import 'router/page_router.dart';

void main() async {
  //运行App
  // FlutterBugly.postCatchedException(() async {
  //交给初始化构造器去统一初始化
  await AppInitializer.initializeRunalone();

  //全局自定义单例服务的注入
  GlobalServicesInjection.init(additionalDependencies: () {
    Get.lazyPut<AuthService>(() => AuthServiceImpl());
    Get.lazyPut<UKService>(() => UKServiceImpl());
    Get.lazyPut<VNService>(() => VNServiceImpl());
    Get.lazyPut<SGService>(() => SGServiceImpl());
    Get.lazyPut<MSService>(() => MSServiceImpl());
    Get.lazyPut<NLService>(() => NLServiceImpl());
    Get.lazyPut<THService>(() => THServiceImpl());
  });

  runApp(MyApp());
  // });
}

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key) {
    /// 全局设置 EasyRefresh 的样式
    EasyRefresh.defaultHeaderBuilder = () => ClassicHeader(
          dragText: 'Pull to refresh'.tr,
          armedText: 'Release ready'.tr,
          readyText: 'Refreshing...'.tr,
          processingText: 'Refreshing...'.tr,
          processedText: 'Succeeded'.tr,
          noMoreText: 'No more'.tr,
          failedText: 'Failed'.tr,
          messageText: 'Last updated at %T'.tr,
          textStyle: const TextStyle(color: Color(0XFFAECAE5), fontSize: 14),
          messageStyle: const TextStyle(color: Color(0XFFAECAE5), fontSize: 12),
          iconTheme: const IconThemeData(color: Color(0XFFAECAE5)),
          backgroundColor: Colors.transparent,
        );
    EasyRefresh.defaultFooterBuilder = () => ClassicFooter(
          dragText: 'Pull to load'.tr,
          armedText: 'Release ready'.tr,
          readyText: 'Loading...'.tr,
          processingText: 'Loading...'.tr,
          processedText: 'Succeeded'.tr,
          noMoreText: 'No more'.tr,
          failedText: 'Failed'.tr,
          showMessage: false,
          triggerOffset: 50,
          iconDimension: 22,
          textStyle: const TextStyle(color: Color(0XFFAECAE5), fontSize: 14),
          messageStyle: const TextStyle(color: Color(0XFFAECAE5), fontSize: 12),
          iconTheme: const IconThemeData(color: Color(0XFFAECAE5)),
          backgroundColor: Colors.transparent,
        );

    /// SmartDialog 配置
    SmartDialog.config
      ..custom = SmartConfigCustom(
        maskColor: Colors.black.withOpacity(0.35),
        useAnimation: true,
      )
      ..attach = SmartConfigAttach(
        animationType: SmartAnimationType.scale,
        usePenetrate: false,
      )
      ..loading = SmartConfigLoading(
        // backDismiss: true,
        clickMaskDismiss: true,
      )
      ..toast = SmartConfigToast(
        intervalTime: const Duration(milliseconds: 100),
        displayTime: const Duration(milliseconds: 2000),
      );
  }

  @override
  Widget build(BuildContext context) {
    //设置全局的状态栏文本样式
    SystemChrome.setSystemUIOverlayStyle(ThemeConfig.systemUiOverlayStyleLightThemeWhite);

    //路由管理，状态管理，依赖管理一切都始于GetMaterialApp
    return KeyboardVisibilityBuilder(builder: (context, isKeyboardVisible) {
      return KeyboardDismissOnTap(
        dismissOnCapturedTaps: false,
        child: GetMaterialApp(
          //顶部是否展示Debug图标
          debugShowCheckedModeBanner: true,
          //是否展示Log
          enableLog: true,
          //默认路由与路由表的加载
          initialRoute: RouterPath.splash,
          getPages: PageRouter.routes + BasicPageRouter.routes + AuthPageRouter.routes + UKPageRouter.routes
              + VNPageRouter.routes + SGPageRouter.routes + NLPageRouter.routes + MSPageRouter.routes + THPageRouter.routes,
          //对原生导航的兼容；SmartDialog路由配置生命周期处理
          navigatorObservers: [GetXRouterObserver(), FlutterSmartDialog.observer, routeObserver],
          //默认页面动画
          defaultTransition: Transition.rightToLeft,
          smartManagement: SmartManagement.keepFactory,
          //网页Title显示
          title: 'YY Employer',
          //样式相关
          theme: ThemeConfig.lightTheme,
          // darkTheme: ThemeConfig.darkTheme,
          themeMode: ThemeMode.light,
          //本地化相关
          locale: TranslationService.locale,
          fallbackLocale: TranslationService.fallbackLocale,
          localizationsDelegates: const [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: const [
            Locale('en', 'US'),
            Locale('zh', ''),
            Locale('th', 'TH'),
          ],
          translations: TranslationService(),
          //SmartDialog初始化默认Loading与Toast
          builder: FlutterSmartDialog.init(
            toastBuilder: (String msg) {
              return CustomToastWidget(msg: msg);
            },
            loadingBuilder: (String msg) {
              return CustomLoadingWidget(msg: msg == 'loading...' ? 'Loading...'.tr : msg);
            },
            notifyStyle: FlutterSmartNotifyStyle(
              successBuilder: (String msg) => CustomSuccessWidget(msg: msg),
              failureBuilder: (String msg) => CustomFailureWidget(msg: msg),
              errorBuilder: (String msg) => CustomErrorWidget(msg: msg),
              alertBuilder: (String msg) => CustomErrorWidget(msg: msg),
              warningBuilder: (String msg) => CustomErrorWidget(msg: msg),
            ),
          ),
        ),
      );
    });
  }
}
