import 'dart:developer';

import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gap/gap.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:tanami_app/core/styles/app_images.dart';
import 'package:tanami_app/shared/components/checkbox_widget.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/bloc/checkbox/checkbox_bloc.dart';
import '../../../../shared/components/bloc/checkbox/checkbox_state.dart';
import '../../../../shared/components/button_widget.dart';
import '../../../../shared/components/text_widget.dart';
import '../bloc/register_user_bloc.dart';
import '../bloc/register_user_event.dart';
import '../bloc/register_user_state.dart';

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

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        const Gap(40),
        Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            const CheckBoxWidget(),
            Expanded(
              child: RichText(
                text: TextSpan(
                    text: AppText.iAgreeToThe,
                    style: GoogleFonts.dmSans(
                      color: AppColor.charcoalColor,
                      fontSize: 14,
                      fontWeight: FontWeight.w500,
                    ),
                    children: [
                      TextSpan(
                        style: GoogleFonts.dmSans(
                          color: AppColor.selectableTextColor,
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                          decoration: TextDecoration.underline,
                        ),
                        text: AppText.termsAndCondition,
                        recognizer: TapGestureRecognizer()
                          ..onTap = () => log('Tap Here onTap'),
                      ),
                      const TextSpan(
                        text: AppText.andThe,
                        style: TextStyle(
                          color: AppColor.charcoalColor,
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      TextSpan(
                        text: AppText.privacyPolicy,
                        recognizer: TapGestureRecognizer()
                          ..onTap = () => log('Tap Here onTap'),
                        style: const TextStyle(
                          color: AppColor.selectableTextColor,
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                          decoration: TextDecoration.underline,
                        ),
                      ),
                    ]),
              ),
            ),
          ],
        ),
        const Gap(10),
        Image.asset(
          AppImages.stage2Image,
          width: 75,
          height: 12,
        ),
        const Gap(36),
        BlocConsumer<RegisterUserBloc, RegisterUserState>(
          listener: (context, state) {
            if (state is RegisterUserLoading) {
              Loader.loader(context);
            } else if (state is RegisterUserSuccess) {
              successToastMessage(context, "successful !");
              goRouter.pop();

              goRouter.goNamed(RouteName.pinScreen, pathParameters: {
                "fromScreen": "register",
              });
            } else if (state is RegisterUserFailure) {
              goRouter.pop();
              errorToastMessage(
                context,
                state.error,
              );
            }
          },
          builder: (context, state) {
            bool isButtonEnabled = false;
            final checkboxState = context.watch<CheckboxBloc>().state;
            if (state is RegisterUserFieldsState &&
                checkboxState is CheckboxChecked) {
              isButtonEnabled = state.areFieldsFilled;
            } else if ((state is RegisterUserSuccess ||
                    state is RegisterUserFailure) &&
                checkboxState is CheckboxChecked) {
              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<RegisterUserBloc>().add(
                            RegisterUserSubmitted(
                              context
                                  .read<RegisterUserBloc>()
                                  .firstNameTextField
                                  .text,
                              context
                                  .read<RegisterUserBloc>()
                                  .passwordTextField
                                  .text,
                              context
                                  .read<RegisterUserBloc>()
                                  .lastNameTextField
                                  .text,
                              context
                                  .read<RegisterUserBloc>()
                                  .repeatPasswordTextField
                                  .text,
                              context
                                  .read<RegisterUserBloc>()
                                  .emailTextField
                                  .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)),
      ],
    );
  }
}
