import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:example/theme/example_theme.dart';
import 'package:get/get.dart';
import 'package:guru_widgets/theme/guru_theme.dart';
import 'package:guru_utils/router/router.dart';

import 'route/router.dart';


void main() async {
  runZonedGuarded(
      () => runApp(GuruTheme(
            guruTheme: ExampleThemes.defaultGuruTheme,
            child: GetMaterialApp(
              title: "GURU DEBUG",
              theme: ExampleThemes.defaultLightTheme,
              initialRoute: AppPages.initialPath,
              routingCallback: RoutingObserver.listener,
              localizationsDelegates: const [
                // 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("en", "US");
                Get.locale = matchLocale;
                return matchLocale;
              },
              getPages: AppPages.routes,
            ),
          )),
      _recordError);
}

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