import 'package:farm_tpf/presentation/screens/login/login_page.dart';
import 'package:farm_tpf/presentation/screens/tabbar/tabbar.dart';
import 'package:farm_tpf/routes/route_name.dart';
import 'package:farm_tpf/routes/routes.dart';
import 'package:farm_tpf/services/firebase_notification_service.dart';
import 'package:farm_tpf/themes/themes.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'authentication/bloc/authentication_bloc.dart';
import 'common/const_enum.dart';
import 'data/repository/auth_repository.dart';
import 'string/app_strings.dart';

class App extends StatelessWidget {
  final AuthenticationRepository authenticationRepository;

  const App({
    Key? key,
    required this.authenticationRepository,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return RepositoryProvider.value(
      value: authenticationRepository,
      child: MultiBlocProvider(
        providers: [
          BlocProvider(
            create: (_) => AuthenticationBloc(
              repository: authenticationRepository,
            ),
          ),
          // BlocProvider(
          //   create: (_) => LoginCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => NotificationBloc(Repository()),
          // ),
          // BlocProvider(
          //   create: (_) => WorkCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => FertilizationCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => InfomationFertilizationCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => WorkDetailCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => CowCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => NoteCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => WorkSlipCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => FertilityCheckupCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => InseminateCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => ReproductiveTreatmentCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => AntenatalCheckupCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => CreateWorkCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => NhapBoCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => NhapBeCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => DanBoBloc(),
          // ),
          // BlocProvider(
          //   create: (_) => GiaPhaCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => ThuYBoBenhCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => KhamBenhCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => DieuTriCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => LichSuDieuTriCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => BoBenhBloc(),
          // ),
          // BlocProvider(
          //   create: (_) => ThanhLyCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => ChangePasswordCubit(),
          // ),
          // BlocProvider(
          //   create: (_) => BoBenhThanhLyBloc(),
          // ),
        ],
        child: const AppView(),
      ),
    );
  }
}

class AppView extends StatefulWidget {
  const AppView({
    Key? key,
  }) : super(key: key);
  @override
  _AppViewState createState() => _AppViewState();
}

class _AppViewState extends State<AppView> {
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (BuildContext context, Widget? child) {
        return GetMaterialApp(
          title: 'Trang Trai Bo',
          debugShowCheckedModeBanner: false,
          onGenerateRoute: Routes.buildRoutes,
          initialRoute: RouteName.splash,
          theme: AppTheme.lightThem,
          localizationsDelegates: [
            GlobalMaterialLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
          ],
          supportedLocales: const [
            Locale('vi', 'VN'),
            Locale('en', 'US'),
          ],
          locale: const Locale('vi', 'VN'),
          fallbackLocale: const Locale('vi', 'VN'),
          translations: AppStrings(),
          builder: (context, child) {
            final mediaQueryData = MediaQuery.of(context);
            final scale = mediaQueryData.textScaleFactor.clamp(1.0, 1.2);
            FirebaseNotificationService.initService();
            return MediaQuery(
              data: MediaQuery.of(context).copyWith(textScaleFactor: scale),
              child: BlocListener<AuthenticationBloc, AuthenticationState>(
                listener: (context, state) {
                  switch (state.status) {
                    case AuthenticationStatus.authenticated:
                      Get.offAll(() => TabbarScreen());
                      break;
                    case AuthenticationStatus.unauthenticated:
                      Get.offAll(() => const LoginPage());
                      break;
                    default:
                      Get.offAll(() => const LoginPage());
                      break;
                  }
                },
                child: child!,
              ),
            );
          },
        );
      },
    );
  }
}
