import 'package:auto_route/auto_route.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/gen/assets.gen.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/core/presentation/widgets/ui_kit/kw_input.dart';
import 'package:krow/features/sign_in/domain/bloc/sign_in_bloc.dart';
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart';

@RoutePage()
class SignInScreen extends StatefulWidget {
  const SignInScreen({super.key});

  @override
  State<SignInScreen> createState() => _SignInScreenState();
}

class _SignInScreenState extends State<SignInScreen> {
  final emailController = TextEditingController();
  final passController = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KwAppBar(),
      body: BlocConsumer<SignInBloc, SignInState>(
        listener: (context, state) {
          if (state.authSuccess) {
            context.router.replace(const SplashRoute());
          }
        },
        builder: (context, state) {
          return ModalProgressHUD(
            inAsyncCall: state.inLoading,
            child: ScrollLayoutHelper(
              upperWidget: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Gap(36),
                  const Text(
                    'Welcome Back!',
                    style: AppTextStyles.headingH1,
                  ),
                  const Gap(8),
                  Text(
                    'Log in to find work opportunities that match your skills',
                    style: AppTextStyles.bodyMediumReg
                        .copyWith(color: AppColors.blackGray),
                  ),
                  const Gap(24),
                  KwTextInput(
                      title: 'Email',
                      hintText: 'Enter your email',
                      keyboardType: TextInputType.emailAddress,
                      helperText: state.emailValidationError,
                      showError: state.emailValidationError.isNotEmpty,
                      onChanged: (_) {
                        BlocProvider.of<SignInBloc>(context)
                            .add(SignInEventEmailChanged(emailController.text));
                      },
                      controller: emailController),
                  const Gap(8),
                  KwTextInput(
                      title: 'Password',
                      hintText: 'Enter password here',
                      obscureText: state.obscurePassword,
                      keyboardType: TextInputType.visiblePassword,
                      helperText: state.passValidationError,
                      showError: state.passValidationError.isNotEmpty,
                      maxLines: 1,
                      onChanged: (_) {
                        BlocProvider.of<SignInBloc>(context)
                            .add(SignInEventPassChanged());
                      },
                      suffixIcon: GestureDetector(
                        onTap: () {
                          BlocProvider.of<SignInBloc>(context)
                              .add(SwitchObscurePasswordEvent());
                        },
                        child: Padding(
                          padding: const EdgeInsets.only(right: 16),
                          child: state.obscurePassword
                              ? Assets.images.icons.eyeOff.svg()
                              : Assets.images.icons.eye.svg(),
                        ),
                      ),
                      controller: passController),
                  // GestureDetector(
                  //   onTap: () async {
                  //     await context.router.push(const ResetPassRoute());
                  //     BlocProvider.of<SignInBloc>(context)
                  //         .add(ResetPassBackEvent());
                  //   },
                  //   child: const Padding(
                  //     padding: EdgeInsets.only(left: 16, top: 12),
                  //     child: Text(
                  //       'Forgot password',
                  //       style: AppTextStyles.bodyTinyMed,
                  //     ),
                  //   ),
                  // ),
                  const Gap(24),
                ],
              ),
              lowerWidget: Padding(
                padding: const EdgeInsets.only(bottom: 36),
                child: KwButton.primary(
                  disabled:
                      emailController.text.isEmpty || passController.text.isEmpty,
                  label: 'Continue',
                  onPressed: () {
                    BlocProvider.of<SignInBloc>(context).add(SignInEventSignIn(
                      passController.text,
                    ));
                  },
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
