import 'package:food_delivery_client/core/helpers/validator_helpers.dart';
import 'package:food_delivery_client/feature/common/presentation/widgets/app_text_form_field.dart';

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

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

  final String phoneNumber;

  @override
  State<CreateNewPasswordBody> createState() => _CreateNewPasswordBodyState();
}

class _CreateNewPasswordBodyState extends State<CreateNewPasswordBody> {
  late TextEditingController _passwordController;
  late TextEditingController _repeatPasswordController;
  final _formKey = GlobalKey<FormState>();
  bool _isValid = false;

  void _validateForm() {
    final isValid = _formKey.currentState?.validate() ?? false;
    if (_isValid != isValid) {
      setState(() {
        _isValid = isValid;
      });
    }
  }

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

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

  @override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey,
      autovalidateMode: AutovalidateMode.onUserInteraction,
      child: WLayout(
        child: Scaffold(
          resizeToAvoidBottomInset: true,
          body: SingleChildScrollView(
            keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
            physics: const NeverScrollableScrollPhysics(),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                30.verticalSpace,
                WBackButton(),
                20.verticalSpace,
                Text(
                  context.loc.createNewPassword,
                  style: context.appThemeTextStyles.size24Bold,
                ),
                8.verticalSpace,
                Text(
                  context.loc.createNewPasswordSubtitle,
                  style: context.appThemeTextStyles.size14Regular,
                ),

                40.verticalSpace,
                Text(
                  context.loc.newPassword,
                  style: context.appThemeTextStyles.size16Medium,
                ),
                10.verticalSpace,
                AppTextFormField(
                  obscureText: true,
                  controller: _passwordController,
                  keyBoardType: TextInputType.visiblePassword,
                  hintText: context.loc.enter_password,
                  validator: (value) {
                    return Validators.validatePassword(
                      _passwordController.text.trim(),
                    );
                  },
                ),
                16.verticalSpace,
                Text(
                  context.loc.confirmNewPassword,
                  style: context.appThemeTextStyles.size16Medium,
                ),
                10.verticalSpace,
                AppTextFormField(
                  obscureText: true,
                  controller: _repeatPasswordController,
                  keyBoardType: TextInputType.visiblePassword,
                  hintText: context.loc.enter_password,
                  validator: (value) {
                    return Validators.validateRepeatPassword(
                      _repeatPasswordController.text.trim(),
                      _passwordController.text.trim(),
                    );
                  },
                ),
                130.verticalSpace,
                AppButton(
                  name: context.loc.resetPassword,
                  isActive: _isValid,
                  onPressed: () {
                    ChangePasswordSuccessPassword().show(context);
                  },
                ),
              ],
            ).paddingSymmetric(horizontal: 24),
          ),
        ),
      ),
    );
  }
}
