import 'package:food_delivery_client/core/helpers/formatters.dart';
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';

class ForgotPasswordBody extends StatefulWidget {
  const ForgotPasswordBody({super.key});

  @override
  State<ForgotPasswordBody> createState() => _ForgotPasswordBodyState();
}

class _ForgotPasswordBodyState extends State<ForgotPasswordBody> {
  late final TextEditingController _phoneNumberController;
  final _formKey = GlobalKey<FormState>();
  bool _isValid = false;

  void _validateForm() {
    final isValid = _formKey.currentState?.validate() ?? false;

    if (isValid != _isValid) {
      setState(() {
        _isValid = isValid;
      });
    }
  }

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

  @override
  void dispose() {
    _phoneNumberController.removeListener(_validateForm);
    _phoneNumberController.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,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                30.verticalSpace,
                WBackButton(),
                20.verticalSpace,
                Text(
                  context.loc.forgotPassword,
                  style: context.appThemeTextStyles.size24Bold,
                ),
                8.verticalSpace,
                Text(
                  context.loc.forgotPasswordSubtitle,
                  style: AppTextStyles.size14Regular.copyWith(
                    color: AppColors.cA7AEC1,
                    height: 1.6,
                  ),
                ).paddingOnly(right: 50),
                30.verticalSpace,
                Text(
                  context.loc.phoneNumber,
                  style: context.appThemeTextStyles.size16Medium,
                ),
                10.verticalSpace,
                AppTextFormField(

                  controller: _phoneNumberController,
                  keyBoardType: TextInputType.number,
                  hintText: context.loc.enter_email_or_phone,
                  prefixIcon: Text(
                    "+ 998",
                    style: context.appThemeTextStyles.size14Regular,
                  ),
                  inputFormatters: [
                    FilteringTextInputFormatter.digitsOnly,
                    Formatters.phoneFormatter,
                    LengthLimitingTextInputFormatter(12),
                  ],
                  validator: (value) {
                    return Validators.validatePhoneNumber(
                      _phoneNumberController.text.trim(),
                    );
                  },
                ),
                100.verticalSpace,
                AppButton(
                  name: context.loc.sendCode,
                  isActive: _isValid,
                  onPressed: () {
                    context.push(
                      Routes.verifyAccount,
                      extra:
                          "+998${_phoneNumberController.text.trim().replaceAll(" ", "")}",
                    );
                    if (_formKey.currentState?.validate() ?? false) {}
                  },
                ),
              ],
            ).paddingSymmetric(horizontal: 24),
          ),
        ),
      ),
    );
  }
}
