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/routes.dart';
import '../../../../core/styles/app_color.dart';
import '../../../../core/styles/app_text.dart';
import '../../../../shared/components/button_widget.dart';
import '../bloc/change_password_bloc.dart';
import '../bloc/change_password_event.dart';
import '../bloc/change_password_state.dart';

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

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        BlocConsumer<ChangePasswordBloc, ChangePasswordState>(
          listener: (context, state) {
            if (state is ChangePasswordLoading) {
              Loader.loader(context);
            } else if (state is ChangePasswordSuccess) {
              successToastMessage(context, "Password changed successful !");
              goRouter.pop();
              goRouter.pop();
            } else if (state is ChangePasswordFailure) {
              goRouter.pop();
              errorToastMessage(
                context,
                state.error,
              );
            }
          },
          builder: (context, state) {
            bool isButtonEnabled = false;
            if (state is ChangePasswordFieldsState) {
              isButtonEnabled = state.areFieldsFilled;
            } else if (state is ChangePasswordSuccess ||
                state is ChangePasswordFailure) {
              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<ChangePasswordBloc>().add(
                            ChangePasswordSubmitted(
                              context
                                  .read<ChangePasswordBloc>()
                                  .currentPasswordTextField
                                  .text,
                              context
                                  .read<ChangePasswordBloc>()
                                  .passwordTextField
                                  .text,
                              context
                                  .read<ChangePasswordBloc>()
                                  .repeatPasswordTextField
                                  .text,
                            ),
                          )
                      : null;
                },
                text: AppText.submitText,
                clr: isButtonEnabled
                    ? AppColor.primaryColor2
                    : AppColor.inactiveBtnColor,
              ),
            );
          },
        ),
        const Gap(5),
        ButtonWidget().textBorderBtn(
          clr: Colors.white,
          borderClr: AppColor.txtBorderColor,
          function: () {
            goRouter.pop();
          },
          text: AppText.cancelText,
        ),
        const Gap(10),
      ],
    );
  }
}
