import 'package:betting/routes/app_pages.dart';
import 'package:betting/translations/localization_service.dart';
import 'package:betting/view/mobile/screen_config/screen_config_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'binding/Splash_binding.dart';
import 'binding/app_binding.dart';
import 'commom/utils/global.dart';
import 'commom/utils/preference_utils.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter_device_type/flutter_device_type.dart';
import 'firebase_options.dart';
import 'notification/notification_service.dart';


void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await PreferenceUtils.init();
  initGoogleMobileAds();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  AppBinding().dependencies();
  runApp(const MyApp());
}

Future<InitializationStatus> initGoogleMobileAds() {
  // TODO: Initialize Google Mobile Ads SDK
  return MobileAds.instance.initialize();
}


class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // This widget is the root of your application.
  ScreenConfigController screenConfigController = Get.find<ScreenConfigController>();
  @override
  Widget build(BuildContext context) {
    LocalNotificationService.initialize(context);
    if(GetPlatform.isAndroid || GetPlatform.isIOS && Device.get().isPhone){
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.landscapeLeft,
        DeviceOrientation.landscapeRight,
        DeviceOrientation.portraitDown,
        DeviceOrientation.portraitUp
      ]);
    }
    return OrientationBuilder(
        builder: (BuildContext context, Orientation orientation) {
          if (mounted) {
            if (Device.get().isTablet) {
              if (orientation == Orientation.portrait) {
                screenConfigController.setIsIpadLandScape(false);
                screenConfigController.setSize(const Size(600, 780));
              } else {
                screenConfigController.setIsIpadLandScape(true);
                screenConfigController.setSize(const Size(600, 780));
              }
            } else {
              screenConfigController.setSize(const Size(360, 690));
            }
          }
          return GetBuilder<ScreenConfigController>(
              builder: (screenConfigController1) {
                return ScreenUtilInit(
                  splitScreenMode: true,
                  minTextAdapt: true,
                  designSize: screenConfigController1.size,
                  builder: (context, widget) => GetMaterialApp(
                    title: 'Betting',
                    color: Colors.white,
                    debugShowCheckedModeBanner: false,
                    // theme: ThemeData(useMaterial3: true, colorScheme: lightColorScheme),
                    // darkTheme: ThemeData(useMaterial3: true, colorScheme: darkColorScheme),
                    builder: EasyLoading.init(builder: ((context, widget) {
                      return MediaQuery(
                        data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
                        child: GestureDetector(
                            onTap: () => dismissKeyboard(),
                            behavior: HitTestBehavior.translucent,
                            child: widget!),
                      );
                    })),
                    locale: LocalizationService.locale,
                    translations: LocalizationService(),
                    getPages: AppPages.pages,
                    initialRoute: Routes.started,
                    initialBinding: SplashBinding(),
                    localizationsDelegates: const [
                      GlobalMaterialLocalizations.delegate,
                      // DefaultCupertinoLocalizations.delegate,
                      GlobalCupertinoLocalizations.delegate, // Here !
                      DefaultWidgetsLocalizations.delegate,
                    ],
                    supportedLocales: const [
                      Locale('vi'),
                      Locale('en'),
                    ],
                  ),
                );
              }
          );
        });
  }



}
