import 'dart:io';

import 'package:boilerplate/core/utils/notification_util.dart';
import 'package:boilerplate/presentation/bloc/landing/bottom_navigation_cubit.dart';
import 'package:boilerplate/presentation/bloc/notification/notification_count/notification_count_cubit.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart' as webview;
import 'package:shared_preferences/shared_preferences.dart';

import 'core/di/injection_init.dart';
import 'core/flavor/flavor_manager.dart';
import 'core/utils/locale/all_translation.dart';
import 'core/utils/navigator_util.dart';
import 'core/utils/shared_preferences_util.dart';
import 'domain/entities/flavor/flavor_data.dart';
import 'presentation/screen/landing/landing_screen.dart';

class MyHttpOverrides extends HttpOverrides {
  @override
  HttpClient createHttpClient(SecurityContext? context) {
    return super.createHttpClient(context)
      ..badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
  }
}

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

  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
  ]);

  HttpOverrides.global = new MyHttpOverrides();

  //#region Firebase

  await Firebase.initializeApp().whenComplete(() {
    NotificationUtil.initial();
  });

  //#endregion

  //#region Set InAppWebView Config

  if (Platform.isAndroid) {
    webview.AndroidInAppWebViewController.setWebContentsDebuggingEnabled(
      true,
    );
  }
  //#endregion

  //#region Manual register get it
  configureDependencies();
  /*Add flavor setting*/
  AppFlavorData setting = await FlavorManager.getFlavorSetting();
  getIt.registerSingleton<AppFlavorData>(setting.copyWith(showApiDebug: true));
  // await allTranslations.init('th'); // th | en

  SharedPreferencesUtil sharedPreferencesUtil =
      await SharedPreferencesUtil.getInstance();
  getIt.registerSingleton<SharedPreferencesUtil>(sharedPreferencesUtil);
  //#endregion
  final flavorSetting = getIt.get<AppFlavorData>();
  SharedPreferences prefs = await SharedPreferences.getInstance();

  if (prefs.getString('lang') == "" ||
      prefs.getString('lang') == null ||
      prefs.getString('lang') == "th") {
    prefs.setString('lang', 'th');
  } else {
    prefs.setString('lang', 'en');
  }

  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      builder: (BuildContext context, Widget? child) {
        return MultiBlocProvider(
          providers: [
            BlocProvider(create: (_) => BottomNavigationCubit()),
            BlocProvider(create: (_) => getIt.get<NotificationCountCubit>())
          ],
          child: MaterialApp(
            debugShowCheckedModeBanner: false,
            navigatorKey: NavigatorUtil.navigate,
            home: LandingScreem(),
            localizationsDelegates: [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],
            supportedLocales: [
              const Locale('th'),
              const Locale('en'),
            ],
            locale: Locale('th'),
          ),
        );
      },
    );
  }
}
