import 'package:easy_debounce/easy_debounce.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hub/flutter_flow/flutter_flow_theme.dart';
import 'package:hub/shared/utils/limited_text_size.dart';

// ignore: must_be_immutable
class CustomInputUtil extends StatefulWidget {
  final TextEditingController? controller;
  final String? labelText;
  final String? hintText;
  final bool? obscureText;
  final IconData? suffixIcon;
  final bool autoFocus;
  FocusNode? focusNode;
  final TextInputAction textInputAction;
  final TextInputType keyboardType;
  final int maxLength;
  final String? Function(String?)? validator;
  final bool haveMaxLength;
  final void Function(String)? onChanged;

  CustomInputUtil(
      {super.key,
      this.controller,
      required this.labelText,
      required this.hintText,
      required this.suffixIcon,
      this.autoFocus = false,
      required this.focusNode,
      this.onChanged,
      this.textInputAction = TextInputAction.next,
      this.keyboardType = TextInputType.text,
      this.maxLength = 80,
      this.validator,
      this.obscureText,
      required this.haveMaxLength});

  @override
  State<CustomInputUtil> createState() => _CustomInputUtilState();
}

class _CustomInputUtilState extends State<CustomInputUtil> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    double limitedInputTextSize = LimitedFontSizeUtil.getInputFontSize(context);

    return Padding(
      padding: const EdgeInsetsDirectional.fromSTEB(24.0, 0.0, 24.0, 10.0),
      child: Column(
        children: [
          TextFormField(
            controller: widget.controller,
            autovalidateMode: AutovalidateMode.onUserInteraction,
            validator: widget.validator,
            cursorColor: FlutterFlowTheme.of(context).primary,
            autofocus: widget.autoFocus,
            focusNode: widget.focusNode,
            onChanged: (value) {
              EasyDebounce.debounce(
                '${widget.controller}',
                const Duration(milliseconds: 500),
                () => setState(() {}),
              );
              if (widget.onChanged != null) {
                widget.onChanged!(value); // Chamar o callback
              }
            },
            textInputAction: widget.textInputAction,
            obscureText: false,
            decoration: InputDecoration(
              isDense: true,
              labelText: widget.labelText,
              labelStyle: FlutterFlowTheme.of(context).labelMedium.override(
                    fontFamily: FlutterFlowTheme.of(context).labelMediumFamily,
                    color: FlutterFlowTheme.of(context).primaryText,
                    letterSpacing: 0.0,
                    useGoogleFonts: GoogleFonts.asMap().containsKey(
                      FlutterFlowTheme.of(context).labelMediumFamily,
                    ),
                    fontSize: limitedInputTextSize,
                  ),
              hintText: widget.hintText,
              hintStyle: FlutterFlowTheme.of(context).labelMedium.override(
                    fontFamily: FlutterFlowTheme.of(context).labelMediumFamily,
                    color: FlutterFlowTheme.of(context).primaryText,
                    letterSpacing: 0.0,
                    useGoogleFonts: GoogleFonts.asMap().containsKey(
                      FlutterFlowTheme.of(context).labelMediumFamily,
                    ),
                    fontSize: limitedInputTextSize,
                  ),
              helperStyle: TextStyle(
                fontFamily: FlutterFlowTheme.of(context).bodyMediumFamily,
                fontSize: limitedInputTextSize,
              ),
              enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(
                  color: FlutterFlowTheme.of(context).customColor6,
                  width: 0.5,
                ),
                borderRadius: BorderRadius.circular(10.0),
              ),
              focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(
                  color: FlutterFlowTheme.of(context).primary,
                  width: 0.5,
                ),
                borderRadius: BorderRadius.circular(10.0),
              ),
              errorBorder: OutlineInputBorder(
                borderSide: BorderSide(
                  color: FlutterFlowTheme.of(context).error,
                  width: 0.5,
                ),
                borderRadius: BorderRadius.circular(10.0),
              ),
              errorStyle: TextStyle(
                fontFamily: FlutterFlowTheme.of(context).bodyMediumFamily,
                color: FlutterFlowTheme.of(context).error,
                fontSize: limitedInputTextSize,
              ),
              focusedErrorBorder: OutlineInputBorder(
                borderSide: BorderSide(
                  color: FlutterFlowTheme.of(context).error,
                  width: 0.5,
                ),
                borderRadius: BorderRadius.circular(10.0),
              ),
              suffixIcon: Icon(
                widget.suffixIcon,
                color: FlutterFlowTheme.of(context).accent1,
              ),
            ),
            style: FlutterFlowTheme.of(context).bodyMedium.override(
                  fontFamily: FlutterFlowTheme.of(context).bodyMediumFamily,
                  color: FlutterFlowTheme.of(context).primaryText,
                  letterSpacing: 0.0,
                  useGoogleFonts: GoogleFonts.asMap().containsKey(
                    FlutterFlowTheme.of(context).bodyMediumFamily,
                  ),
                  fontSize: limitedInputTextSize,
                ),
            maxLines: null,
            maxLength: widget.haveMaxLength ? widget.maxLength : null,
            keyboardType: widget.keyboardType,
            inputFormatters: [
              LengthLimitingTextInputFormatter(widget.maxLength),
            ],
          ),
        ],
      ),
    );
  }
}
