import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:tangheem/classes/colors.dart';
import 'package:tangheem/extensions/int_extensions.dart';
import 'package:tangheem/extensions/string_extensions.dart';
import 'package:tangheem/extensions/widget_extensions.dart';

class CommonTextFieldWidget extends StatefulWidget {
  final String hint;
  final TextEditingController controller;
  final bool isPassword;
  final bool isEmail;
  final String prefixIcon;
  final Widget suffixWidget;
  final Function onTap;
  final int maxLines;
  final FocusNode focusNode;
  final bool validate;
  final Color fillColor;
  final bool isBorder;

  CommonTextFieldWidget(
      {Key key,
      @required this.hint,
      @required this.controller,
      this.maxLines = 1,
      this.focusNode,
      this.isPassword = false,
      this.isEmail = false,
      this.prefixIcon,
      this.suffixWidget,
      this.onTap,
      this.fillColor,
      this.isBorder = false,
      this.validate = false})
      : super(key: key);

  @override
  _CommonTextFieldWidgetState createState() {
    return _CommonTextFieldWidgetState();
  }
}

class _CommonTextFieldWidgetState extends State<CommonTextFieldWidget> {
  bool _formValidate = false;

  @override
  void initState() {
    super.initState();
    if (widget.controller != null) {
      widget.controller.addListener(() {
        if (widget.isEmail) {
          if (_formValidate != widget.controller.text.isValidEmail()) {
            _formValidate = widget.controller.text.isValidEmail();
            setState(() {});
          }
        } else if (_formValidate != widget.controller.text.isNotEmpty) {
          _formValidate = widget.controller.text.isNotEmpty;
          setState(() {});
        }
      });
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: widget.fillColor ?? Color(0xffF8F7FB),
        borderRadius: BorderRadius.circular(30),
        border: Border.all(color: widget.isBorder ? Color(0xddddDDdd) : Colors.transparent, width: 1),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          TextField(
            textAlignVertical: TextAlignVertical.center,
            controller: widget.controller,
            obscureText: widget.isPassword,
            textInputAction: TextInputAction.next,
            style: TextStyle(color: ColorConsts.darkText, fontSize: 14, fontFamily: "Montserrat"),
            cursorColor: ColorConsts.primaryBlue,
            readOnly: widget.onTap != null,
            maxLines: widget.maxLines,
            onTap: widget.onTap,
            focusNode: widget.focusNode,
            scrollPhysics: BouncingScrollPhysics(),
            decoration: InputDecoration(
              contentPadding: EdgeInsets.fromLTRB(4, 16, 24, 16),
              alignLabelWithHint: true,
              fillColor: widget.fillColor ?? Color(0xffF8F7FB),
              filled: true,
              hintStyle: TextStyle(color: ColorConsts.greyHintColor, fontSize: 14, fontFamily: "Montserrat"),
              hintText: widget.hint,
              // suffixIcon: widget.suffixWidget == null
              //     ? Container(
              //         color: Colors.green,
              //         child: Stack(
              //           alignment: Alignment.topCenter,
              //           children: [
              //             (Icon(Icons.check_circle_outline_rounded, color: (widget.controller != null && _formValidate) ? ColorConsts.borderDark2Text : ColorConsts.redLightColor, size: 20)),
              //           ],
              //         ),
              //       )
              //     : Padding(
              //         padding: EdgeInsets.only(left: 24, right: 0),
              //         child: widget.suffixWidget,
              //       ),
              // suffixIconConstraints: widget.suffixWidget == null ? null : BoxConstraints(maxHeight: 30),
              // prefixIconConstraints: widget.prefixIcon == null ? widget.prefixIcon : BoxConstraints(maxHeight: 18),
              // prefixIcon: widget.prefixIcon == null
              //     ? widget.prefixIcon
              //     : Padding(
              //         padding: EdgeInsets.only(left: 4, right: 0),
              //         child: SvgPicture.asset(widget.prefixIcon, color: ColorConsts.textGrey2),
              //       ),
              //  border: InputBorder.none,
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(30),
                borderSide: BorderSide(color: Colors.transparent, width: 1),
              ),
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(30),
                borderSide: BorderSide(color: Colors.transparent, width: 1),
              ),
            ),
          ).expanded,
          (Icon(Icons.check_circle_outline_rounded, color: (widget.controller != null && _formValidate) ? ColorConsts.borderDark2Text : ColorConsts.redLightColor, size: 20))
              .paddingOnly(top: 14, left: 16),
        ],
      ),
    );
  }
}


