import 'package:cheminova/provider/forgot_password_provider.dart'; // Import the provider for handling forgot password logic
import 'package:cheminova/screens/change_password_screen.dart'; // Import screen for changing password
import 'package:cheminova/screens/login_screen.dart'; // Import the login screen
import 'package:cheminova/screens/password_change_screen.dart'; // Import screen for password change
import 'package:cheminova/screens/verify_code_screen.dart'; // Import screen for verifying code
import 'package:cheminova/widgets/common_background.dart'; // Import custom background widget
import 'package:cheminova/widgets/common_elevated_button.dart'; // Import custom elevated button widget
import 'package:cheminova/widgets/common_text_form_field.dart'; // Import custom text form field widget
import 'package:flutter/material.dart'; // Import Flutter material package
import 'package:provider/provider.dart'; // Import provider package for state management

// Create a StatefulWidget for the Forgot Password screen
class ForgotPasswordScreen extends StatefulWidget {
  const ForgotPasswordScreen({super.key});

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

// Define the state class for the ForgotPasswordScreen
class _ForgotPasswordScreenState extends State<ForgotPasswordScreen> {
  final _formKey = GlobalKey<FormState>(); // Key to identify the form
  late ForgotPasswordProvider forgotPasswordProvider; // Provider for managing forgot password state

  @override
  void initState() {
    super.initState();
    forgotPasswordProvider = ForgotPasswordProvider(); // Initialize the provider
  }

  @override
  Widget build(BuildContext context) {
    // Use ChangeNotifierProvider to provide ForgotPasswordProvider to the widget tree
    return ChangeNotifierProvider<ForgotPasswordProvider>(
      create: (_) => forgotPasswordProvider,
      builder: (context, child) {
        return CommonBackground(
          isFullWidth: false,
          child: Scaffold(
            backgroundColor: Colors.transparent,
            appBar: AppBar(
              leading: InkWell(
                onTap: () {
                  Navigator.pop(context); // Navigate back when the back button is pressed
                },
                child: Image.asset('assets/Back_attendance.png'),
              ),
              backgroundColor: Colors.transparent,
            ),
            body: Center(
              child: SingleChildScrollView(
                child: Container(
                  padding: const EdgeInsets.all(20.0).copyWith(top: 30, bottom: 30),
                  margin: const EdgeInsets.symmetric(horizontal: 30.0),
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.white),
                    color: const Color(0xffB4D1E5).withOpacity(0.9),
                    borderRadius: BorderRadius.circular(26.0),
                  ),
                  child: Form(
                    key: _formKey, // Assign the form key
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: <Widget>[
                        // Logo displayed at the top
                        Align(
                          alignment: Alignment.topLeft,
                          child: Image.asset(
                            'assets/lock_logo2.png',
                            height: 50.0, // Set height for the logo
                            width: 50.0,  // Set width for the logo
                          ),
                        ),
                        const Text(
                          'Forgot Password',
                          style: TextStyle(
                            fontSize: 30,
                            color: Colors.black,
                            fontWeight: FontWeight.w500,
                            fontFamily: 'Anek',
                          ),
                        ),
                        const Text(
                          'Enter Registered Email ID to generate new password',
                          style: TextStyle(
                            fontSize: 14,
                            color: Colors.black,
                            fontWeight: FontWeight.w300,
                            fontFamily: 'Roboto',
                          ),
                        ),
                        const SizedBox(height: 20),
                        // Text field for entering email
                        Consumer<ForgotPasswordProvider>(
                          builder: (context, value, child) => CommonTextFormField(
                            controller: value.emailController, // Controller for the email input
                            title: ' Enter Your Email ID',
                          ),
                        ),
                        const SizedBox(height: 20),
                        // Button to navigate back to the login screen
                        Align(
                          alignment: Alignment.center,
                          child: TextButton(
                            onPressed: () {
                              Navigator.pop(context); // Navigate back to login
                            },
                            child: const Text(
                              'Back to Login',
                              style: TextStyle(
                                fontSize: 20,
                                color: Colors.black,
                                fontWeight: FontWeight.w400,
                                fontFamily: 'Roboto',
                              ),
                            ),
                          ),
                        ),
                        const SizedBox(height: 15),
                        // Button to send the password reset request
                        Align(
                          alignment: Alignment.center,
                          child: Consumer<ForgotPasswordProvider>(
                            builder: (context, value, child) => CommonElevatedButton(
                              backgroundColor: const Color(0xff004791), // Button background color
                              borderRadius: 30,
                              width: double.infinity,
                              height: kToolbarHeight - 10,
                              text: 'SEND', // Button text
                              onPressed: value.isLoading // Disable button if loading
                                  ? null
                                  : () async {
                                if (_formKey.currentState!.validate()) { // Validate the form
                                  // Call the forgot password method and handle the result
                                  value.forgotPassword().then((result) {
                                    var (status, message) = result; // Destructure the result
                                    ScaffoldMessenger.of(context)
                                        .showSnackBar(SnackBar(
                                        content: Text(message))); // Show snackbar with message
                                    if (status) { // Navigate to login page if successful
                                      Navigator.pushReplacement(
                                          context,
                                          MaterialPageRoute(
                                              builder: (context) => const LoginPage()));
                                    }
                                  });
                                }
                              },
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
