import 'package:farm_tpf/themes/app_dimension.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';

import '../../../themes/app_colors.dart';
import '../../../themes/styles_text.dart';
import '../../../utils/app_images.dart';
import '../../../utils/validators.dart';

class TextFieldNormal extends StatefulWidget {
  final TextEditingController controller;
  final Function(String)? onFieldSubmitted;
  final Function(String)? onChanged;
  final FormFieldValidator<String>? validator;
  final String hint;
  final FocusNode? focusNode;
  final TextInputType? keyboardType;
  final TextInputAction? textInputAction;
  final List<TextInputFormatter>? inputFormatters;
  final bool? obscureText;
  final String? errorText;
  final Color? borderColor;
  final bool? isPasswordField;
  final Widget? prefixIcon;
  final String? prefixText;
  final int? maxLength;
  final bool? disabled;
  final TextCapitalization? textCapitalization;
  final String? suffixText;
  final HexColor? disableColor;
  final InputBorder? customBorder;
  final int? maxLines;
  final bool? autofocus;

  TextFieldNormal({
    required this.controller,
    required this.hint,
    this.validator,
    this.onFieldSubmitted,
    this.focusNode,
    this.keyboardType,
    this.textInputAction,
    this.inputFormatters,
    this.obscureText,
    this.errorText,
    this.onChanged,
    this.borderColor,
    this.isPasswordField,
    this.prefixIcon,
    this.maxLength,
    this.disabled,
    this.textCapitalization,
    this.suffixText,
    this.disableColor,
    this.prefixText,
    this.customBorder,
    this.maxLines,
    this.autofocus,
  });

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

class _TextFieldNormalState extends State<TextFieldNormal> {
  var isObscureText = false;
  late HexColor disableColor;
  final _errorMessage = ValueNotifier('');

  @override
  void initState() {
    super.initState();
    isObscureText = widget.isPasswordField ?? false;
    disableColor = widget.disableColor ?? AppColors.white;
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        TextFormField(
          keyboardType: widget.keyboardType ?? TextInputType.text,
          textInputAction: widget.textInputAction ?? TextInputAction.done,
          inputFormatters: widget.inputFormatters,
          obscureText: isObscureText,
          controller: widget.controller,
          validator: onValidator,
          onFieldSubmitted: widget.onFieldSubmitted,
          focusNode: widget.focusNode,
          autocorrect: false,
          autofocus: widget.autofocus ?? false,
          onChanged: widget.onChanged,
          maxLength: widget.maxLength,
          style: StylesText.body6,
          maxLines: widget.maxLines ?? 1,
          autovalidateMode: AutovalidateMode.disabled,
          enabled: widget.disabled != true,
          textCapitalization: widget.textCapitalization ?? TextCapitalization.none,
          decoration: InputDecoration(
            contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16),
            isDense: true,
            counterText: '',
            suffixText: widget.suffixText,
            suffixIconConstraints: BoxConstraints(maxHeight: 20),
            suffixIcon: widget.isPasswordField != null
                ? Padding(
                    padding: const EdgeInsetsDirectional.only(end: 20.0),
                    child: InkWell(
                      onTap: () {
                        setState(
                          () {
                            isObscureText = !isObscureText;
                          },
                        );
                      },
                      child: isObscureText
                          ? SizedBox(
                              width: 20.w,
                              height: 20.h,
                              child: SvgPicture.asset(
                                AssetSVG.eye,
                              ),
                            )
                          : SizedBox(
                              width: 20.w,
                              height: 20.h,
                              child: SvgPicture.asset(
                                AssetSVG.eyeOff,
                              ),
                            ),
                    ),
                  )
                : Padding(
                    padding: const EdgeInsetsDirectional.only(end: 8.0),
                    child: Text(
                      '',
                      style: StylesText.body3,
                    ),
                  ),
            enabledBorder: widget.customBorder ??
                OutlineInputBorder(
                  borderRadius: BorderRadius.circular(12),
                  borderSide: BorderSide(
                    color: widget.borderColor ?? AppColors.neutral4,
                    width: 1,
                  ),
                ),
            focusedBorder: widget.customBorder ??
                OutlineInputBorder(
                  borderRadius: BorderRadius.circular(12),
                  borderSide: BorderSide(
                    color: widget.borderColor ?? AppColors.neutral4,
                    width: 1,
                  ),
                ),
            errorBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(12),
              borderSide: BorderSide(
                color: widget.borderColor ?? AppColors.semantic6,
                width: 1,
              ),
            ),
            focusedErrorBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(12),
              borderSide: BorderSide(
                color: widget.borderColor ?? AppColors.semantic6,
                width: 1,
              ),
            ),
            disabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(12),
              borderSide: BorderSide(
                color: widget.borderColor ?? AppColors.neutral4,
                width: 1,
              ),
            ),
            hintText: widget.hint,
            hintStyle: StylesText.body6.copyWith(
              color: AppColors.neutral3,
            ),
            errorStyle: const TextStyle(fontSize: 0, height: 0),
            fillColor: (widget.disabled ?? false) ? AppColors.background1.withOpacity(0.1) : AppColors.white,
            filled: true,
          ),
        ),
        ValueListenableBuilder<String>(
          valueListenable: _errorMessage,
          builder: (context, message, _) {
            if (Validators.stringNotNullOrEmpty(message)) {
              return Padding(
                padding: const EdgeInsets.only(top: 0.0),
                child: Row(
                  children: [
                    SvgPicture.asset(AssetSVG.icWarning),
                    const SizedBox(
                      width: 4,
                    ),
                    Text(
                      message,
                      style: StylesText.caption3.copyWith(
                        color: AppColors.semantic6,
                      ),
                    ),
                  ],
                ),
              );
            } else {
              return const SizedBox.shrink();
            }
          },
        ),
      ],
    );
  }

  String? onValidator(String? value) {
    if (widget.validator != null) {
      _errorMessage.value = widget.validator!(value!) ?? '';
      return widget.validator!(value);
    } else {
      _errorMessage.value = '';
      return null;
    }
  }
}
