import 'package:bootstrap_icons/bootstrap_icons.dart';
import 'package:english_learning/features/auth/provider/validator_provider.dart';
import 'package:flutter/material.dart';
import 'package:english_learning/core/utils/styles/theme.dart';
import 'package:provider/provider.dart';

class CustomFieldWidget extends StatefulWidget {
  final String fieldName;
  final String labelText;
  final dynamic hintText;
  final String? Function(String?)? validator;
  final bool obscureText;
  final TextEditingController? controller;
  final IconButton? suffixIcon;
  final TextInputAction textInputAction;
  final TextInputType? keyboardType;
  final bool? isError;
  final FocusNode? focusNode;
  final Function(String?)? onChanged;
  final String? errorText;
  final Function()? onSuffixIconTap;
  final bool isRequired;
  final bool isEnabled;
  final Function(String?)? onFieldSubmitted;

  const CustomFieldWidget({
    super.key,
    required this.fieldName,
    required this.labelText,
    required this.textInputAction,
    this.hintText,
    this.validator,
    this.obscureText = false,
    this.controller,
    this.suffixIcon,
    this.focusNode,
    this.keyboardType,
    this.isError = false,
    this.errorText,
    this.onChanged,
    this.onSuffixIconTap,
    this.isRequired = false,
    this.isEnabled = true,
    this.onFieldSubmitted,
  });

  @override
  State<CustomFieldWidget> createState() => _CustomFieldWidgetState();
}

class _CustomFieldWidgetState extends State<CustomFieldWidget> {
  late TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _controller = widget.controller ?? TextEditingController();

    context
        .read<ValidatorProvider>()
        .setController(widget.fieldName, _controller);
  }

  @override
  void dispose() {
    context.read<ValidatorProvider>().removeController(widget.fieldName);
    if (widget.controller == null) {
      _controller.dispose();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text.rich(
          TextSpan(
            children: [
              TextSpan(
                text: widget.labelText,
                style: AppTextStyles.blackTextStyle.copyWith(
                  fontSize: 12,
                  fontWeight: FontWeight.bold,
                ),
              ),
              if (widget.isRequired)
                TextSpan(
                  text: ' *',
                  style: AppTextStyles.blackTextStyle.copyWith(
                    fontSize: 12,
                    fontWeight: FontWeight.bold,
                    color: AppColors.redColor, // Red color for asterisk
                  ),
                ),
            ],
          ),
        ),
        const SizedBox(height: 8),
        TextFormField(
          controller: widget.controller,
          keyboardType: widget.keyboardType,
          textInputAction: widget.textInputAction,
          obscureText: widget.obscureText,
          focusNode: widget.focusNode,
          onChanged: widget.onChanged,
          onFieldSubmitted: widget.onFieldSubmitted,
          enabled: widget.isEnabled,
          decoration: InputDecoration(
            hintText: widget.hintText,
            hintStyle: AppTextStyles.disableTextStyle.copyWith(
              fontSize: 12,
            ),
            fillColor: widget.isEnabled
                ? AppColors.whiteColor
                : AppColors.cardDisabledColor,
            filled: true,
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(8),
            ),
            focusedBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(8),
              borderSide: const BorderSide(
                width: 1,
                color: AppColors.cardDisabledColor,
              ),
            ),
            enabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(8),
              borderSide: const BorderSide(
                width: 1,
                color: AppColors.cardDisabledColor,
              ),
            ),
            disabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(8),
              borderSide: const BorderSide(
                width: 1,
                color: AppColors
                    .cardDisabledColor, // Adjust this color if necessary
              ),
            ),
            suffixIcon: widget.onSuffixIconTap != null
                ? GestureDetector(
                    onTap: widget.onSuffixIconTap,
                    child: AnimatedSwitcher(
                      duration: const Duration(milliseconds: 300),
                      transitionBuilder:
                          (Widget child, Animation<double> animation) {
                        return ScaleTransition(scale: animation, child: child);
                      },
                      child: Icon(
                        widget.obscureText
                            ? BootstrapIcons.eye_slash
                            : BootstrapIcons.eye,
                        color: AppColors.greyColor,
                        size: 20,
                      ),
                    ),
                  )
                : null,
            errorText: widget.errorText,
          ),
          validator: widget.validator,
        ),
      ],
    );
  }
}
