import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:niogu_ecommerce_v1/core/constant/app_color.dart';
import 'package:niogu_ecommerce_v1/core/constant/app_font_size.dart';
import 'package:niogu_ecommerce_v1/core/widgets/custom_snackbar.dart';
import 'package:sizer/sizer.dart';

class CustomTextFormField extends StatelessWidget {
  final String label;
  final Color? labelColor;
  final String? hint;
  final String? addInformation;
  final TextEditingController controller;
  final TextInputType keyboardType;
  final bool autoFocus;
  final String? errorText;
  final String? Function(String?)? validator;
  final Function(String)? onChanged;
  final bool isObscure;
  final bool readOnly;
  final List<TextInputFormatter>? inputFormatters;
  final String? onTapReadOnlyMessage;
  final Color? enabledBorderColor;
  final Color? focusedBorderColor;
  final IconData? prefixIcon;
  final IconData? suffixIcon;
  final VoidCallback? suffixPressed;
  final String? suffixText;

  CustomTextFormField({
    super.key,
    required this.label,
    this.labelColor,
    this.hint,
    this.addInformation,
    required this.controller,
    this.keyboardType = TextInputType.text,
    this.autoFocus = false,
    this.errorText,
    this.validator,
    this.onChanged,
    this.isObscure = false,
    this.readOnly = false,
    this.inputFormatters,
    this.onTapReadOnlyMessage,
    this.enabledBorderColor,
    this.focusedBorderColor,
    this.prefixIcon,
    this.suffixIcon,
    this.suffixPressed,
    this.suffixText,
  });

  @override
  Widget build(BuildContext context) {
    final bool isTablet = 100.w >= 600;
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: TextStyle(
            fontSize: isTablet ? AppFontSize.medium.sp : AppFontSize.small.sp,
            fontWeight: FontWeight.bold,
            color: labelColor ?? Colors.black87,
          ),
        ),
        if (addInformation != null) ...[
          SizedBox(height: 1.h),
          Padding(
            padding: EdgeInsets.only(bottom: 1.5.h),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    Icon(
                      Icons.info_outline,
                      size: 3.5.w,
                      color: AppColor.primaryColor,
                    ),
                    SizedBox(width: 0.75.w),
                    Text(
                      addInformation!,
                      style: TextStyle(
                        color: AppColor.primaryColor,
                        fontSize: isTablet
                            ? (AppFontSize.medium - 1.25).sp
                            : (AppFontSize.small - 1.25).sp,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ],
        SizedBox(height: 1.h),
        TextFormField(
          controller: controller,
          keyboardType: keyboardType,
          autofocus: autoFocus,
          obscureText: isObscure,
          onChanged: onChanged,
          validator: validator,
          readOnly: readOnly,
          onTap: !readOnly
              ? null
              : () {
                  if (onTapReadOnlyMessage != null &&
                      onTapReadOnlyMessage!.isNotEmpty) {
                    CustomSnackbar.showWarning(context, onTapReadOnlyMessage!);
                  }
                },
          autovalidateMode: AutovalidateMode.onUserInteraction,
          style: TextStyle(
            fontSize: isTablet ? AppFontSize.medium.sp : AppFontSize.small.sp,
            fontWeight: FontWeight.w500,
          ),
          inputFormatters: inputFormatters,
          decoration: InputDecoration(
            filled: readOnly,
            fillColor: readOnly ? Colors.grey.shade100 : Colors.white,
            hintText: hint,
            hintStyle: TextStyle(
              color: Colors.grey.shade500,
              fontSize: isTablet ? AppFontSize.medium.sp : AppFontSize.small.sp,
            ),
            contentPadding: EdgeInsets.symmetric(
              horizontal: 4.w,
              vertical: 1.8.h,
            ),
            enabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(2.5.w),
              borderSide: BorderSide(
                color: enabledBorderColor ?? Colors.grey.shade300,
              ),
            ),
            focusedBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(2.5.w),
              borderSide: BorderSide(
                color: readOnly
                    ? Colors.grey.shade300
                    : (focusedBorderColor ?? AppColor.primaryColor),
                width: 1,
              ),
            ),
            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),
            ),
            focusedErrorBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(2.5.w),
              borderSide: const BorderSide(color: Colors.redAccent, width: 1.5),
            ),
            prefixIcon: prefixIcon == null
                ? null
                : Padding(
                    padding: EdgeInsets.symmetric(
                      horizontal: isTablet ? 5.w : 2.5.w,
                    ),
                    child: Icon(
                      prefixIcon,
                      size: 5.w,
                      color: Colors.grey.shade500,
                    ),
                  ),
            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,
                      ),
                    ),
                  ),
            suffixText: suffixText,
            suffixStyle: TextStyle(
              fontSize: isTablet
                  ? (AppFontSize.medium - 1.25).sp
                  : (AppFontSize.small - 1.25).sp,
              color: Colors.grey,
            ),
          ),
        ),
      ],
    );
  }
}
