import 'package:food_delivery_client/core/helpers/string_helpers.dart';
import 'package:food_delivery_client/core/helpers/validator_helpers.dart';
import 'package:pinput/pinput.dart';
import '../../../../../../food_delivery_client.dart';

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

  final String phoneNumber;

  @override
  State<VerifyAccountBody> createState() => _VerifyAccountBodyState();
}

class _VerifyAccountBodyState extends State<VerifyAccountBody> {
  late TextEditingController _otpController;
  final _formKey = GlobalKey<FormState>();
  bool _isValid = false;

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

  @override
  void initState() {
    _otpController = TextEditingController();
    _otpController.addListener(_formValidator);
    super.initState();
  }

  @override
  void dispose() {
    _otpController.removeListener(_formValidator);
    _otpController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final defaultPinTheme = PinTheme(
      height: 56,
      width: 56,
      textStyle: context.appThemeTextStyles.size24SemiBold1,
      decoration: BoxDecoration(
        border: Border.all(
          color: context.appThemeColors.borderColor,
          style: BorderStyle.solid,
          width: 1,
        ),
        color: context.theme.scaffoldBackgroundColor,
        borderRadius: AppUtils.kBorderRadius12,
      ),
    );
    return Form(
      key: _formKey,
      autovalidateMode: AutovalidateMode.onUserInteraction,
      child: WLayout(
        child: Scaffold(
          resizeToAvoidBottomInset: true,
          body: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                30.verticalSpace,
                WBackButton(),
                20.verticalSpace,
                Text(
                  context.loc.verifyYourAccount,
                  style: context.appThemeTextStyles.size24Bold,
                ),
                8.verticalSpace,
                RichText(
                  text: TextSpan(
                    text: context.loc.verifyAccountSubtitle,
                    style: AppTextStyles.size14Regular.copyWith(
                      color: AppColors.cA7AEC1,
                      height: 1.6,
                    ),
                    children: [
                      TextSpan(text: " "),
                      TextSpan(
                        text: StringHelpers.formatUzbekPhoneNumber(
                          widget.phoneNumber,
                        ),
                        style: AppTextStyles.size14Regular.copyWith(
                          color: AppColors.cFF6F00,
                        ),
                      ),
                    ],
                  ),
                ),
                50.verticalSpace,
                Pinput(
                  autofocus: true,
                  enabled: true,
                  length: 5,
                  controller: _otpController,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  keyboardType: TextInputType.number,
                  inputFormatters: [
                    FilteringTextInputFormatter.digitsOnly,
                    LengthLimitingTextInputFormatter(5),
                  ],
                  defaultPinTheme: defaultPinTheme,
                  focusedPinTheme: defaultPinTheme.copyWith(
                    decoration: defaultPinTheme.decoration?.copyWith(
                      border: Border.all(color: AppColors.cFF6F00),
                    ),
                  ),
                  validator: (value) {
                    return Validators.validateOtpFields(
                      _otpController.text.trim(),
                    );
                  },
                ),
                90.verticalSpace,
                AppButton(
                  isActive: _isValid,
                  name: context.loc.verifyAccount,
                  onPressed: () {
                    if (_formKey.currentState?.validate() ?? false) {
                      context.push(
                        Routes.createNewPassword,
                        extra: widget.phoneNumber,
                      );
                    }
                  },
                ),
                20.verticalSpace,
                Align(
                  alignment: AlignmentGeometry.center,
                  child: RichText(
                    text: TextSpan(
                      text: context.loc.didNotReceiveCode,
                      style: context.appThemeTextStyles.size14Regular1,
                      children: [
                        WidgetSpan(
                          baseline: TextBaseline.alphabetic,
                          alignment: PlaceholderAlignment.baseline,
                          child: TextButton(
                            onPressed: () {},
                            child: Text(
                              context.loc.resend,
                              style: AppTextStyles.size14Regular.copyWith(
                                color: AppColors.cFF6F00,
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ).paddingSymmetric(horizontal: 24),
          ),
        ),
      ),
    );
  }
}
