import 'package:food_delivery_client/core/helpers/validator_helpers.dart';
import 'package:food_delivery_client/feature/auth/domain/usecases/reset_password_usecase.dart';
import 'package:food_delivery_client/feature/auth/presentation/blocs/reset_password_bloc/reset_password_bloc.dart';
import 'package:food_delivery_client/feature/auth/presentation/widgets/w_auth_background.dart';

import '../../../../../food_delivery_client.dart';

class ResetPasswordPage extends StatefulWidget {
  const ResetPasswordPage({super.key, required this.phoneNumber});

  final String phoneNumber;

  @override
  State<ResetPasswordPage> createState() => _ResetPasswordPageState();
}

class _ResetPasswordPageState extends State<ResetPasswordPage> {
  late TextEditingController _passwordController;
  late TextEditingController _repeatPasswordController;
  final _formKey = GlobalKey<FormState>();

  @override
  void initState() {
    _passwordController = TextEditingController();
    _repeatPasswordController = TextEditingController();
    super.initState();
  }

  @override
  void dispose() {
    _passwordController.dispose();
    _repeatPasswordController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => sl<ResetPasswordBloc>(),
      child: BlocConsumer<ResetPasswordBloc, ResetPasswordState>(
        listener: (context, state) {
          if (state.status.isLoaded()) {
            context.go(Routes.login);
          }
        },
        builder: (context, state) {
          return Form(
            key: _formKey,
            child: WLayout(
              top: false,
              child: Scaffold(
                body: WAuthBackground(
                  child: SizedBox(
                    width: context.w,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        25.verticalSpace,
                        Text(
                          context.loc.reset_password,
                          style: AppTextStyles.size20Medium,
                        ),
                        20.verticalSpace,
                        Text(
                          context.loc.new_password,
                          style: AppTextStyles.size14Regular.copyWith(
                            color: AppColors.c6A6E7F,
                          ),
                        ),
                        5.verticalSpace,
                        AppTextFormField(
                          obscureText: true,
                          controller: _passwordController,
                          borderRadius: AppUtils.kBorderRadius8,
                          hintText: context.loc.enter_password,
                          keyBoardType: TextInputType.visiblePassword,
                          validator: (value) {
                            return Validators.validatePassword(
                              _passwordController.text.trim(),
                            );
                          },
                        ),
                        20.verticalSpace,
                        Text(
                          context.loc.password,
                          style: AppTextStyles.size14Regular.copyWith(
                            color: AppColors.c6A6E7F,
                          ),
                        ),
                        5.verticalSpace,
                        AppTextFormField(
                          obscureText: true,
                          controller: _repeatPasswordController,
                          borderRadius: AppUtils.kBorderRadius8,
                          hintText: context.loc.enter_password,
                          keyBoardType: TextInputType.visiblePassword,
                          validator: (value) {
                            return Validators.validateRepeatPassword(
                              _repeatPasswordController.text.trim(),
                              _passwordController.text.trim(),
                            );
                          },
                        ),
                        25.verticalSpace,
                        AppButton(
                          name: context.loc.continue_str,
                          trailing: SvgPicture.asset(
                            AppIcons.icArrowRightLight,
                          ).paddingOnly(left: 8),
                          isLoading: state.status.isLoading(),
                          onPressed: () {
                            if (_formKey.currentState?.validate() ?? false) {
                              context.read<ResetPasswordBloc>().add(
                                ResetPasswordEvent.loaded(
                                  ResetPasswordParams(
                                    phoneNumber: widget.phoneNumber,
                                    newPassword: _passwordController.text
                                        .trim(),
                                  ),
                                ),
                              );
                            }
                          },
                          backgroundColor: AppColors.c34A853,
                          borderRadius: 15,
                        ),
                      ],
                    ).paddingSymmetric(horizontal: 16),
                  ),
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
