import 'package:flutter/material.dart';
import 'package:rasadyar_app/infrastructure/service/app_navigation_observer.dart';
import 'package:rasadyar_app/infrastructure/utils/local_storage_utils.dart';
import 'package:rasadyar_app/presentation/routes/app_pages.dart';
import 'package:rasadyar_core/core.dart';

import 'infrastructure/di/di.dart';
import 'presentation/routes/auth_route_resolver_impl.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Hive.initFlutter();
  await setupPreInjection();
  Get.put(GService());
  var gService = Get.find<GService>();
  await gService.init();
  if (gService.isFirstTime()) {
    await seedTargetPage();
    await gService.setIsNotFirstTime();
  }
  Get.put(TokenStorageService());
  await Get.find<TokenStorageService>().init();
  Get.put<AuthRouteResolver>(AppAuthRouteResolver());
  Get.put(AuthMiddleware());

  runApp(MyApp());

   //runApp(DevicePreview(builder: (context) => ForDevicePreview(),));
}

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

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(412, 917),
      minTextAdapt: true,
      splitScreenMode: true,
      child: GetMaterialApp(
        title: 'رصدیار',
        theme: ThemeData(
          fontFamily: 'yekan',
          colorScheme: ColorScheme.fromSeed(seedColor: AppColor.blueNormal),
        ),
        initialRoute: AppPages.initRoutes,
        getPages: AppPages.pages,
        locale: const Locale("fa", "IR"),
        supportedLocales: const [Locale("fa", "IR")],
        navigatorObservers: [CustomNavigationObserver()],
        localizationsDelegates: [
          PersianMaterialLocalizations.delegate,
          PersianCupertinoLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(412, 917),
      minTextAdapt: true,
      splitScreenMode: true,
      child: GetMaterialApp(
        title: 'رصدیار',
        theme: ThemeData(
          fontFamily: 'yekan',
          colorScheme: ColorScheme.fromSeed(seedColor: AppColor.blueNormal),
        ),
        initialRoute: AppPages.initRoutes,
        getPages: AppPages.pages,
        locale: const Locale("fa", "IR"),
        supportedLocales: const [Locale("fa", "IR")],
        navigatorObservers: [CustomNavigationObserver()],
        localizationsDelegates: [
          PersianMaterialLocalizations.delegate,
          PersianCupertinoLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
      ),
    );
  }
}
