import 'package:english_learning/features/auth/provider/validator_provider.dart';
import 'package:english_learning/core/utils/styles/theme.dart';
import 'package:bootstrap_icons/bootstrap_icons.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class PasswordFieldWidget extends StatefulWidget {
  final String hintText;
  final String labelText;
  const PasswordFieldWidget({
    super.key,
    required this.hintText,
    required this.labelText,
  });

  @override
  _PasswordFieldWidgetState createState() => _PasswordFieldWidgetState();
}

class _PasswordFieldWidgetState extends State<PasswordFieldWidget> {
  late FocusNode _focusNode;

  @override
  void initState() {
    super.initState();
    _focusNode = FocusNode();

    _focusNode.addListener(() {
      if (!_focusNode.hasFocus) {
        final validatorProvider =
            Provider.of<ValidatorProvider>(context, listen: false);
        validatorProvider.validateField(
            'password', validatorProvider.getError('password'),
            validator: validatorProvider.passwordValidator);
      }
    });
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final validatorProvider = Provider.of<ValidatorProvider>(context);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          widget.labelText,
          style: AppTextStyles.blackTextStyle.copyWith(
            fontSize: 12,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 8),
        TextField(
          focusNode: _focusNode,
          obscureText: validatorProvider.isObscure('password'),
          onChanged: (value) {
            validatorProvider.validateField('password', value,
                validator: validatorProvider.passwordValidator);
          },
          decoration: InputDecoration(
            hintText: widget.hintText,
            hintStyle: AppTextStyles.disableTextStyle.copyWith(
              fontSize: 12,
            ),
            suffixIcon: GestureDetector(
              onTap: () => validatorProvider.toggleVisibility('password'),
              child: AnimatedSwitcher(
                duration: const Duration(milliseconds: 300),
                transitionBuilder: (Widget child, Animation<double> animation) {
                  return ScaleTransition(scale: animation, child: child);
                },
                child: Icon(
                  validatorProvider.isObscure('password')
                      ? BootstrapIcons.eye_slash
                      : BootstrapIcons.eye,
                  color: AppColors.greyColor,
                ),
              ),
            ),
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(8),
              borderSide: const BorderSide(
                width: 1,
                color: AppColors.disableColor,
              ),
            ),
            errorText: validatorProvider.getError('password'),
          ),
        ),
      ],
    );
  }
}
