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/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/screens/signin/signin_screen.dart';
import 'package:english_learning/features/settings/modules/change_password/widgets/change_password_dialog.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

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

  @override
  State<ChangePasswordScreen> createState() => _ChangePasswordScreenState();
}

class _ChangePasswordScreenState extends State<ChangePasswordScreen> {
  final TextEditingController _oldPasswordController = TextEditingController();
  final TextEditingController _newPasswordController = TextEditingController();
  final TextEditingController _confirmPasswordController =
      TextEditingController();

  @override
  void dispose() {
    _oldPasswordController.dispose();
    _newPasswordController.dispose();
    _confirmPasswordController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.whiteColor,
      appBar: AppBar(
        elevation: 0,
        iconTheme: const IconThemeData(color: AppColors.whiteColor),
        centerTitle: true,
        title: Text(
          'Change Password',
          style: AppTextStyles.whiteTextStyle.copyWith(
            fontSize: 16,
            fontWeight: FontWeight.bold,
          ),
        ),
        flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: AppColors.gradientTheme,
          ),
        ),
      ),
      body: SingleChildScrollView(
          physics: const NeverScrollableScrollPhysics(),
          child: Consumer<ValidatorProvider>(
            builder: (context, validatorProvider, child) {
              return Padding(
                padding: const EdgeInsets.symmetric(
                  horizontal: 16.0,
                  vertical: 22.0,
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    CustomFieldWidget(
                      isRequired: true,
                      labelText: 'Old Password',
                      hintText: 'Enter your old password',
                      textInputAction: TextInputAction.next,
                      fieldName: 'Old Password',
                      obscureText: true,
                      controller: _oldPasswordController,
                    ),
                    const SizedBox(height: 12),
                    CustomFieldWidget(
                      isRequired: true,
                      labelText: 'New Password',
                      hintText: 'Enter your new password',
                      textInputAction: TextInputAction.next,
                      fieldName: 'new password',
                      obscureText: true,
                      controller: _newPasswordController,
                    ),
                    const SizedBox(height: 12),
                    CustomFieldWidget(
                      isRequired: true,
                      labelText: 'Confirm New Password',
                      hintText: 'Retype your new password',
                      textInputAction: TextInputAction.next,
                      fieldName: 'confirm new password',
                      obscureText: true,
                      controller: _confirmPasswordController,
                    ),
                    const SizedBox(height: 24),
                    GlobalButton(
                      text: 'Update Now',
                      onPressed: () async {
                        final userProvider =
                            Provider.of<UserProvider>(context, listen: false);
                        bool success = await userProvider.updatePassword(
                          oldPassword: _oldPasswordController.text,
                          newPassword: _newPasswordController.text,
                          confirmPassword: _confirmPasswordController.text,
                        );

                        if (success) {
                          showDialog(
                            context: context,
                            barrierDismissible: false,
                            builder: (BuildContext context) {
                              return ChangePasswordDialog(
                                onSubmit: () {
                                  Navigator.pushAndRemoveUntil(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => SigninScreen()),
                                    (route) => false,
                                  );
                                },
                              );
                            },
                          );
                        } else {
                          CustomSnackBar.show(
                            context,
                            message:
                                'Failed to update password. Please try again.',
                            isError: true,
                          );
                        }
                      },
                    )
                  ],
                ),
              );
            },
          )),
    );
  }
}
