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/presentation/widgets/w_auth_background.dart';
import '../../../../../../food_delivery_client.dart';
import '../../../blocs/login_bloc/login_bloc.dart';

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

  @override
  State<WLoginBody> createState() => _WLoginBodyState();
}

class _WLoginBodyState extends State<WLoginBody> {
  late final TextEditingController _phoneController;
  late final TextEditingController _passwordController;
  final _formKey = GlobalKey<FormState>();

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

  @override
  void dispose() {
    _phoneController.dispose();
    _passwordController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<LoginBloc, LoginState>(
      listener: (context, state) {
        if (state.status.isLoaded()) {
          context.go(Routes.main);
        }
      },
      builder: (context, state) {
        return Form(
          key: _formKey,
          autovalidateMode: AutovalidateMode.onUserInteraction,
          child: WAuthBackground(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                25.verticalSpace,
                Align(
                  alignment: AlignmentGeometry.center,
                  child: Text(
                    context.loc.login,
                    style: AppTextStyles.size24Bold,
                  ),
                ),
                20.verticalSpace,
                Text(
                  context.loc.phone_number,
                  style: AppTextStyles.size14Regular.copyWith(
                    color: AppColors.c6A6E7F,
                  ),
                ),
                5.verticalSpace,
                AppTextFormField(
                  hintText: context.loc.enter_phone_number,
                  prefixIcon: Text(
                    "+ 998",
                    style: AppTextStyles.size16Regular.copyWith(fontSize: 16),
                  ),
                  borderRadius: AppUtils.kBorderRadius8,
                  controller: _phoneController,
                  keyBoardType: TextInputType.number,

                  inputFormatters: [
                    FilteringTextInputFormatter.digitsOnly,
                    Formatters.phoneFormatter,
                    LengthLimitingTextInputFormatter(12),
                  ],
                  validator: (value) {
                    return Validators.validatePhoneNumber(
                      _phoneController.text.trim(),
                    );
                  },
                ),
                10.verticalSpace,
                Text(
                  context.loc.password,
                  style: AppTextStyles.size14Regular.copyWith(
                    color: AppColors.c6A6E7F,
                  ),
                ),
                5.verticalSpace,
                AppTextFormField(
                  height: 50,
                  obscureText: true,
                  hintText: context.loc.enter_password,
                  keyBoardType: TextInputType.text,
                  borderRadius: AppUtils.kBorderRadius8,
                  controller: _passwordController,
                  validator: (value) {
                    return Validators.validatePassword(
                      _passwordController.text.trim(),
                    );
                  },
                ),
                Align(
                  alignment: AlignmentGeometry.centerRight,
                  child: TextButton(
                    onPressed: () {
                      context.push(Routes.verifyPhoneNumber, extra: false);
                    },
                    child: Text(
                      context.loc.forgot_password,
                      style: AppTextStyles.size14Medium.copyWith(
                        color: AppColors.c34A853,
                      ),
                    ),
                  ),
                ),
                40.verticalSpace,
                AppButton(
                  name: context.loc.continue_str,
                  isLoading: state.status.isLoading(),
                  trailing: SvgPicture.asset(
                    AppIcons.icArrowRightLight,
                  ).paddingOnly(left: 10),
                  onPressed: () {
                    if (_formKey.currentState?.validate() ?? false) {
                      context.go(Routes.main);
                      // context.read<LoginBloc>().add(
                      //   LoginEvent.login(
                      //     LoginParams(
                      //       phoneNumber:
                      //           "+998${_phoneController.text.trim().replaceAll(" ", "")}",
                      //       password: _passwordController.text.trim(),
                      //     ),
                      //   ),
                      // );
                    }
                  },
                  borderRadius: 15,
                  backgroundColor: AppColors.c34A853,
                ),
                20.verticalSpace,
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      context.loc.dont_have_account,
                      style: AppTextStyles.size14Medium,
                    ),
                    TextButton(
                      onPressed: () {
                        context.push(Routes.verifyPhoneNumber, extra: true);
                      },
                      child: Text(
                        context.loc.sign_up,
                        style: AppTextStyles.size15Bold.copyWith(
                          color: AppColors.c34A853,
                        ),
                      ),
                    ),
                  ],
                ),
                20.verticalSpace,
              ],
            ).paddingSymmetric(horizontal: 16),
          ),
        );
      },
    );
  }
}
