// lib/ui/common/flux_text_field.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flux/core/theme/theme.dart';

class FluxTextField extends StatefulWidget {
  final String label;
  final String? labelText;
  final IconData? icon;
  final bool isPassword;
  final bool autoFocus;
  final TextEditingController? controller;
  final TextInputType? keyboardType; // Aggiunto per flessibilità
  final int? minLines;
  final int? maxLines;
  final Function(String)? onSubmitted;
  final Function(String)? onChanged;
  final int? maxLength;
  final String? Function(String?)? validator;
  final List<TextInputFormatter>? inputFormatters;
  final TextCapitalization? textCapitalization;
  final bool? autocorrect;
  final bool? enabled;
  final Iterable<String>? autofillHints;

  const FluxTextField({
    super.key, // Usiamo super.key per Flutter moderno
    required this.label,
    this.labelText,
    this.icon,
    this.isPassword = false,
    this.autoFocus = false,
    this.controller,
    this.keyboardType,
    this.minLines,
    this.maxLines = 1,
    this.onSubmitted,
    this.onChanged,
    this.maxLength,
    this.validator,
    this.inputFormatters,
    this.textCapitalization,
    this.autocorrect,
    this.enabled = true,
    this.autofillHints,
  });

  @override
  State<FluxTextField> createState() => _FluxTextFieldState();
}

class _FluxTextFieldState extends State<FluxTextField> {
  late bool _obscureText;

  @override
  void initState() {
    super.initState();
    _obscureText = widget.isPassword;
  }

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: widget.controller,
      validator: widget.validator,
      obscureText: _obscureText,

      enableSuggestions: !widget.isPassword,
      autocorrect: widget.isPassword ? false : widget.autocorrect ?? true,
      keyboardType: widget.keyboardType,
      autofocus: widget.autoFocus,
      minLines: widget.minLines,
      // Se minLines è impostato, maxLines deve essere almeno uguale o null (espandibile)
      maxLines: widget.minLines != null ? null : widget.maxLines,
      style: TextStyle(color: context.primaryText),
      decoration: InputDecoration(
        prefixIcon: widget.icon != null
            ? Icon(widget.icon, color: context.accent.withValues(alpha: 0.6))
            : null,

        labelText: widget.labelText ?? widget.label,
        labelStyle: TextStyle(color: context.secondaryText, fontSize: 14),
        filled: true,
        fillColor: context.surface.withValues(alpha: 0.5),
        enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(12),
          borderSide: BorderSide(
            color: context.secondaryText.withValues(alpha: 0.1),
          ),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(12),
          borderSide: BorderSide(color: context.accent, width: 1.5),
        ),
        contentPadding: const EdgeInsets.symmetric(
          horizontal: 16,
          vertical: 16,
        ),

        suffixIcon: widget.isPassword
            ? IconButton(
                icon: Icon(
                  // Cambiamo icona in base allo stato
                  _obscureText ? Icons.visibility_off : Icons.visibility,
                  color: Colors.grey,
                ),
                onPressed: () {
                  // Quando l'utente clicca, invertiamo lo stato e ridisegniamo
                  setState(() {
                    _obscureText = !_obscureText;
                  });
                },
              )
            : null, // Se non è una password, niente icona
      ),
      onFieldSubmitted: widget.onSubmitted,
      onChanged: widget.onChanged,
      maxLength: widget.maxLength,
      inputFormatters: widget.inputFormatters,

      textCapitalization: widget.textCapitalization ?? TextCapitalization.none,
      enabled: widget.enabled,
      autofillHints: widget.autofillHints,
    );
  }
}
