import 'package:flutter/material.dart';
import 'package:sigap/src/utils/constants/colors.dart';
import 'package:sigap/src/utils/constants/sizes.dart';

class CustomTextField extends StatelessWidget {
  final String label;
  final TextEditingController? controller;
  final String? initialValue;
  final String? Function(String?)? validator;
  final TextInputType? keyboardType;
  final TextInputAction? textInputAction;
  final String? errorText;
  final int? maxLines;
  final String? hintText;
  final Widget? prefixIcon;
  final Widget? suffixIcon;
  final bool? enabled;
  final bool readOnly;
  final bool obscureText;
  final void Function(String)? onChanged;
  final Color? accentColor;
  final Color? fillColor;
  final InputDecoration? decoration; // New parameter for custom decoration

  const CustomTextField({
    super.key,
    required this.label,
    this.controller,
    this.initialValue,
    this.validator,
    this.keyboardType,
    this.textInputAction,
    this.errorText,
    this.maxLines = 1,
    this.hintText,
    this.prefixIcon,
    this.suffixIcon,
    this.enabled = true,
    this.readOnly = false,
    this.obscureText = false,
    this.onChanged,
    this.accentColor,
    this.fillColor,
    this.decoration, // Add to constructor
  }) : assert(
         controller == null || initialValue == null,
         'Either provide a controller or an initialValue, not both',
       );

  @override
  Widget build(BuildContext context) {
    // Use theme's primary color by default or the provided accent color
    final Color effectiveAccentColor =
        accentColor ?? Theme.of(context).primaryColor;
    final isDark = Theme.of(context).brightness == Brightness.dark;

    // Determine the effective fill color
    final Color effectiveFillColor =
        enabled == false
            ? (isDark ? Colors.grey[800]! : Colors.grey[200]!)
            : fillColor ?? (isDark ? TColors.dark : TColors.lightContainer);

    // Get the input decoration - either custom or default
    final inputDecoration =
        decoration ??
        _getInputDecoration(
      context,
      effectiveAccentColor,
      isDark,
      effectiveFillColor,
    );

    // Style for disabled text
    final TextStyle? textStyle =
        enabled == false
            ? Theme.of(context).textTheme.bodyMedium?.copyWith(
              color: isDark ? Colors.grey[500] : Colors.grey[600],
            )
            : Theme.of(context).textTheme.bodyMedium;

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // Label text with dimmed color if disabled
        Text(
          label,
          style: Theme.of(context).textTheme.bodyMedium?.copyWith(
            fontWeight: FontWeight.w500,
            color:
                enabled == false
                    ? (isDark ? Colors.grey[500] : Colors.grey[600])
                    : null,
          ),
        ),
        const SizedBox(height: TSizes.sm),

        // Conditional rendering based on whether controller or initialValue is provided
        if (controller != null)
          TextFormField(
            controller: controller,
            validator: enabled == false ? null : validator,
            keyboardType: keyboardType,
            textInputAction: textInputAction,
            maxLines: maxLines,
            enabled: enabled,
            readOnly: readOnly,
            obscureText: obscureText,
            onChanged: onChanged,
            style: textStyle,
            decoration: inputDecoration,
          )
        else
          TextFormField(
            initialValue: initialValue,
            validator: enabled == false ? null : validator,
            keyboardType: keyboardType,
            textInputAction: textInputAction,
            maxLines: maxLines,
            enabled: enabled,
            readOnly: readOnly,
            obscureText: obscureText,
            onChanged: onChanged,
            style: textStyle,
            decoration: inputDecoration,
          ),
        const SizedBox(height: TSizes.spaceBtwInputFields),
      ],
    );
  }

  InputDecoration _getInputDecoration(
    BuildContext context,
    Color effectiveAccentColor,
    bool isDark,
    Color effectiveFillColor,
  ) {
    final borderColor =
        enabled == false
            ? (isDark ? Colors.grey[700] : Colors.grey[300])
            : Theme.of(context).dividerColor;
    
    return InputDecoration(
      hintText: hintText,
      hintStyle: Theme.of(context).textTheme.bodyMedium?.copyWith(
        color: isDark ? Colors.grey[400] : Colors.grey[600],
      ),
      errorText: errorText != null && errorText!.isNotEmpty ? errorText : null,
      errorStyle: TextStyle(color: TColors.error, fontSize: 12),
      contentPadding: const EdgeInsets.symmetric(
        horizontal: TSizes.md,
        vertical: TSizes.md,
      ),
      prefixIcon:
          enabled == false && prefixIcon != null
              ? IconTheme(
                data: IconThemeData(
                  color: isDark ? Colors.grey[600] : Colors.grey[400],
                ),
                child: prefixIcon!,
              )
              : prefixIcon,
      suffixIcon:
          enabled == false && suffixIcon != null
              ? IconTheme(
                data: IconThemeData(
                  color: isDark ? Colors.grey[600] : Colors.grey[400],
                ),
                child: suffixIcon!,
              )
              : suffixIcon,
      filled: true,
      fillColor: effectiveFillColor,
      // Use theme-aware border styling with different styling for disabled state
      border: OutlineInputBorder(
        borderRadius: BorderRadius.circular(TSizes.inputFieldRadius),
        borderSide: BorderSide(color: borderColor!, width: 1),
      ),
      enabledBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(TSizes.inputFieldRadius),
        borderSide: BorderSide(color: borderColor, width: 1),
      ),
      focusedBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(TSizes.inputFieldRadius),
        borderSide: BorderSide(
          color: enabled == false ? borderColor : effectiveAccentColor,
          width: enabled == false ? 1 : 1.5,
        ),
      ),
      disabledBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(TSizes.inputFieldRadius),
        borderSide: BorderSide(
          color: borderColor,
          width: 1,
          style: BorderStyle.solid,
        ),
      ),
      errorBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(TSizes.inputFieldRadius),
        borderSide: BorderSide(
          color: TColors.error,
          width: 1,
        ),
      ),
      focusedErrorBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(TSizes.inputFieldRadius),
        borderSide: BorderSide(
          color: TColors.error,
          width: 1.5,
        ),
      ),
    );
  }
}
