import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:tanami_app/core/styles/app_text.dart';

import '../../../../core/routes/route_name.dart';
import '../../../../core/routes/routes.dart';
import '../../../../core/styles/app_color.dart';
import '../../../../shared/components/bloc/password_field/password_visibility_bloc.dart';
import '../../../../shared/components/button_widget.dart';
import '../../../../shared/components/form_label_textfield.dart';
import '../../../../shared/components/text_widget.dart';
import '../bloc/change_password_bloc.dart';

class RestorePasswordForm extends StatelessWidget {
  const RestorePasswordForm({super.key});

  @override
  Widget build(BuildContext context) {
    final restorePasswordBloc = context.read<ChangePasswordBloc>();

    // Reset fields when the screen is built
    restorePasswordBloc.resetFields();

    return Form(
      key: restorePasswordBloc.formKey,
      child: Padding(
        padding: const EdgeInsets.symmetric(
          horizontal: 14,
        ),
        child: Align(
          alignment: Alignment.topLeft,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Gap(50),
              BlocProvider(
                create: (_) => PasswordVisibilityBloc(),
                child: FormLabelTextField(
                  hintText: AppText.enterPassword,
                  title: AppText.currentPsswordText,
                  type: AppText.password.toLowerCase(),
                  textEditingController:
                      restorePasswordBloc.currentPasswordTextField,
                ),
              ),
              const Gap(12),
              BlocProvider(
                create: (_) => PasswordVisibilityBloc(),
                child: FormLabelTextField(
                  hintText: AppText.enterPassword,
                  title: AppText.newPasswordText,
                  type: AppText.password.toLowerCase(),
                  textEditingController: restorePasswordBloc.passwordTextField,
                ),
              ),
              const Gap(12),
              BlocProvider(
                create: (_) => PasswordVisibilityBloc(),
                child: FormLabelTextField(
                  hintText: AppText.enterPassword,
                  title: AppText.repeatPasswordText,
                  type: AppText.password.toLowerCase(),
                  textEditingController:
                      restorePasswordBloc.repeatPasswordTextField,
                ),
              ),
              const Gap(12),
              Align(
                alignment: Alignment.topRight,
                child: ButtonWidget().textBtn(
                  function: () {
                    goRouter.pushNamed(
                        RouteName.forgotPasswordPhoneVerificationScreen);
                  },
                  text: TextWidget().text15W400(AppText.forgotPassword,
                      clr: AppColor.forgotPassButtonColor,
                      textDecoration: TextDecoration.underline),
                ),
              ),
              const Gap(20),
            ],
          ),
        ),
      ),
    );
  }
}
