// ignore_for_file: use_key_in_widget_constructors

import 'package:flutter/material.dart';
import '../../../api/services/change_password_service.dart';
import '../../../di/injection.dart';
import '../../../l10n/app_localizations.dart';
import 'change_password_otp_screen.dart';

class ChangePasswordScreen extends StatefulWidget {
  final String mobileNumber;
  const ChangePasswordScreen({super.key, required this.mobileNumber});

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

class _ChangePasswordScreenState extends State<ChangePasswordScreen> {
  final _formKey = GlobalKey<FormState>();
  final currentPasswordController = TextEditingController();
  final newPasswordController = TextEditingController();
  final confirmPasswordController = TextEditingController();

  bool _showCurrentPassword = false;
  bool _showNewPassword = false;
  bool _showConfirmPassword = false;

  final passwordRegex =
      RegExp(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$');

  String? validateCurrentPassword(String? value) {
    if (value == null || value.isEmpty) {
      return AppLocalizations.of(context).currentpwdrqd;
    }
    return null;
  }

  String? validateNewPassword(String? value) {
    if (value == null || value.isEmpty) {
      return AppLocalizations.of(context).newpwdrqd;
    }
    if (!passwordRegex.hasMatch(value)) {
      return AppLocalizations.of(context).pwdFormat;
    }
    if (value == currentPasswordController.text) {
      return AppLocalizations.of(context).newoldpwddiff;
    }
    return null;
  }

  String? validateConfirmPassword(String? value) {
    if (value == null || value.isEmpty) {
      return AppLocalizations.of(context).confirmpwdrqd;
    }
    if (value != newPasswordController.text) {
      return AppLocalizations.of(context).pwdmismatch;
    }
    return null;
  }

  final ChangePasswordService _changePasswordService =
      getIt<ChangePasswordService>();
  void _proceed() async {
    if (_formKey.currentState!.validate()) {
      try {
        await _changePasswordService.getOtp(mobileNumber: widget.mobileNumber);

        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => ChangePasswordOTPScreen(
              currentPassword: currentPasswordController.text,
              newPassword: newPasswordController.text,
              confirmPassword: confirmPasswordController.text,
              mobileNumber: widget.mobileNumber,
            ),
          ),
        );
      } catch (e) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
              content:
                  Text('${AppLocalizations.of(context).failedtosentOTP}: $e')),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:
          AppBar(title: Text(AppLocalizations.of(context).changeLoginPassword)),
      body: Stack(
        children: [
          Padding(
            padding: const EdgeInsets.all(16),
            child: Form(
              key: _formKey,
              child: Column(
                children: [
                  TextFormField(
                    controller: currentPasswordController,
                    obscureText: !_showCurrentPassword,
                    decoration: InputDecoration(
                      labelText: AppLocalizations.of(context).currentpwd,
                      suffixIcon: IconButton(
                        icon: Icon(_showCurrentPassword
                            ? Icons.visibility
                            : Icons.visibility_off),
                        onPressed: () => setState(
                            () => _showCurrentPassword = !_showCurrentPassword),
                      ),
                    ),
                    validator: validateCurrentPassword,
                  ),
                  const SizedBox(height: 16),
                  TextFormField(
                    controller: newPasswordController,
                    obscureText: !_showNewPassword,
                    decoration: InputDecoration(
                      labelText: AppLocalizations.of(context).newpwd,
                      suffixIcon: IconButton(
                        icon: Icon(_showNewPassword
                            ? Icons.visibility
                            : Icons.visibility_off),
                        onPressed: () => setState(
                            () => _showNewPassword = !_showNewPassword),
                      ),
                    ),
                    validator: validateNewPassword,
                  ),
                  const SizedBox(height: 16),
                  TextFormField(
                    controller: confirmPasswordController,
                    obscureText: !_showConfirmPassword,
                    decoration: InputDecoration(
                      labelText: AppLocalizations.of(context).confirmpwd,
                      suffixIcon: IconButton(
                        icon: Icon(_showConfirmPassword
                            ? Icons.visibility
                            : Icons.visibility_off),
                        onPressed: () => setState(
                            () => _showConfirmPassword = !_showConfirmPassword),
                      ),
                    ),
                    validator: validateConfirmPassword,
                  ),
                  const SizedBox(height: 24),
                  ElevatedButton(
                    onPressed: _proceed,
                    child: Text(AppLocalizations.of(context).proceed),
                  ),
                ],
              ),
            ),
          ),
          IgnorePointer(
            child: Center(
              child: Opacity(
                opacity: 0.07, // Reduced opacity
                child: ClipOval(
                  child: Image.asset(
                    'assets/images/logo.png',
                    width: 200, // Adjust size as needed
                    height: 200, // Adjust size as needed
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
