import 'package:flutter/material.dart';

class CustomTextField extends StatefulWidget {
  final bool? showPassword;
  final TextInputType keyboardType;
  final TextInputAction? textInputAction;
  final ValueChanged<String>? onSubmitted;
  final String hint;
  final String label;
  final int? lines;
  final ValueChanged<String>? onChanged;
  final int? length;
  final TextEditingController? controller;

  const CustomTextField({
    super.key,
    this.showPassword,
    this.keyboardType = TextInputType.text,
    this.textInputAction,
    this.onSubmitted,
    this.hint = '',
    this.label = '',
    this.lines,
    this.length,
    this.onChanged,
    this.controller,
  });

  @override
  State<CustomTextField> createState() => CustomTextFieldState();
}

class CustomTextFieldState extends State<CustomTextField> {
  late bool _showPassword;
  @override
  void initState() {
    super.initState();
    _showPassword = widget.showPassword ?? true;
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      spacing: 8,
      children: [
        if (widget.label.isNotEmpty)
          Align(
            alignment: Alignment.bottomLeft,
            child: Text(
              widget.label,
              style: const TextStyle(fontSize: 14, letterSpacing: 0),
            ),
          ),
        TextFormField(
          onFieldSubmitted: widget.onSubmitted,
          textInputAction: widget.textInputAction,
          controller: widget.controller,
          keyboardType: widget.keyboardType,
          obscureText: !_showPassword,
          enableSuggestions: _showPassword,
          autocorrect: !_showPassword,
          style: const TextStyle(color: Color(0xFF4B4B4B)),
          decoration: InputDecoration(
            counterText: "",
            hintText: widget.hint,
            border: const OutlineInputBorder(
              borderRadius: BorderRadius.all(Radius.circular(12)),
              borderSide: BorderSide(color: Color(0xFF4B4B4B)),
              gapPadding: 16,
            ),
            suffixIcon: widget.showPassword != null
                ? IconButton(
                    icon: Icon(
                      _showPassword ? Icons.visibility_off : Icons.visibility,
                    ),
                    onPressed: () {
                      setState(() {
                        _showPassword = !_showPassword;
                      });
                    },
                  )
                : null,
          ),
          minLines: widget.lines ?? 1,
          maxLines: widget.lines ?? 1,
          maxLength: widget.length,
          onChanged: widget.onChanged,
        ),
      ],
    );
  }
}
