import 'package:flutter/material.dart';
import 'package:niogu_app/core/constants/app_font_size.dart';
import 'package:sizer/sizer.dart';

class CustomFormInput extends StatelessWidget {
  final String label;
  final String? hint;
  final IconData icon;
  final TextEditingController controller;
  final void Function(String)? onChanged;
  final bool obscureText;
  final TextInputType inputType;
  final String? errorText;
  final String? Function(String?)? validator;
  final IconData? suffixIcon;
  final VoidCallback? suffixPressed;

  const CustomFormInput({
    super.key,
    required this.label,
    this.hint,
    required this.icon,
    required this.controller,
    this.onChanged,
    this.obscureText = false,
    this.inputType = TextInputType.text,
    this.errorText,
    this.validator,
    this.suffixIcon,
    this.suffixPressed,
  });

  @override
  Widget build(BuildContext context) {
    final bool isTablet = 100.w >= 600;
    return TextFormField(
      controller: controller,
      onChanged: onChanged,
      obscureText: obscureText,
      keyboardType: inputType,
      autovalidateMode: AutovalidateMode.onUserInteraction,
      validator: validator,
      style: TextStyle(
        fontSize: isTablet ? AppFontSize.medium.sp : AppFontSize.small.sp,
      ),
      decoration: InputDecoration(
        hintText: hint,
        hintStyle: TextStyle(
          fontSize: isTablet
              ? (AppFontSize.medium - 1.25).sp
              : (AppFontSize.small - 1.25).sp,
          color: Colors.grey[500],
        ),
        labelText: label,
        labelStyle: TextStyle(fontSize: (AppFontSize.medium - 1.25).sp),
        prefixIcon: Padding(
          padding: isTablet
              ? EdgeInsets.symmetric(horizontal: 3.w)
              : EdgeInsets.zero,
          child: Icon(icon, color: Colors.grey, size: 5.w),
        ),
        errorText: errorText,
        errorStyle: TextStyle(
          color: Colors.red,
          fontSize: isTablet ? AppFontSize.medium.sp : AppFontSize.small.sp,
        ),

        errorBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(2.5.w),
          borderSide: const BorderSide(color: Colors.redAccent),
        ),
        border: OutlineInputBorder(borderRadius: BorderRadius.circular(2.5.w)),
        contentPadding: EdgeInsets.symmetric(
          vertical: isTablet ? 3.6.h : 1.8.h,
        ),
        suffixIcon: suffixIcon == null
            ? null
            : Padding(
                padding: EdgeInsets.symmetric(
                  horizontal: isTablet ? 5.w : 2.5.w,
                ),
                child: IconButton(
                  onPressed: suffixPressed,
                  icon: Icon(
                    suffixIcon,
                    size: 5.w,
                    color: Colors.grey.shade500,
                  ),
                ),
              ),
        isDense: true,
      ),
    );
  }
}
