import 'package:farm_tpf/presentation/screens/splash/view/splash_page.dart';
import 'package:farm_tpf/presentation/screens/tabbar/tabbar.dart';
import 'package:farm_tpf/utils/const_color.dart';
import 'package:farm_tpf/utils/const_enum.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'authentication/bloc/authentication_bloc.dart';
import 'data/repository/authentication_repository.dart';
import 'presentation/screens/login/login_page.dart';

final GlobalKey<NavigatorState> globalNavigator = GlobalKey<NavigatorState>();

class App extends StatelessWidget {
  const App({
    Key? key,
    required this.authenticationRepository,
  })  : assert(authenticationRepository != null),
        super(key: key);

  final AuthenticationRepository authenticationRepository;

  @override
  Widget build(BuildContext context) {
    return RepositoryProvider.value(
      value: authenticationRepository,
      child: BlocProvider(
        create: (_) => AuthenticationBloc(
          repository: authenticationRepository,
        ),
        child: AppView(),
      ),
    );
  }
}

class AppView extends StatefulWidget {
  @override
  _AppViewState createState() => _AppViewState();
}

class _AppViewState extends State<AppView> {
  final _navigatorKey = GlobalKey<NavigatorState>();

  NavigatorState? get _navigator => _navigatorKey.currentState;

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (BuildContext context, Widget? child) {
        return GetMaterialApp(
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            backgroundColor: Colors.white,
            brightness: Brightness.light,
            primaryColor: AppColors.DEFAULT,
            accentColor: AppColors.DEFAULT,
            hoverColor: AppColors.GREEN,
            // textTheme: GoogleFonts.sairaTextTheme(
            //   Theme.of(context).textTheme,
            // ),
          ),
          navigatorKey: _navigatorKey,
          builder: (context, child) {
            return BlocListener<AuthenticationBloc, AuthenticationState>(
              listener: (context, state) {
                switch (state.status) {
                  case AuthenticationStatus.authenticated:
                    // _navigator!.pushAndRemoveUntil<void>(
                    //   TabbarScreen.route(),
                    //   (route) => false,
                    // );
                    Get.offAll(() => TabbarScreen());
                    break;
                  case AuthenticationStatus.unauthenticated:
                    // _navigator!.pushAndRemoveUntil<void>(
                    //   LoginPage.route(),
                    //   (route) => false,
                    // );
                    Get.offAll(() => const LoginPage());
                    break;
                  default:
                    break;
                }
              },
              child: child,
            );
          },
          onGenerateRoute: (_) => SplashPage.route(),
        );
      },
    );
  }
}
