import 'package:cims_apps/application/theme/color_palette.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:remove_emoji_input_formatter/remove_emoji_input_formatter.dart';

class TextFormView extends StatelessWidget {
  final String name;
  final String? helperText;
  final String? initialValue;
  final VoidCallback? onTap;
  final VoidCallback? onSubmit;
  final bool enabled;
  final bool readOnly;
  final String? hintText, errorText;
  final TextEditingController? ctrl;
  final Widget? suffixIcon, suffixLable;
  final Widget? prefixIcon, prefix;
  final TextInputType? keyboardType;
  final FormFieldValidator<String>? validator;
  final bool obscureText;
  final int? maxLength;
  final ValueChanged<String>? onChanged;
  final List<TextInputFormatter>? inputFormatters;
  final TextStyle? errorStyle, hintTextStyle;
  final _borderRadius = const BorderRadius.all(Radius.circular(10));
  final Color? enabledborderColor;
  final Color? focusedBorderColor;
  final bool textrea, isTextAlignCenter;
  final Widget? trailingTitleWidget;
  final BoxConstraints? suffixIconConstraints;
  final BoxConstraints? preffixIconConstraints;
  final bool disableColor;
  final Color? disabledborderColor;
  final bool? enableInteractiveSelection;
  final Color? fontColorDisabled;
  final FocusNode? focusNode;

  // ignore: prefer_const_constructors_in_immutables
  TextFormView(
      {Key? key,
      required this.name,
      this.helperText,
      this.onTap,
      this.fontColorDisabled,
      this.enabledborderColor,
      this.disabledborderColor,
      this.initialValue,
      this.enabled = true,
      this.readOnly = false,
      this.obscureText = false,
      this.hintText,
      this.hintTextStyle,
      this.suffixIcon,
      this.suffixLable,
      this.prefixIcon,
      this.keyboardType,
      this.ctrl,
      this.focusedBorderColor,
      this.validator,
      this.maxLength,
      this.onChanged,
      this.inputFormatters,
      this.onSubmit,
      this.textrea = false,
      this.errorText,
      this.errorStyle,
      this.trailingTitleWidget,
      this.suffixIconConstraints,
      this.preffixIconConstraints,
      this.disableColor = false,
      this.enableInteractiveSelection = true,
      this.focusNode,
      this.isTextAlignCenter = false,
      this.prefix})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (inputFormatters != null && maxLength != null) {
      inputFormatters?.add(LengthLimitingTextInputFormatter(maxLength));
    }
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            name.isNotEmpty
                ? validator != null
                    ? Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            name,
                            style: const TextStyle(
                              fontSize: 16,
                              // color: ColorPalette.greyLight,
                            ),
                          ),
                          suffixLable ??
                              const Text(
                                "",
                                style: TextStyle(
                                  fontSize: 16,
                                  color: Colors.red,
                                ),
                              ),
                        ],
                      )
                    : Text(
                        name,
                        style: const TextStyle(
                          fontSize: 16,
                        ),
                      )
                : const SizedBox(),
            trailingTitleWidget ?? const SizedBox(),
          ],
        ),
        const SizedBox(height: 8.0),
        TextFormField(
          focusNode: focusNode,
          onTapOutside: (event) => FocusScope.of(context).unfocus(),
          minLines: textrea ? 8 : 1,
          maxLines: textrea ? null : 1,
          initialValue: initialValue,
          enabled: enabled,
          controller: ctrl,
          maxLength: maxLength,
          keyboardType: keyboardType,
          onTap: onTap,
          onEditingComplete: onSubmit,
          style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 14,
            color: fontColorDisabled ?? Colors.black,
          ),
          readOnly: readOnly,
          validator: validator,
          obscureText: obscureText,
          onChanged: onChanged,
          inputFormatters: inputFormatters ??
              [
                RemoveEmojiInputFormatter(),
                if (maxLength != null)
                  LengthLimitingTextInputFormatter(maxLength)
              ],
          enableInteractiveSelection: enableInteractiveSelection,
          textAlign: isTextAlignCenter ? TextAlign.center : TextAlign.left,
          decoration: InputDecoration(
              helperText: helperText,
              errorStyle: errorStyle,
              errorText: errorText,
              errorMaxLines: 2,
              hintStyle: hintTextStyle ??
                  const TextStyle(
                    fontSize: 14,
                    color: ColorPalette.greyFont,
                    fontWeight: FontWeight.normal,
                  ),
              isDense: true,
              hintText: hintText,
              filled: true,
              fillColor: enabled && disableColor == false
                  ? Colors.white
                  : const Color.fromARGB(255, 233, 236, 239),
              disabledBorder: OutlineInputBorder(
                borderRadius: _borderRadius,
                borderSide: BorderSide(
                  color: disabledborderColor ?? ColorPalette.greyBorder,
                ),
              ),
              enabledBorder: OutlineInputBorder(
                borderRadius: _borderRadius,
                borderSide: BorderSide(
                  color: enabledborderColor ?? ColorPalette.greyBorder,
                ),
              ),
              focusedBorder: OutlineInputBorder(
                borderRadius: _borderRadius,
                borderSide: BorderSide(
                  color: focusedBorderColor ?? ColorPalette.greyBorder,
                ),
              ),
              border: OutlineInputBorder(borderRadius: _borderRadius),
              suffixIcon: suffixIcon,
              prefixIcon: prefixIcon,
              suffixIconConstraints: suffixIconConstraints,
              prefixIconConstraints: preffixIconConstraints,
              prefix: prefix,
              contentPadding: const EdgeInsets.symmetric(
                horizontal: 8.0,
                vertical: 16.0,
              )),
        )
      ],
    );
  }
}
