import 'package:car_customer_app/config/customer_dependencies.dart';
import 'package:car_customer_app/config/customer_routes.dart';
import 'package:car_customer_app/view_models/dashboard_view_model.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mc_common_app/classes/app_state_cm.dart';
import 'package:mc_common_app/services/services.dart';
import 'package:mc_common_app/theme/app_theme.dart';
import 'package:mc_common_app/config/dependencies.dart';
import 'package:mc_common_app/config/routes.dart';
import 'package:mc_common_app/models/post_params_model.dart';
import 'package:mc_common_app/repositories/user_repo.dart';
import 'package:mc_common_app/view_models/base_view_model.dart';
import 'package:mc_common_app/view_models/user_view_model.dart';
import 'package:sizer/sizer.dart';
import 'package:provider/provider.dart';
import 'package:provider/single_child_widget.dart';

import 'generated/codegen_loader.g.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  CustomerDependencies.addDependencies();
  await EasyLocalization.ensureInitialized();
  CustomerAppRoutes.routes.addAll(AppRoutes.routes);
  runApp(
    EasyLocalization(
      supportedLocales: const [Locale('ar', 'SA'), Locale('en', 'US')],
      fallbackLocale: const Locale('en', 'US'),
      startLocale: const Locale('en', 'US'),
      assetLoader: const CodegenLoader(),
      path: "assetsl/langs",
      child: MultiProvider(
        providers: <SingleChildWidget>[
          ChangeNotifierProvider<BaseVM>(
            create: (_) => BaseVM(),
          ),
          ChangeNotifierProvider<DashboardVM>(
            create: (_) => DashboardVM(
              userRepo: injector.get<UserRepo>(),
              commonServices: injector.get<CommonServices>(),
            ),
          ),
          ChangeNotifierProvider<UserVM>(
            create: (_) => UserVM(userRepo: injector.get<UserRepo>()),
          ),
        ],
        child: const MyApp(),
      ),
    ),
  );
}

// todo terminal command to generate translation files
// flutter pub run easy_localization:generate --source-dir ./assets/langs
// todo terminal command to generate translation keys
// flutter pub run easy_localization:generate --source-dir ./assets/langs -f keys -o locale_keys.g.dart
// command to generate languages data from json

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

  @override
  Widget build(BuildContext context) {
    return Sizer(
      builder: (context, orientation, deviceType) {
        AppStateCm().setPostParamsModel(
          PostParamsModel(
            languageID: EasyLocalization.of(context)?.locale.languageCode == "ar" ? 1 : 2,
          ),
        );
        return MaterialApp(
          theme: AppTheme.getTheme(EasyLocalization.of(context)?.locale.languageCode == "ar"),
          debugShowCheckedModeBanner: false,
          localizationsDelegates: context.localizationDelegates,
          supportedLocales: context.supportedLocales,
          locale: context.locale,
          initialRoute: AppRoutes.initialRoute,
          routes: CustomerAppRoutes.routes,
        );
      },
    );
  }
}
