import 'package:flutter/material.dart';
import 'package:glassmorphism/glassmorphism.dart';

class CustomTextFormField extends StatefulWidget {
  const CustomTextFormField({
    Key? key,
    this.validator,
    this.textEditingController,
    this.hintText,
    this.leadingIcon,
    this.prefixIconColor = const Color(0xFF737373),
    this.isInputPassword = false,
    this.validatorText,
    this.value,
    this.readonly = false,
    this.enabled = true,
    this.maxlines = 1,
    this.texttype,
    this.inputFormatters,
    this.onInput,
  }) : super(key: key);

  final dynamic validator;
  final TextEditingController? textEditingController;
  final String? hintText;
  final Widget? leadingIcon;
  final Color prefixIconColor;
  final bool isInputPassword;
  final String? validatorText;
  final String? value;
  final bool readonly;
  final bool enabled;
  final int maxlines;
  final TextInputType? texttype;
  final dynamic inputFormatters;
  final Function(String)? onInput;

  @override
  State<CustomTextFormField> createState() => _CustomTextFormFieldState();
}

class _CustomTextFormFieldState extends State<CustomTextFormField> {
  late bool obscureText;

  @override
  void initState() {
    super.initState();
    obscureText = widget.isInputPassword;
  }

  @override
  Widget build(BuildContext context) {
    return GlassmorphicContainer(
      width: double.infinity,
      height: 50,
      borderRadius: 8,
      blur: 10,
      alignment: Alignment.bottomCenter,
      border: 0.8,
      linearGradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: [
            Color(0xFFffffff).withOpacity(0.1),
            Color(0xFFFFFFFF).withOpacity(0.05),
          ],
          stops: [
            0.1,
            1,
          ]),
      borderGradient: LinearGradient(
        begin: Alignment.topLeft,
        end: Alignment.bottomRight,
        colors: [
          Color(0xff9A0000).withOpacity(0.5),
          Color(0xFFffffff).withOpacity(0.5),
        ],
      ),
      child: TextFormField(
        cursorColor: Colors.red,
        initialValue: widget.value,
        readOnly: widget.readonly,
        enabled: widget.enabled,
        enableInteractiveSelection: false,
        maxLines: widget.maxlines,
        autovalidateMode: AutovalidateMode.onUserInteraction,
        obscureText: obscureText,
        controller: widget.textEditingController,
        decoration: InputDecoration(
          hintText: widget.hintText,
          prefixIconColor: widget.prefixIconColor,
          // ignore: prefer_null_aware_operators
          prefixIcon: widget.leadingIcon == null ? null : widget.leadingIcon!,
          suffixIcon: widget.isInputPassword
              ? GestureDetector(
                  onTap: () => setState(() => obscureText = !obscureText),
                  child: obscureText
                      ? const Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Padding(
                              padding: EdgeInsets.only(right: 20.0),
                              child: Icon(Icons.remove_red_eye),
                            ),
                          ],
                        )
                      : const Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Padding(
                              padding: EdgeInsets.only(right: 20.0),
                              child: Icon(
                                Icons.remove_red_eye_outlined,
                                color: Color(0xFF959595),
                              ),
                            ),
                          ],
                        ),
                )
              : null,
          border: InputBorder.none,
          contentPadding: EdgeInsets.symmetric(horizontal: 10),
        ),
        style: TextStyle(color: Colors.white),
        keyboardType: widget.texttype,
        // validator: widget.validator ??
        //     (value) {
        //       if (value == null || value.isEmpty) {
        //         return "Empty value";
        //       }
        //       return null;
        //     },
        inputFormatters: widget.inputFormatters,
        onChanged: (value) {
          widget.onInput?.call(value);
        },
      ),
    );
  }
}
