import 'package:auto_route/auto_route.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/application/routing/routes.gr.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/scroll_layout_helper.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_app_bar.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_button.dart';
import 'package:krow/features/sign_in/domain/bloc/sign_in_bloc.dart';
import 'package:krow/features/sign_in/presentation/reset_pass_screen/widgets/reset_email_form.dart';
import 'package:krow/features/sign_in/presentation/reset_pass_screen/widgets/waiting_email_link_form.dart';

@RoutePage()
class ResetPassScreen extends StatelessWidget {
  const ResetPassScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KwAppBar(),
      body: BlocConsumer<SignInBloc, SignInState>(
        listener: (context, state) {
          if (state.resetPassStep == ResetPassStep.password) {
            context.router.replace(EnterNewPassRoute());
          }
        },
        builder: (context, state) {
          var form = _getForm(state);
          return ScrollLayoutHelper(
            upperWidget: form,
            lowerWidget: Column(
              children: [
                KwButton.primary(
                  disabled: state.resetPassStep != ResetPassStep.email,
                  label: _buttonText(state),
                  onPressed: () {
                    if (state.resetPassStep == ResetPassStep.email) {
                      context.read<SignInBloc>().add(ResetPassNextStepEvent());
                    }
                  },
                ),
                const Gap(36),
                RichText(
                    text: TextSpan(
                  text: 'Remember your password? ',
                  style: AppTextStyles.bodyMediumReg
                      .copyWith(color: AppColors.blackGray),
                  children: [
                    TextSpan(
                      recognizer: TapGestureRecognizer()
                        ..onTap = () {
                          Navigator.maybePop(context);
                        },
                      text: 'Log In',
                      style: AppTextStyles.bodyMediumSmb,
                    ),
                  ],
                )),
                const Gap(36),
              ],
            ),
          );
        },
      ),
    );
  }

  String _buttonText(SignInState state) {
    switch (state.resetPassStep) {
      case ResetPassStep.email:
        return 'Send Reset Link';
      case ResetPassStep.link:
        return 'Continue';
      default:
        return 'Next';
    }
  }

  _getForm(SignInState state) {
    switch (state.resetPassStep) {
      case ResetPassStep.email:
        return const ResetEmailForm();
      case ResetPassStep.link:
        return WaitingEmailLinkForm(email: state.email);
      default:
        return const SizedBox.shrink();
    }
  }
}
