import 'dart:async';

import 'package:daily_challenge/daily_challenge_package.dart';
import 'package:example/daily_challenge/daily_challenge.dart';
import 'package:example/data/database/ui_db.dart';
import 'package:example/data/settings/ui_settings.dart';
import 'package:example/route/router.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get/get.dart';
import 'package:guru_utils/property/app_property.dart';
import 'package:guru_utils/router/router.dart';
import 'package:guru_widgets/theme/guru_theme.dart';

import 'generated/l10n.dart';
import 'theme/example_theme.dart';

Future initialize() async {
  WidgetsFlutterBinding.ensureInitialized();
  await UiDB.instance.initDatabase();
  AppProperty.initialize(UiDB.instance, cacheSize: 256);
  await UiSettings.instance.refresh();

  final dailyChallenge = DailyChallengePackage(DailyChallengeDelegateImpl());
  await dailyChallenge.initialize();
}

void main() async {
  await initialize();
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
  runZonedGuarded(
      () => runApp(GuruTheme(
            guruTheme: ExampleThemes.defaultGuruTheme,
            child: GetMaterialApp(
              title: "GURU UIUX",
              theme: ExampleThemes.defaultLightTheme,
              initialRoute: AppPages.initialPath,
              routingCallback: RoutingObserver.listener,
              localizationsDelegates: const [
                S.delegate,
                // Built-in localization of basic text for Material widgets
                GlobalMaterialLocalizations.delegate,
                // Built-in localization for text direction LTR/RTL
                GlobalWidgetsLocalizations.delegate,
                // Built-in localization of basic text for Cupertino widgets
                GlobalCupertinoLocalizations.delegate,
              ],
              localeResolutionCallback: (locale, supportedLocales) {
                // Check if the current device locale is supported
                //supportedLocales.firstWhere((supportedLocale) => supportedLocale.languageCode == locale.languageCode, orElse: () => supportedLocales.first),
                Locale? matchLocale;
                if (locale != null) {
                  for (var supportedLocale in supportedLocales) {
                    if (supportedLocale.languageCode == locale.languageCode &&
                        supportedLocale.countryCode == locale.countryCode) {
                      matchLocale = supportedLocale;
                      break;
                    }
                  }

                  if (matchLocale == null) {
                    for (var supportedLocale in supportedLocales) {
                      if (supportedLocale.languageCode == locale.languageCode) {
                        matchLocale = supportedLocale;
                        break;
                      }
                    }
                  }
                }
                matchLocale = matchLocale ?? const Locale("ar");
                Get.locale = matchLocale;
                return matchLocale;
              },
              getPages: AppPages.routes,
              // routeInformationParser: GetInformationParser(
              //     // initialRoute: Routes.HOME,
              //     ),
              // routerDelegate: GetDelegate(
              //   backButtonPopMode: PopMode.History,
              //   preventDuplicateHandlingMode:
              //       PreventDuplicateHandlingMode.ReorderRoutes,
              // ),
            ),
          )),
      _recordError);
}

Future<void> _recordError(dynamic exception, StackTrace stack) async {
  debugPrint("record error! ===> $exception $stack");
}
