part of 'field.dart';

class AppPasswordTextFormField extends StatefulWidget {
  final TextEditingController? controller;
  final String label;
  final Function(String value)? onChanged;
  final TextInputType? keyboardType;
  final TextInputAction? textInputAction;
  final TextCapitalization? textCapitalization;
  final bool showLabel;
  final Widget? prefixIcon;
  final Widget? suffixIcon;
  final bool readOnly;
  final int maxLines;
  final String? Function(String?)? validator;

  const AppPasswordTextFormField({
    super.key,
    this.controller,
    required this.label,
    this.onChanged,
    this.keyboardType,
    this.textInputAction,
    this.textCapitalization,
    this.showLabel = true,
    this.prefixIcon,
    this.suffixIcon,
    this.readOnly = false,
    this.maxLines = 1,
    this.validator,
  });

  @override
  State<AppPasswordTextFormField> createState() =>
      _AppPasswordTextFormFieldState();
}

class _AppPasswordTextFormFieldState extends State<AppPasswordTextFormField> {
  bool isPasswordVisible = true;
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (widget.showLabel) ...[
          Text(
            widget.label,
            style: AppStyle.md.copyWith(fontWeight: FontWeight.w700),
          ),
          const SpaceHeight(12.0),
        ],
        TextFormField(
          controller: widget.controller,
          onChanged: widget.onChanged,
          obscureText: isPasswordVisible,
          keyboardType: widget.keyboardType,
          textInputAction: widget.textInputAction,
          textCapitalization:
              widget.textCapitalization ?? TextCapitalization.none,
          readOnly: widget.readOnly,
          maxLines: widget.maxLines,
          validator: widget.validator,
          decoration: InputDecoration(
            prefixIcon: widget.prefixIcon,
            suffixIcon: isPasswordVisible
                ? IconButton(
                    icon: const Icon(
                      Icons.visibility,
                      color: AppColor.textSecondary,
                      size: 20,
                    ),
                    constraints: const BoxConstraints(),
                    padding: const EdgeInsets.all(8),
                    onPressed: () {
                      setState(() {
                        isPasswordVisible = !isPasswordVisible;
                      });
                    },
                  )
                : IconButton(
                    icon: const Icon(
                      Icons.visibility_off,
                      color: AppColor.textSecondary,
                      size: 20,
                    ),
                    constraints: const BoxConstraints(),
                    padding: const EdgeInsets.all(8),
                    onPressed: () {
                      setState(() {
                        isPasswordVisible = !isPasswordVisible;
                      });
                    },
                  ),
            hintText: widget.label,
          ),
        ),
      ],
    );
  }
}
