import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
import 'package:syncrow_app/features/auth/view/widgets/forget_password.dart';
import 'package:syncrow_app/features/shared_widgets/default_button.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/utils/resource_manager/styles_manager.dart';

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

  @override
  Widget build(BuildContext context) {
    var pressed = false;
    return BlocBuilder<AuthCubit, AuthState>(
      builder: (context, state) {
        final formKey = AuthCubit.get(context).loginFormKey;
        return Form(
          key: formKey,
          autovalidateMode: AutovalidateMode.onUserInteraction,
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const BodyMedium(
                  text: "Email",
                  fontColor: Colors.white,
                ),
                TextFormField(
                  autovalidateMode: AutovalidateMode.disabled,
                  textInputAction: TextInputAction.done,
                  keyboardType: TextInputType.text,
                  scrollPadding: EdgeInsets.zero,
                  autocorrect: false,
                  autofillHints: const [AutofillHints.email],
                  controller: AuthCubit.get(context).emailController,
                  validator: (value) {
                    if (state is AuthTokenError && !pressed) {
                      return null;
                    }
                    return AuthCubit.get(context).emailAddressValidator(value);
                  },
                  onTapOutside: (event) {
                    FocusScope.of(context).unfocus();
                  },
                  onChanged: (value) {},
                  decoration: defaultInputDecoration(context, hint: "Example@email.com"),
                ),
                const SizedBox(height: 10),
                const BodyMedium(
                  text: "Password",
                  fontColor: Colors.white,
                ),
                TextFormField(
                  autovalidateMode: AutovalidateMode.disabled,
                  textInputAction: TextInputAction.done,
                  keyboardType: TextInputType.text,
                  scrollPadding: EdgeInsets.zero,
                  autocorrect: false,
                  autofillHints: const [AutofillHints.password],
                  controller: AuthCubit.get(context).passwordController,
                  validator: (value) {
                    if (state is AuthTokenError && !pressed) {
                      return null;
                    }
                    // return AuthCubit.get(context).passwordValidator(value);
                  },
                  onTapOutside: (event) {
                    FocusScope.of(context).unfocus();
                  },
                  obscureText: !AuthCubit.get(context).isPasswordVisible,
                  decoration: defaultInputDecoration(context, hint: "At least 8 characters"),
                ),
                const SizedBox(height: 10),
                // const LoginUserAgreement(),
                const ForgetPassword(),
                const SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Expanded(
                      child: DefaultButton(
                        isDone: state is AuthLoginSuccess,
                        isLoading: state is AuthLoading,
                        customButtonStyle: ButtonStyle(
                          backgroundColor: WidgetStateProperty.all(
                            Colors.black.withOpacity(.25),
                          ),
                          foregroundColor: WidgetStateProperty.all(
                            Colors.white,
                          ),
                        ),
                        child: const Text(
                          'Login',
                        ),
                        onPressed: () {
                          pressed = true;
                          if (formKey.currentState!.validate()) {
                            if ((state is! AuthLoading)) {
                              AuthCubit.get(context).login();
                              FocusScope.of(context).unfocus();
                            }
                          }
                        },
                      ),
                    ),
                  ],
                )
              ],
            ),
          ),
        );
      },
    );
  }
}
