import 'package:auto_route/annotations.dart';
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/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/enter_new_pass_form.dart';
import 'package:krow/features/sign_in/presentation/reset_pass_screen/widgets/reset_success_form.dart';

@RoutePage()
class EnterNewPassScreen extends StatefulWidget {
  final String? code;

  const EnterNewPassScreen({super.key, @QueryParam('oobCode') this.code});

  @override
  State<EnterNewPassScreen> createState() => _EnterNewPassScreenState();
}

class _EnterNewPassScreenState extends State<EnterNewPassScreen> {
  @override
  void initState() {
    super.initState();
    context
        .read<SignInBloc>()
        .add(SignInEventOnResetVerified(widget.code ?? ''));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KwAppBar(),
      body: BlocBuilder<SignInBloc, SignInState>(
        builder: (context, state) {
          var form = _getForm(state);

          return ScrollLayoutHelper(
            upperWidget: form,
            lowerWidget: Column(
              children: [
                KwButton.primary(
                  label: _buttonText(state),
                  onPressed: () {
                    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.password:
        return 'Save New Password';
      case ResetPassStep.success:
        return 'Go to Login';
      default:
        return 'Next';
    }
  }

  _getForm(SignInState state) {
    if (state.resetPassStep == ResetPassStep.success) {
      return const ResetSuccessForm();
    } else {
      return EnterNewPassForm(obscurePassword: state.obscurePassword);
    }
  }
}
