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 '../../../../core/routes/routes.dart';
import '../../../../core/styles/app_color.dart';
import '../../../../core/styles/app_text.dart';
import '../../../../shared/components/bloc/checkbox/checkbox_bloc.dart';
import '../../../../shared/components/bloc/checkbox/checkbox_event.dart';
import '../../../../shared/components/bloc/checkbox/checkbox_state.dart';
import '../../../../shared/components/button_widget.dart';
import '../../../../shared/components/loader.dart';
import '../../../../shared/components/toast_message.dart';
import '../bloc/delete_account_bloc.dart';
import '../bloc/delete_account_event.dart';
import '../bloc/delete_account_state.dart';
import 'delete_account_dialog.dart';

Widget bottomSection(BuildContext context) {
  final deleteAccountBloc = context.read<DeleteAccountBloc>();

  return Column(
    mainAxisSize: MainAxisSize.min,
    children: [
      Container(
        margin: const EdgeInsets.symmetric(
          horizontal: 16,
          vertical: 10,
        ),
        width: 1.sw,
        height: 56.h,
        child: ButtonWidget().elevatedBtn(
          txtClr: AppColor.plainWhite,
          function: () {
            goRouter.pop();
          },
          text: AppText.noIWantToStay,
          clr: AppColor.primaryColor2,
        ),
      ),
      BlocConsumer<DeleteAccountBloc, DeleteAccountState>(
        listener: (context, state) {
          if (state is DeleteAccountLoading) {
            Loader.loader(context);
          } else if (state is DeleteAccountSuccess) {
            goRouter.pop();
            context.read<CheckboxBloc>().add(ToggleCheckbox());
            deleteAccountBloc.descriptionTextField.clear();
            showDialog(
              context: context,
              builder: (context) {
                return const AccountDeletionDialog();
              },
            );
          } else if (state is DeleteAccountFailure) {
            goRouter.pop();
            errorToastMessage(
              context,
              state.error,
            );
          }
        },
        builder: (context, state) {
          return Container(
            width: 1.sw,
            height: 56.h,
            child: ButtonWidget().textBorderBtn(
              borderClr: AppColor.txtBorderColor,
              txtClr: AppColor.plainBlack,
              function: () {
                if (deleteAccountBloc.formKey.currentState!.validate()) {
                  context.read<CheckboxBloc>().add(ValidateCheckbox());
                  if (context.read<CheckboxBloc>().state is CheckboxChecked) {
                    context.read<DeleteAccountBloc>().add(
                          DeleteAccountSubmitted(
                            context
                                .read<DeleteAccountBloc>()
                                .descriptionTextField
                                .text,
                          ),
                        );
                  }
                }
              },
              text: AppText.deleteAccountText,
              clr: AppColor.plainWhite,
            ),
          );
        },
      ),
      const Gap(20),
    ],
  );
}
