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

import '../../../../application/auth/register_form/register_form_bloc.dart';
import '../../../../injection.dart';
import '../../../components/button/button.dart';
import '../../../components/toast/flushbar.dart';
import '../../../router/app_router.gr.dart';
import 'widgets/birth_date_field.dart';
import 'widgets/name_field.dart';

@RoutePage()
class RegisterPage extends StatelessWidget implements AutoRouteWrapper {
  final String phoneNumber;
  const RegisterPage({super.key, required this.phoneNumber});

  @override
  Widget build(BuildContext context) {
    return BlocListener<RegisterFormBloc, RegisterFormState>(
      listenWhen: (p, c) =>
          p.failureOrRegisterOption != c.failureOrRegisterOption,
      listener: (context, state) {
        state.failureOrRegisterOption.fold(
          () {},
          (either) => either.fold(
            (f) => AppFlushbar.showAuthFailureToast(context, f),
            (data) {
              AppFlushbar.showSuccess(context, data.message);
              Future.delayed(Duration(milliseconds: 1000), () {
                context.router.push(
                  OtpRoute(
                    registrationToken: data.registrationToken,
                    phoneNumber: phoneNumber,
                  ),
                );
              });
            },
          ),
        );
      },
      child: Scaffold(
        appBar: AppBar(title: const Text('Daftar')),
        body: BlocBuilder<RegisterFormBloc, RegisterFormState>(
          builder: (context, state) {
            return Padding(
              padding: const EdgeInsets.symmetric(horizontal: 24.0),
              child: Form(
                autovalidateMode: state.showErrorMessages
                    ? AutovalidateMode.always
                    : AutovalidateMode.disabled,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const SizedBox(height: 40),

                    RegisterNameField(),
                    SizedBox(height: 24),
                    RegisterBirthDateField(),

                    const SizedBox(height: 50),

                    Spacer(),

                    // Continue Button
                    AppElevatedButton(
                      onPressed: state.isSubmitting
                          ? null
                          : () => context.read<RegisterFormBloc>().add(
                              RegisterFormEvent.submitted(),
                            ),
                      title: 'Daftar & Lanjutkan',
                      isLoading: state.isSubmitting,
                    ),

                    const SizedBox(height: 24),
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  @override
  Widget wrappedRoute(BuildContext context) => BlocProvider(
    create: (context) =>
        getIt<RegisterFormBloc>()
          ..add(RegisterFormEvent.phoneNumberChanged(phoneNumber)),
    child: this,
  );
}
