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

import 'core/routes/routes.dart';
import 'core/utils/connectivity/network_connectivity.dart';
import 'features/countrySelection/presentation/bloc/choose_country_bloc.dart';
import 'shared/components/bloc/bottom_nav_bar/bottom_navigation_bloc.dart';

/* CREATED BY - JAYESH JAIN 
   DATE - 24-05-2024 
 */

/// The main function that runs the application.
Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // FlutterError.onError = (FlutterErrorDetails details) {
  //   FlutterError.dumpErrorToConsole(details);
  //   runApp(CustomErrorWidget(
  //     errorMessage: details.toString(),
  //   ));
  // };
// Set the preferred orientations of the device.
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
  ]).then((value) => runApp(const MyApp()));
}

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

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

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  final NetworkConnectivity _networkConnectivity =
      NetworkConnectivity(onStatusChange: (_) {});
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);

    // Initialize the NetworkConnectivity instance.
    _networkConnectivity.initialize();
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    _networkConnectivity.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider(
          create: (context) => RadioBloc(),
        ),
        BlocProvider(
          create: (_) => BottomNavigationBloc(),
        )
      ],
      child: ScreenUtilInit(
        builder: (BuildContext context, Widget? child) => MaterialApp.router(
          title: 'Tanami Capital',
          theme: ThemeData(
            useMaterial3: true,
          ),
          debugShowCheckedModeBanner: false,
          routerConfig: goRouter,
        ),
        designSize: const Size(390, 844),
      ),
    );
  }
}
