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

class PasswordField extends StatelessWidget {
  final String label;
  final TextEditingController controller;
  final String? Function(String?)? validator;
  final RxBool isVisible;
  final String? errorText;
  final Function() onToggleVisibility;
  final TextInputAction textInputAction;
  final String? hintText;
  final Widget? prefixIcon;
  final Color? accentColor;

  const PasswordField({
    super.key,
    required this.label,
    required this.controller,
    required this.validator,
    required this.isVisible,
    this.errorText,
    required this.onToggleVisibility,
    this.textInputAction = TextInputAction.done,
    this.hintText,
    this.prefixIcon,
    this.accentColor,
  });

  @override
  Widget build(BuildContext context) {
    final Color effectiveAccentColor = accentColor ?? TColors.primary;
    final isDark = THelperFunctions.isDarkMode(context);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: Theme.of(context).textTheme.bodyLarge?.copyWith(
            fontWeight: FontWeight.w500,
            color: isDark ? TColors.white : TColors.textPrimary,
          ),
        ),
        const SizedBox(height: TSizes.sm),
        Obx(
          () =>
          TextFormField(
            controller: controller,
            validator: validator,
            obscureText: !isVisible.value,
            textInputAction: textInputAction,
            style: Theme.of(context).textTheme.bodyLarge?.copyWith(
              color: isDark ? TColors.white : TColors.textPrimary,
            ),
            decoration: InputDecoration(
              hintText: hintText,
              hintStyle: Theme.of(
                context,
              ).textTheme.bodyMedium?.copyWith(color: TColors.textSecondary),
              errorText:
                  errorText != null && errorText!.isNotEmpty ? errorText : null,
              errorStyle: Theme.of(
                context,
              ).textTheme.bodySmall?.copyWith(color: TColors.error),
              contentPadding: const EdgeInsets.symmetric(
                horizontal: TSizes.md,
                vertical: TSizes.md,
              ),
              prefixIcon: prefixIcon,
              suffixIcon: IconButton(
                onPressed: onToggleVisibility,
                icon: Icon(
                  isVisible.value ? Icons.visibility_off : Icons.visibility,
                  color: effectiveAccentColor,
                  semanticLabel:
                      isVisible.value ? 'Hide password' : 'Show password',
                ),
              ),
              filled: true,
              fillColor:
                  isDark ? TColors.dark : TColors.lightContainer,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(TSizes.inputFieldRadius),
                borderSide: BorderSide(color: TColors.borderPrimary, width: 1),
              ),
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(TSizes.inputFieldRadius),
                borderSide: BorderSide(color: TColors.borderPrimary, width: 1),
              ),
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(TSizes.inputFieldRadius),
                borderSide: BorderSide(color: effectiveAccentColor, width: 1.5),
              ),
              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),
              ),
            ),
          ),
        ),
        const SizedBox(height: TSizes.spaceBtwInputFields),
      ],
    );
  }
}
