import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../application/auth/login_form/login_form_bloc.dart';
import '../../../../common/constant/app_constant.dart';
import '../../../../common/theme/theme.dart';
import '../../../../injection.dart';
import '../../../components/assets/assets.gen.dart';
import '../../../components/button/button.dart';
import '../../../components/spaces/space.dart';
import '../../../components/toast/flushbar.dart';
import '../../../router/app_router.gr.dart';
import 'widgets/email_field.dart';
import 'widgets/password_field.dart';

@RoutePage()
class LoginPage extends StatelessWidget implements AutoRouteWrapper {
  const LoginPage({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocListener<LoginFormBloc, LoginFormState>(
      listenWhen: (previous, current) =>
          previous.failureOrLoginOption != current.failureOrLoginOption,
      listener: (context, state) {
        state.failureOrLoginOption.fold(
          () => null,
          (either) => either.fold(
            (f) => AppFlushbar.showAuthFailureToast(context, f),
            (data) {
              if (context.mounted) {
                // context.read<AuthBloc>().add(AuthEvent.fetchCurrentUser());
                context.router.replace(const SyncRoute());
              }
            },
          ),
        );
      },
      child: Scaffold(
        body: BlocBuilder<LoginFormBloc, LoginFormState>(
          builder: (context, state) {
            return Form(
              autovalidateMode: state.showErrorMessages
                  ? AutovalidateMode.always
                  : AutovalidateMode.disabled,
              child: ListView(
                padding: const EdgeInsets.symmetric(
                  horizontal: 260.0,
                  vertical: 20.0,
                ),
                children: [
                  const SpaceHeight(60.0),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 130.0),
                    child: Assets.images.logo.image(width: 100, height: 100),
                  ),
                  const SpaceHeight(16.0),
                  Center(
                    child: Text(
                      AppConstant.appName,
                      style: AppStyle.lg.copyWith(fontWeight: FontWeight.w700),
                    ),
                  ),
                  const SpaceHeight(8.0),
                  Center(
                    child: Text(
                      'Akses Login Kasir Resto',
                      style: AppStyle.sm.copyWith(
                        color: AppColor.textSecondary,
                      ),
                    ),
                  ),
                  const SpaceHeight(20.0),
                  const LoginEmailField(),
                  const SpaceHeight(12.0),
                  const LoginPasswordField(),
                  const SpaceHeight(24.0),
                  AppElevatedButton.filled(
                    onPressed: state.isSubmitting
                        ? null
                        : () => context.read<LoginFormBloc>().add(
                            const LoginFormEvent.submitted(),
                          ),
                    textColor: state.isSubmitting
                        ? AppColor.primary
                        : Colors.white,
                    label: 'Masuk',
                    isLoading: state.isSubmitting,
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );
  }

  @override
  Widget wrappedRoute(BuildContext context) =>
      BlocProvider(create: (context) => getIt<LoginFormBloc>(), child: this);
}
