// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:qadirneyriz/config/config.dart';
import 'package:qadirneyriz/widgets/custom_button.dart';

// ignore: must_be_immutable
class CustomTextField extends StatefulWidget {
  final String label;
  final String hintText;
  final bool obscureText;
  final double contentPadding;
  final bool hasButton;
  final TextInputAction textInputAction;
  final TextEditingController textEditingController;
  final TextInputType textInputType;
  final int? maxLine;
  final Color? fillsColor;
  bool isReadOnly = false;
  final double paddingVertical;
  final double paddingHarizon;
  bool isPass = false;
  FocusNode? focus;
  IconData? firstIcon;
  final void Function(String s)? onChanged;
  final void Function()? buttonOnPressed;
  bool hasLable = true;
  CustomTextField({
    Key? key,
    required this.label,
    required this.hintText,
    this.obscureText = false,
    this.contentPadding = 10,
    this.hasButton = false,
    this.textInputAction = TextInputAction.done,
    required this.textEditingController,
    required this.textInputType,
    this.maxLine,
    this.fillsColor = Colors.white,
    this.isReadOnly = false,
    this.isPass = false,
    this.focus,
    this.firstIcon,
    this.onChanged,
    this.buttonOnPressed,
    this.hasLable = true,
    this.paddingVertical = 10,
    this.paddingHarizon = 20,
  }) : super(key: key);

  @override
  _CustomTextFieldState createState() => _CustomTextFieldState();
}

bool obscureText = true;

class _CustomTextFieldState extends State<CustomTextField> {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(
          horizontal: widget.paddingHarizon, vertical: widget.paddingVertical),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          widget.hasLable
              ? Padding(
                  padding: const EdgeInsets.only(right: 5),
                  child: Text(
                    widget.label,
                    style: TextStyle(
                      fontWeight: FontWeight.normal,
                      fontSize: 13,
                      color: Colors.black.withOpacity(.8),
                    ),
                  ),
                )
              : Container(),
          SizedBox(height: widget.hasLable ? 6 : 0),
          Stack(
            children: [
              TextFormField(
                inputFormatters: [
                  widget.textInputType == TextInputType.number
                      ? FilteringTextInputFormatter.allow(
                          RegExp('[a-z A-Z 0-9 ا-ی]'))
                      : FilteringTextInputFormatter.singleLineFormatter
                ],
                readOnly: widget.isReadOnly,
                onFieldSubmitted: (v) {
                  FocusScope.of(context).requestFocus(widget.focus);
                },
                onChanged: widget.onChanged,

                minLines: 1, // تعیین ارتفاع بر اساس تعداد خطوط
                maxLines: 1, // تعداد خطوط نامحدود
                controller: widget.textEditingController,
                keyboardType: widget.textInputType,
                textInputAction: widget.textInputAction,
                obscureText: (widget.isPass) ? obscureText : false,
                style: TextStyle(
                  fontSize: 12,
                  color: Colors.black.withOpacity(.5),
                ),

                decoration: InputDecoration(
                    hintText: widget.hintText,
                    hintStyle: TextStyle(
                        color: config.ui.mainGray.withOpacity(.6),
                        fontSize: 12),
                    // filled: true,
                    // fillColor: widget.fillsColor,
                    enabledBorder: UnderlineInputBorder(
                      borderSide: BorderSide(
                        color:
                            config.ui.mainGreen, // رنگ بردر پایین در حالت فعال
                        width: 2, // ضخامت بردر پایین
                      ),
                    ),
                    focusedBorder: UnderlineInputBorder(
                      borderSide: BorderSide(
                        color:
                            config.ui.mainGreen, // رنگ بردر پایین در حالت فوکوس
                        width: 2, // ضخامت بردر پایین
                      ),
                    ),
                    prefixIcon: widget.firstIcon != null
                        ? Row(
                            mainAxisSize: MainAxisSize.min,
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Icon(
                                widget.firstIcon,
                                color: config.ui.mainGreen,
                              ),
                              const SizedBox(
                                width: 10,
                              ),
                              Container(
                                color: config.ui.mainGreen,
                                width: 1.5,
                                height: 25,
                              ),
                            ],
                          )
                        : null,
                    suffixIcon: (widget.isPass == true)
                        ? InkWell(
                            onTap: () {
                              setState(() {
                                obscureText = !obscureText;
                              });
                            },
                            child: Icon(
                              obscureText
                                  ? Icons.visibility_off
                                  : Icons.visibility,
                              color: Colors.grey,
                            ),
                          )
                        : null,
                    contentPadding: EdgeInsets.all(widget.contentPadding)),
              ),
              widget.hasButton
                  ? Positioned(
                      left: 10,
                      top: 10,
                      child: Center(
                        child: CustomButton(
                          hieght: 35,
                          width: 100,
                          text: 'ثبت',
                          onPressed: widget.buttonOnPressed,
                        ),
                      ))
                  : Container()
            ],
          ),
        ],
      ),
    );
  }
}
