import 'package:flutter/material.dart';

import '../../../api/services/change_password_service.dart';
import '../../../di/injection.dart';
import '../../../l10n/app_localizations.dart';

class ChangePasswordOTPScreen extends StatefulWidget {
  final String currentPassword;
  final String newPassword;
  final String confirmPassword;
  final String mobileNumber;

  // ignore: use_key_in_widget_constructors
  const ChangePasswordOTPScreen({
    required this.currentPassword,
    required this.newPassword,
    required this.confirmPassword,
    required this.mobileNumber,
  });

  @override
  State<ChangePasswordOTPScreen> createState() =>
      _ChangePasswordOTPScreenState();
}

class _ChangePasswordOTPScreenState extends State<ChangePasswordOTPScreen> {
  bool _isLoading = true;
  final TextEditingController otpController = TextEditingController();

  @override
  void initState() {
    super.initState();
    // Simulate OTP sending delay
    Future.delayed(const Duration(seconds: 2), () {
      setState(() {
        _isLoading = false;
      });
    });
  }

  final changePasswordService = getIt<ChangePasswordService>();
  Future<void> _validateOTP() async {
    try {
      await changePasswordService.validateOtp(
        otp: otpController.text,
        mobileNumber: widget.mobileNumber,
      );

      // If OTP is valid, then change the password
      await changePasswordService.validateChangePwd(
        OldLPsw: widget.currentPassword,
        newLPsw: widget.newPassword,
        confirmLPsw: widget.confirmPassword,
      );

      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text(AppLocalizations.of(context).pwdchangeSuccess)),
      );

      // Navigate back to profile or login
      Navigator.of(context).popUntil((route) => route.isFirst);
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text(AppLocalizations.of(context).invalidOtp)),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(AppLocalizations.of(context).otpVerification)),
      body: Stack(
        children: [
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: _isLoading
                ? const Center(child: CircularProgressIndicator())
                : Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Text(
                        AppLocalizations.of(context).otpSent,
                        textAlign: TextAlign.center,
                        style: const TextStyle(fontSize: 16),
                      ),
                      const SizedBox(height: 24),
                      TextFormField(
                        controller: otpController,
                        keyboardType: TextInputType.number,
                        decoration: InputDecoration(
                          labelText: AppLocalizations.of(context).enterOTP,
                          border: const OutlineInputBorder(),
                        ),
                      ),
                      const SizedBox(height: 24),
                      SizedBox(
                        width: double.infinity,
                        child: ElevatedButton(
                          onPressed: _validateOTP,
                          style: ElevatedButton.styleFrom(
                            padding: const EdgeInsets.symmetric(vertical: 16),
                          ),
                          child: Text(AppLocalizations.of(context).validateOTP),
                        ),
                      ),
                    ],
                  ),
          ),
          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
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
