import 'dart:io';

import 'package:easy_localization/easy_localization.dart';

import 'package:flutter/material.dart';
import 'package:mc_common_app/theme/app_theme.dart';
import 'package:logger/logger.dart';
import 'package:provider/provider.dart';
import 'package:provider/single_child_widget.dart';
import 'package:sizer/sizer.dart';

final navigatorKey = GlobalKey<NavigatorState>();

Logger logger = Logger(
  printer: PrettyPrinter(
    lineLength: 0,
  ),
);

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();

  await EasyLocalization.ensureInitialized();
  // AppState().setPostParamsInitConfig();
  HttpOverrides.global = MyHttpOverrides();

  runApp(
    EasyLocalization(
      supportedLocales: const <Locale>[
        Locale('en', 'US'),
        Locale('ar', 'SA'),
      ],
      path: 'resources',
      // assetLoader: const CodegenLoader(),
      child: MultiProvider(
        providers: const <SingleChildWidget>[
          // ChangeNotifierProvider<LoginProviderModel>(
          //   create: (_) => LoginProviderModel(),
          // ),
        ],
        child: const MyApp(),
      ),
    ),
  );
}

// todo terminal command to genertate translation files
// flutter pub run easy_localization:generate -S resources -f keys -O lib/generated -o locale_keys.g.dart

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

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, constraints) {
      return Sizer(
        builder: (
          BuildContext context,
          Orientation orientation,
          DeviceType deviceType,
        ) {
          List<LocalizationsDelegate<dynamic>> delegates = context.localizationDelegates;
          // AppState().setPostParamsModel(
          //   PostParamsModel(
          //     languageID: EasyLocalization.of(context)?.locale.languageCode == "ar" ? 1 : 2,
          //   ),
          // );
          return MaterialApp(
            // key: navigatorKey,
            navigatorKey: navigatorKey,
            theme: AppTheme.getTheme(
              isArabic: EasyLocalization.of(context)?.locale.languageCode == "ar",
            ),
            debugShowCheckedModeBanner: false,
            localizationsDelegates: delegates,
            supportedLocales: context.supportedLocales,
            locale: context.locale,
          );
        },
      );
    });
  }
}

// class MyApp extends StatelessWidget {
//   MyApp({super.key}) {
//     AppDependencies.addDependencies();
//     AppState = Injector.appInstance.get<AppState>();
//     // AppState.setPostParamsInitConfig();
//   }
//
//   @override
//   Widget build(BuildContext context) {
//     return LayoutBuilder(builder: (context, constraints) {
//       return Sizer(
//         builder: (
//           BuildContext context,
//           Orientation orientation,
//           DeviceType deviceType,
//         ) {
//           SizeConfig().init(constraints, orientation);
//           List<LocalizationsDelegate<dynamic>> delegates = context.localizationDelegates;
//           // AppState().setPostParamsModel(
//           //   PostParamsModel(
//           //     languageID: EasyLocalization.of(context)?.locale.languageCode == "ar" ? 1 : 2,
//           //   ),
//           // );
//           return MaterialApp(
//             // key: navigatorKey,
//             navigatorKey: navigatorKey,
//             theme: AppTheme.getTheme(
//               EasyLocalization.of(context)?.locale.languageCode == "ar",
//             ),
//             debugShowCheckedModeBanner: false,
//             localizationsDelegates: delegates,
//             supportedLocales: context.supportedLocales,
//             locale: context.locale,
//             initialRoute: AppRoutes.initialPage,
//             routes: AppRoutes.routes,
//           );
//         },
//       );
//     });
//   }
// }
