import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:rasadyar_core/core.dart';

enum RTextFieldVariant {
  normal,
  noBorder,
  password,
  passwordNoBorder,
}

class RTextField extends StatefulWidget {
  final TextEditingController controller;
  final String? label;
  final String? hintText;
  final String? initText;
  final bool obscure;
  final bool readonly;
  final bool enabled;
  final int? maxLength;
  final int? minLines;
  final int? maxLines;
  final Widget? suffixIcon;
  final Widget? prefixIcon;
  final BoxConstraints? boxConstraints;
  final RTextFieldVariant variant;
  final bool filled;
  final Color? filledColor;
  final Color? borderColor;
  final bool showCounter;
  final bool isDense;
  final TextInputType? keyboardType;
  final TextStyle? style;
  final TextStyle? hintStyle;
  final TextStyle? labelStyle;
  final TextStyle? errorStyle;
  final EdgeInsets? padding;
  final FormFieldValidator<String>? validator;
  final void Function(String)? onChanged;
  final void Function(String)? onSubmitted;
  final List<TextInputFormatter>? inputFormatters;

  const RTextField({
    super.key,
    required this.controller,
    this.label,
    this.hintText,
    this.initText,
    this.obscure = false,
    this.readonly = false,
    this.enabled = true,
    this.maxLength,
    this.minLines,
    this.maxLines = 1,
    this.suffixIcon,
    this.prefixIcon,
    this.boxConstraints,
    this.variant = RTextFieldVariant.normal,
    this.filled = false,
    this.filledColor,
    this.showCounter = false,
    this.isDense = false,
    this.keyboardType,
    this.style,
    this.hintStyle,
    this.labelStyle,
    this.errorStyle,
    this.padding,
    this.validator,
    this.onChanged,
    this.onSubmitted,
    this.borderColor,
    this.inputFormatters
  });


  @override
  State<RTextField> createState() => _RTextFieldState();

  bool get _isPassword => variant == RTextFieldVariant.password;
  bool get _noBorder => variant == RTextFieldVariant.noBorder;
  bool get _passwordNoBorder => variant == RTextFieldVariant.passwordNoBorder;


  InputBorder get _inputBorder =>
      _noBorder || _passwordNoBorder ? InputBorder.none : OutlineInputBorder(
        borderRadius: BorderRadius.circular(8),
        borderSide: BorderSide(
          color: borderColor ?? AppColor.lightGreyDarkActive,
          width: 1,
        ),
      );






}

class _RTextFieldState extends State<RTextField> {
  late bool obscure;

  @override
  void initState() {
    super.initState();
    if (widget.initText != null) {
      widget.controller.text = widget.initText!;
    }
    obscure = widget.obscure;
  }

  @override
  void didUpdateWidget(covariant RTextField oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.initText != null && widget.initText != oldWidget.initText) {
      widget.controller.text = widget.initText!;
    }
  }

  Widget _buildSuffixIcon() {
    if (widget.suffixIcon != null) return widget.suffixIcon!;
    if (!widget._isPassword) return const SizedBox.shrink();

    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 8),
      child: GestureDetector(
        onTap: () {
          setState(() {
            obscure = !obscure;
          });
        },
        child: Icon(
          obscure ? CupertinoIcons.eye : CupertinoIcons.eye_slash,
          color: AppColor.darkGreyDarkActive,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: widget.padding ?? EdgeInsets.zero,
      child: TextFormField(
        controller: widget.controller,
        readOnly: widget.readonly,
        minLines: widget.minLines,
        maxLines: widget.maxLines,
        onChanged: widget.onChanged,
        validator: widget.validator,
        inputFormatters: widget.inputFormatters,
        enabled: widget.enabled,
        obscureText: obscure,
        onTapOutside: (_) => FocusScope.of(context).unfocus(),
        onFieldSubmitted: widget.onSubmitted,
        maxLength: widget.maxLength,
        textDirection: TextDirection.rtl,
        style: widget.style,
        keyboardType: widget.keyboardType,
        decoration: InputDecoration(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16),
          errorStyle: widget.errorStyle,
          errorMaxLines: 1,
          isDense: widget.isDense,
          suffixIcon: _buildSuffixIcon(),
          suffixIconConstraints: widget.boxConstraints,
          prefixIcon: widget.prefixIcon,
          prefixIconConstraints: widget.boxConstraints,
          hintText: widget.hintText,
          labelText: widget.label,
          alignLabelWithHint: true,
          labelStyle: AppFonts.yekan14
              .copyWith(color: AppColor.lightGreyDarkActive)
              .merge(widget.labelStyle),
          filled: widget.filled || widget._noBorder || widget._passwordNoBorder,
          fillColor: widget.filledColor,
          counter: widget.showCounter ? null : const SizedBox(),
          hintStyle: widget.hintStyle,
          enabledBorder: widget._inputBorder,
          focusedBorder: widget._inputBorder,
          border: widget._inputBorder,
        ),
      ),
    );
  }
}