import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gap/gap.dart';
import 'package:tanami_app/shared/components/loader.dart';
import 'package:tanami_app/shared/components/toast_message.dart';

import '../../../../core/routes/route_name.dart';
import '../../../../core/routes/routes.dart';
import '../../../../core/styles/app_color.dart';
import '../../../../core/styles/app_text.dart';
import '../../../../shared/components/button_widget.dart';
import '../../../../shared/components/text_widget.dart';
import '../../../countrySelection/presentation/bloc/choose_country_bloc.dart';
import '../bloc/restore_password_phone_verification_bloc.dart';
import '../bloc/restore_password_phone_verification_event.dart';
import '../bloc/restore_password_phone_verification_state.dart';

class RestorePasswordPhoneVerificationBottomSection extends StatelessWidget {
  const RestorePasswordPhoneVerificationBottomSection({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final radioBloc = context.read<RadioBloc>();
    return Column(
      children: [
        BlocConsumer<RestorePasswordPhoneVerificationBloc,
            RestorePasswordPhoneVerificationState>(
          listener: (context, state) {
            if (state is RestorePasswordPhoneVerificationLoading) {
              Loader.loader(context);
            } else if (state is RestorePasswordPhoneVerificationSuccess) {
              successToastMessage(context, "OTP send successful !");
              goRouter.pop();
              radioBloc.resetSelection();
              goRouter.pushNamed(RouteName.otpScreen,
                  pathParameters: {"fromScreen": "forgot-password"});
            } else if (state is RestorePasswordPhoneVerificationFailure) {
              goRouter.pop();
              errorToastMessage(
                context,
                state.error,
              );
            }
          },
          builder: (context, state) {
            bool isButtonEnabled = false;
            if (state is RestorePasswordPhoneVerificationFieldsState) {
              isButtonEnabled = state.areFieldsFilled;
            } else if (state is RestorePasswordPhoneVerificationSuccess ||
                state is RestorePasswordPhoneVerificationFailure) {
              isButtonEnabled = true;
            }
            return Container(
              margin: const EdgeInsets.symmetric(
                horizontal: 16,
              ),
              width: 1.sw,
              height: 56.h,
              child: ButtonWidget().elevatedBtn(
                txtClr: isButtonEnabled
                    ? AppColor.plainWhite
                    : AppColor.inactiveBtnTxtColor,
                function: () {
                  isButtonEnabled
                      ? context
                          .read<RestorePasswordPhoneVerificationBloc>()
                          .add(
                            RestorePasswordPhoneVerificationSubmitted(
                                context
                                    .read<
                                        RestorePasswordPhoneVerificationBloc>()
                                    .phoneNumberTextField
                                    .text,
                                ""),
                          )
                      : null;
                },
                text: AppText.nextText,
                clr: isButtonEnabled
                    ? AppColor.primaryColor2
                    : AppColor.inactiveBtnColor,
              ),
            );
          },
        ),
        const Gap(5),
        ButtonWidget().textBtn(
            function: () {
              goRouter.pop();
            },
            text: TextWidget().text14W700(AppText.backText,
                clr: AppColor.textLabelColor,
                textDecoration: TextDecoration.underline)),
      ],
    );
  }
}
