import 'package:english_learning/core/widgets/custom_snackbar.dart';
import 'package:english_learning/features/auth/provider/user_provider.dart';
import 'package:english_learning/features/auth/provider/validator_provider.dart';
import 'package:english_learning/features/auth/screens/signin/signin_screen.dart';
import 'package:english_learning/core/widgets/form_field/custom_field_widget.dart';
import 'package:english_learning/core/widgets/global_button.dart';
import 'package:english_learning/core/utils/styles/theme.dart';
import 'package:english_learning/features/auth/widgets/dialog/forgot_password_verification.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class ForgotPasswordScreen extends StatefulWidget {
  const ForgotPasswordScreen({super.key});

  @override
  State<ForgotPasswordScreen> createState() => _ForgotPasswordScreenState();
}

class _ForgotPasswordScreenState extends State<ForgotPasswordScreen> {
  final TextEditingController _emailController = TextEditingController();
  bool _isLoading = false;
  Future<void> _handleForgotPassword() async {
    setState(() {
      _isLoading = true;
    });

    final userProvider = Provider.of<UserProvider>(context, listen: false);
    bool success = await userProvider.forgotPassword(
      email: _emailController.text,
    );

    setState(() {
      _isLoading = false;
    });

    if (success) {
      showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return ForgotPasswordVerification(
            onSubmit: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => SigninScreen(),
                ),
              );
            },
          );
        },
      );
    } else {
      CustomSnackBar.show(
        context,
        message: 'Email is not registered!',
        isError: true,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    final mediaQuery = MediaQuery.of(context);
    final screenHeight = mediaQuery.size.height;

    return Scaffold(
      backgroundColor: AppColors.whiteColor,
      body: SingleChildScrollView(
        child: Center(
          child: Container(
            margin: const EdgeInsets.symmetric(horizontal: 16),
            child: Consumer<ValidatorProvider>(
                builder: (context, validatorProvider, child) {
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(height: screenHeight * 0.15),
                  Text(
                    'Forgot Password?',
                    style: AppTextStyles.blueTextStyle.copyWith(
                      fontSize: 30,
                      fontWeight: FontWeight.w900,
                    ),
                  ),
                  const SizedBox(height: 4),
                  Text(
                    'Enter your email and we will send you a link to reset your password',
                    style: AppTextStyles.tetriaryTextStyle.copyWith(
                      fontSize: 14,
                      fontWeight: FontWeight.w400,
                    ),
                  ),
                  SizedBox(height: screenHeight * 0.05),
                  const Center(
                    child: Image(
                      image: AssetImage(
                          'lib/features/auth/assets/images/shrug.png'),
                      width: 200,
                      height: 205.37,
                    ),
                  ),
                  SizedBox(height: screenHeight * 0.08),
                  CustomFieldWidget(
                    controller: _emailController,
                    fieldName: 'email',
                    isRequired: true,
                    textInputAction: TextInputAction.next,
                    labelText: 'Email Address',
                    hintText: 'Enter Your Email Address',
                    keyboardType: TextInputType.emailAddress,
                    validator: validatorProvider.emailValidator,
                    onChanged: (value) {
                      validatorProvider.validateField(
                        'email',
                        value,
                        validator: validatorProvider.emailValidator,
                      );
                    },
                    errorText: validatorProvider.getError('email'),
                  ),
                  const SizedBox(height: 24),
                  _isLoading
                      ? const Center(child: CircularProgressIndicator())
                      : GlobalButton(
                          text: 'Submit',
                          onPressed: _handleForgotPassword,
                        ),
                  const SizedBox(height: 8),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            'Remember the password? ',
                            style: AppTextStyles.blackButtonTextStyle.copyWith(
                              fontSize: 14,
                              fontWeight: FontWeight.w400,
                            ),
                          ),
                          GestureDetector(
                            onTap: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => SigninScreen()),
                              );
                            },
                            child: Text(
                              'Login',
                              style: AppTextStyles.blueTextStyle.copyWith(
                                fontSize: 14,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ],
              );
            }),
          ),
        ),
      ),
    );
  }
}
