// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../themes/themes.dart';

class AppTextField extends StatelessWidget {
  final Icon? prefixIcon;
  final Widget? suffixIcon;
  final String? labelText;
  final String? initialValue;
  final String? hintText;
  final TextInputType? keyboardType;
  final TextEditingController? controller;
  final Color? fillColor;
  final void Function()? onTap;
  final bool enabled;
  final bool obscureText;
  final bool readOnly;
  final int? maxLength;
  final int? maxLines;
  final String? errorText;
  final double? textfieldHeight;
  final List<String>? autofillHints;
  List<TextInputFormatter>? inputFormatters;
  final TextCapitalization textCapitalization;
  String? etiqueta;
  String? Function(String?)? validator;
  final double? vertical;
  final Function(String v)? onChanged;
  final Function(String v)? onSubmitted;

  AppTextField({
    super.key,
    this.etiqueta,
    this.labelText,
    this.prefixIcon,
    this.keyboardType,
    this.onTap,
    this.controller,
    this.hintText,
    this.suffixIcon,
    this.inputFormatters,
    this.enabled = true,
    this.readOnly = false,
    this.obscureText = false,
    this.fillColor = Colors.white,
    this.errorText,
    this.textfieldHeight,
    this.textCapitalization = TextCapitalization.none,
    this.initialValue,
    this.maxLength,
    this.maxLines = 1,
    this.validator,
    this.onChanged,
    this.vertical,
    this.autofillHints,
    this.onSubmitted,
  });

  double _getFontSize(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;
    if (screenWidth < 480) {
      return 12;
    } else if (screenWidth < 800) {
      return 15;
    }
    return 18;
  }

  @override
  Widget build(BuildContext context) {
    double fontSize = _getFontSize(context);
    etiqueta ??= "";
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (etiqueta != '')
          Text(
            etiqueta.toString(),
            style: TextStyle(
                fontSize: fontSize,
                fontWeight: FontWeight.bold,
                color: enabled ? Colors.black : Colors.grey),
          ),
        if (etiqueta != '')
          const SizedBox(
            height: 5,
          ),
        TextFormField(
          onFieldSubmitted: onSubmitted,
          autofillHints: autofillHints,
          validator: validator,
          enabled: enabled,
          style: TextStyle(
            fontSize: fontSize,
          ),
          maxLength: maxLength,
          maxLines: maxLines,
          inputFormatters: inputFormatters,
          initialValue: initialValue,
          controller: controller,
          onTap: onTap,
          readOnly: readOnly,
          keyboardType: keyboardType,
          textCapitalization: textCapitalization,
          cursorColor: AppTheme.tertiary,
          obscureText: obscureText,
          autocorrect: true,
          onChanged: onChanged,
          decoration: InputDecoration(
            contentPadding: vertical == null
                ? null
                : EdgeInsets.symmetric(vertical: vertical!),
            focusedBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10),
              borderSide: BorderSide(
                color: AppTheme.tertiary,
              ),
            ),
            enabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10),
              borderSide: BorderSide(
                color: Colors.grey[400]!,
              ),
            ),
            errorBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10),
              borderSide: BorderSide(
                color: Colors.red[200]!,
              ),
            ),
            focusedErrorBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10),
              borderSide: const BorderSide(
                color: Colors.red,
              ),
            ),
            disabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10),
              borderSide: BorderSide(
                color: Colors.grey[200]!,
              ),
            ),
            errorText: errorText,
            labelText: labelText,
            hintText: hintText,
            floatingLabelStyle: TextStyle(
              color: AppTheme.tertiary,
              fontSize: fontSize,
            ),
            filled: true,
            fillColor: fillColor,
            prefixIcon: prefixIcon,
            prefixIconColor: AppTheme.tertiary,
            suffixIcon: suffixIcon,
            suffixIconColor: AppTheme.tertiary,
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(15),
              borderSide: BorderSide.none,
            ),
          ),
        ),
      ],
    );
  }
}
