import 'package:flutter/material.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/app_style/app_text_style.dart';

class AppTextFormField extends StatefulWidget {
  final Function(String) onSaved;
  final Function(String) validator;
  final Function(String) onChange;
  final bool obscureText;
  final VoidCallback showPassword;
  final String hintText;
  final String labelText;
  final TextInputType textInputType;
  final String initialValue;
  final TextStyle style;
  final bool enable;
  final TextAlign textAlign;
  final FocusNode node;
  final Widget suffixIcon;
  final Widget prefixIcon;
  final IconData prefixIconData;
  final int prefixIconSize;
  final TextEditingController controller;
  final TextInputAction textInputAction;
  final VoidCallback onAction;
  final Color backgroundColor;
  final bool alignLabelWithHint;
  final bool showShadow;
  final void Function() onTap;

  const AppTextFormField({
    Key key,
    this.onSaved,
    this.validator,
    this.node,
    this.onChange,
    this.obscureText,
    this.showPassword,
    this.hintText,
    this.labelText,
    this.textInputType = TextInputType.text,
    this.initialValue,
    this.enable = true,
    this.style,
    this.textAlign,
    this.suffixIcon,
    this.prefixIcon,
    this.prefixIconData,
    this.prefixIconSize,
    this.controller,
    this.textInputAction,
    this.onAction,
    this.backgroundColor,
    this.showShadow = true,
    this.alignLabelWithHint,
    this.onTap,
  }) : super(key: key);

  @override
  State<AppTextFormField> createState() => _AppTextFormFieldState();
}

class _AppTextFormFieldState extends State<AppTextFormField> {
  @override
  void initState() {
    if (widget.controller != null) widget.controller.text = widget.initialValue;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final border = UnderlineInputBorder(borderSide: BorderSide.none, borderRadius: BorderRadius.circular(10));
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        boxShadow: widget.showShadow ? [BoxShadow(color: Colors.black.withOpacity(0.05), blurRadius: 10)] : null,
      ),
      child: TextFormField(
        focusNode: widget.node,
        enabled: widget.enable,
        onSaved: widget.onSaved,
        initialValue: widget.controller != null ? null : widget.initialValue,
        validator: widget.validator,
        onChanged: widget.onChange,
        obscureText: widget.obscureText ?? false,
        keyboardType: widget.textInputType,
        maxLines: widget.textInputType == TextInputType.multiline ? 4 : 1,
        obscuringCharacter: "*",
        controller: widget.controller,
        textInputAction: widget.textInputType == TextInputType.multiline ? null : widget.textInputAction ?? TextInputAction.next,
        onEditingComplete: widget.onAction ?? () => FocusScope.of(context).nextFocus(),
        style: AppTextStyle.body1?.copyWith(fontWeight: FontWeight.w500),
        onTap: widget.onTap,
        decoration: InputDecoration(
          alignLabelWithHint: widget.alignLabelWithHint,
          border: border,
          disabledBorder: border,
          focusedBorder: border,
          enabledBorder: border,
          errorBorder: border,
          contentPadding: EdgeInsets.symmetric(vertical: 8.toScreenHeight, horizontal: 16.toScreenWidth),
          constraints: const BoxConstraints(),
          suffixIconConstraints: const BoxConstraints(minWidth: 0),
          filled: true,
          fillColor: widget.backgroundColor ??
              (context.isDark && (widget.enable == false)
                  ? AppColor.neutral50
                  : (widget.enable == false)
                      ? AppColor.neutral40
                      : AppColor.background(context)),
          errorStyle: AppTextStyle.tiny.copyWith(color: context.isDark ? AppColor.red50 : AppColor.red60),
          floatingLabelStyle: AppTextStyle.body1?.copyWith(fontWeight: FontWeight.w500, color: context.isDark ? null : AppColor.neutral20),
          hintText: widget.hintText ?? "",
          labelText: widget.labelText ?? "",
          prefixIcon: widget.prefixIcon ??
              (widget.prefixIconData == null
                  ? null
                  : Icon(
                      widget.prefixIconData,
                      size: widget.prefixIconSize == null ? 20.toScreenWidth : (widget.prefixIconSize - 10).toScreenWidth,
                      color: AppColor.neutral70,
                    )),
          suffixIcon: widget.suffixIcon,
        ),
      ),
    );
  }
}
