import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'app/app.dart';
import 'app/core/utils/notification_service.dart';

DBHelper dbHelper = DBHelper();

Future<void> _initAppInitials() async {
  WidgetsFlutterBinding.ensureInitialized();

  // HttpOverrides.global = MyHttpOverrides();

  await ScreenUtil.ensureScreenSize();

  await initLoginStatus();
}

// Firebase initials is separated, app needs to be initialized
// regardless Firebase
Future<void> _initFBInitials() async {
  try {
    await initFCMFunctions();
  } catch (e) {
    debugPrint('FCM error: $e');
  }
}

Future<void> main() async {
  await _initAppInitials();

  final String locale = await getLocale();

  FlutterNativeSplash.remove();

  // FB functions needs to init after
  /// [FlutterNativeSplash.remove()]
  await _initFBInitials();

  runApp(ElektronikaShopApp(lang: locale));
}

class ElektronikaShopApp extends StatelessWidget {
  final String? lang;

  ElektronikaShopApp({required this.lang});

  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        return ScreenUtilInit(
          designSize: Size(constraints.maxWidth, constraints.maxHeight),
          builder: (_, child) => GetMaterialApp(
            initialRoute: lang == null ? AppRoutes.SPLASH : AppRoutes.DASHBOARD,
            getPages: AppPages.list,
            debugShowCheckedModeBanner: false,
            locale: lang == null ? Locale('tm', 'TM') : Locale(lang!, lang!.toUpperCase()),
            fallbackLocale: LocalizationService.fallbackLocale,
            translations: LocalizationService(),
            theme: AppTheme.light,
            darkTheme: AppTheme.dark,
            themeMode: ThemeMode.light, // ThemeMode.system
          ),
        );
      },
    );
  }
}

// cd ios
//arch -x86_64 pod install 
//arch -x86_64 pod install --repo-update