import 'package:ambito/src/domain/entity/baumarten/baumarten.dart';
import 'package:ambito/src/domain/entity/massnahme/massnahme.dart';
import 'package:ambito/src/packages/ambito_api/base.dart';
import 'package:ambito/src/pages/loading/loading_page.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'package:isar/isar.dart';
import 'package:logger/logger.dart';
import 'package:syncfusion_localizations/syncfusion_localizations.dart';

final Logger logger = Logger(
  printer: PrettyPrinter(),
);

late Isar isar;

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

  await dotenv.load(fileName: '.env');

  await Isar.initialize();
  const dir = Isar.sqliteInMemory;
  const engine = kIsWeb ? IsarEngine.sqlite : IsarEngine.isar;
  isar = Isar.open(
    schemas: [BaumartenSchema, MassnahmeSchema],
    directory: dir,
    engine: engine,
    inspector: true,
  );

  await Future.wait([
    BaseApi().getContent('baumarten'),
    BaseApi().getContent('massnahmen'),
  ]);

  runApp(const Ambito());
}

class Ambito extends StatelessWidget {
  const Ambito({super.key});
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      localizationsDelegates: [
        FlutterI18nDelegate(
          translationLoader: FileTranslationLoader(
            fallbackFile: 'de',
            basePath: 'i18n',
          ),
          missingTranslationHandler: (key, locale) {
            if (kDebugMode) {
              print("--- Missing Key: $key, languageCode: $locale");
            }
          },
        ),
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        SfGlobalLocalizations.delegate,
      ],
      title: 'Flutter Demo',
      supportedLocales: const [Locale('de')],
      locale: const Locale('de'),
      builder: FlutterI18n.rootAppBuilder(),
      theme: ThemeData(
        scaffoldBackgroundColor: Colors.white,
        useMaterial3: true,
      ),
      home: const LoadingPage(),
    );
  }
}
