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/auth/domain/usecases/verify_otp_code_login_usecase.dart';
import 'package:food_delivery_client/feature/auth/domain/usecases/verify_phone_login_usecase.dart';
import 'package:food_delivery_client/feature/auth/presentation/blocs/verify_phone_bloc/verify_phone_bloc.dart';
import 'package:food_delivery_client/feature/auth/presentation/widgets/w_auth_background.dart';

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

class VerifyPhoneNumberPage extends StatefulWidget {
  const VerifyPhoneNumberPage({super.key, required this.isRegister});

  final bool isRegister;

  @override
  State<VerifyPhoneNumberPage> createState() => _VerifyPhoneNumberPageState();
}

class _VerifyPhoneNumberPageState extends State<VerifyPhoneNumberPage> {
  late TextEditingController _phoneNumberController;
  final _formKey = GlobalKey<FormState>();

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

  @override
  void dispose() {
    _phoneNumberController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => sl<VerifyPhoneBloc>(),
      child: BlocConsumer<VerifyPhoneBloc, VerifyPhoneState>(
        listener: (context, state) {
          if (state.status.isLoaded()) {
            context.push(
              Routes.verifyOtpCode,
              extra: OtpCodePageParams(
                phoneNumber:
                    "+998${_phoneNumberController.text.trim().replaceAll(" ", "")}",
                isRegister: widget.isRegister,
              ),
            );
          }
        },
        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: [
                        50.verticalSpace,
                        Text(
                          context.loc.enter_phone_number,
                          style: AppTextStyles.size20Medium,
                        ),
                        20.verticalSpace,
                        AppTextFormField(
                          controller: _phoneNumberController,
                          borderRadius: AppUtils.kBorderRadius8,
                          hintText: context.loc.phone_number,
                          keyBoardType: TextInputType.phone,
                          inputFormatters: [
                            FilteringTextInputFormatter.digitsOnly,
                            Formatters.phoneFormatter,
                            LengthLimitingTextInputFormatter(12),
                          ],

                          prefixIcon: Text(
                            "+ 998",
                            style: AppTextStyles.size16Regular,
                          ),
                          validator: (value) {
                            return Validators.validatePhoneNumber(
                              _phoneNumberController.text.trim(),
                            );
                          },
                        ),
                        25.verticalSpace,
                        AppButton(
                          onPressed: () {
                            if (_formKey.currentState?.validate() ?? false) {


                              if (widget.isRegister) {
                                context.read<VerifyPhoneBloc>().add(
                                  VerifyPhoneEvent.verifyPhoneRegister(
                                    VerifyPhoneNumberParams(
                                      phoneNumber:
                                          "+998${_phoneNumberController.text.trim().replaceAll(" ", "")}",
                                    ),
                                  ),
                                );
                              } else {
                                context.read<VerifyPhoneBloc>().add(
                                  VerifyPhoneEvent.verifyPhoneReset(
                                    VerifyPhoneNumberParams(
                                      phoneNumber:
                                          "+998${_phoneNumberController.text.trim().replaceAll(" ", "")}",
                                    ),
                                  ),
                                );
                              }
                            }
                          },
                          name: context.loc.continue_str,
                          borderRadius: 15,
                          backgroundColor: AppColors.c34A853,
                        ),
                        10.verticalSpace,
                        Text(
                          context.loc.consent_message("Felix Eats"),
                          style: AppTextStyles.size12Medium.copyWith(
                            color: AppColors.c888888,
                          ),
                        ),
                      ],
                    ).paddingSymmetric(horizontal: 16),
                  ),
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
