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/core/utils/secure/secure_storage_service.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/login_bloc.dart';
import '../bloc/login_event.dart';
import '../bloc/login_state.dart';

class BottomSection extends StatelessWidget {
  final String fromScreen;
  const BottomSection({
    super.key,
    required this.fromScreen,
  });

  @override
  Widget build(BuildContext context) {
    SecureStorageService secureStorageService = SecureStorageService();
    final radioBloc = context.read<RadioBloc>();
    return Column(
      children: [
        const Gap(12),
        Align(
          alignment: Alignment.topRight,
          child: ButtonWidget().textBtn(
              function: () {
                goRouter
                    .pushNamed(RouteName.forgotPasswordPhoneVerificationScreen);
              },
              text: TextWidget().text15W400(AppText.forgotPassword,
                  clr: AppColor.forgotPassButtonColor,
                  textDecoration: TextDecoration.underline)),
        ),
        const Gap(20),
        BlocConsumer<LoginBloc, LoginState>(
          listener: (context, state) async {
            if (state is LoginLoading) {
              Loader.loader(context);
            } else if (state is LoginSuccess) {
              goRouter.goNamed('mainScreen');
              successToastMessage(context, "login successful !");
              goRouter.pop();
              radioBloc.resetSelection();
              await secureStorageService.write('isLoginedIn', "true");
              goRouter.pushNamed(RouteName.pinScreen, pathParameters: {
                "fromScreen":
                    fromScreen == "forgot-pin" ? "forgot-pin" : "login",
              }); //Push Or GO Need to Check
            } else if (state is LoginFailure) {
              goRouter.pop();
              errorToastMessage(
                context,
                state.error,
              );
            }
          },
          builder: (context, state) {
            bool isButtonEnabled = false;
            if (state is LoginFieldsState) {
              isButtonEnabled = state.areFieldsFilled;
            } else if (state is LoginSuccess || state is LoginFailure) {
              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<LoginBloc>().add(
                            LoginSubmitted(
                                context
                                    .read<LoginBloc>()
                                    .phoneNumberTextField
                                    .text,
                                context
                                    .read<LoginBloc>()
                                    .passwordTextField
                                    .text,
                                ""),
                          )
                      : null;
                },
                text: AppText.loginText,
                clr: isButtonEnabled
                    ? AppColor.primaryColor2
                    : AppColor.inactiveBtnColor,
              ),
            );
          },
        ),
        const Gap(10),
        ButtonWidget().textBorderBtn(
          function: () {
            radioBloc.resetSelection();
            fromScreen == "forgot-pin"
                ? goRouter.pop()
                : goRouter
                    .pushNamed(RouteName.registerStepScreen, pathParameters: {
                    "fromScreentype": "login",
                  });
          },
          clr: AppColor.plainWhite,
          borderClr: AppColor.txtBorderColor,
          text: fromScreen == "forgot-pin"
              ? AppText.backText
              : AppText.signUpText,
        ),
        const Gap(15),
      ],
    );
  }
}
