import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'app_texts_widget.dart';

class AppTextFieldCustom extends StatefulWidget {
  final double height;
  final Function onClick;
  final String hintText;
  final TextEditingController controller;
  final bool isDropDown;
  final String dropDownText;
  final Icon suffixIcon;
  final Color dropDownColor;
  final bool enabled;
  final TextInputType inputType;
  final int minLines;
  final int maxLines;
  final List<TextInputFormatter> inputFormatters;

  AppTextFieldCustom({
    this.height = 0,
    this.onClick,
    this.hintText,
    this.controller,
    this.isDropDown = false,
    this.dropDownText,
    this.suffixIcon,
    this.dropDownColor,
    this.enabled = true,
    this.inputType = TextInputType.text,
    this.minLines = 1,
    this.maxLines = 1,
    this.inputFormatters,
  });

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

class _AppTextFieldCustomState extends State<AppTextFieldCustom> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: widget.height != 0 ? widget.height : null,
      decoration:
          containerBorderDecoration(Color(0Xffffffff), Color(0xFFEFEFEF)),
      padding: EdgeInsets.only(top: 4.0, bottom: 4.0, left: 8.0, right: 8.0),
      child: InkWell(
        onTap: widget.onClick ?? null,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  if ((widget.controller != null &&
                          widget.controller.text != "") ||
                      widget.dropDownText != null)
                    AppText(
                      widget.hintText,
                      fontFamily: 'Poppins',
                      fontSize: SizeConfig.textMultiplier * 1.4,
                      fontWeight: FontWeight.w600,
                    ),
                  widget.dropDownText == null
                      ? TextField(
                          textAlign: TextAlign.left,
                          decoration: textFieldSelectorDecoration(
                              widget.hintText, null, true),
                          style: TextStyle(
                            fontSize: SizeConfig.textMultiplier * 1.7,
                            fontFamily: 'Poppins',
                            color: Colors.grey.shade800,
                          ),
                          controller: widget.controller,
                          keyboardType: widget.inputType,
                          enabled: widget.enabled,
                          minLines: widget.minLines,
                          maxLines: widget.maxLines,
                          inputFormatters: widget.inputFormatters != null ? widget.inputFormatters : [],
                        )
                      : AppText(
                          widget.dropDownText,
                          fontFamily: 'Poppins',
                          color: Colors.grey.shade800,
                          fontSize: SizeConfig.textMultiplier * 1.7,
                        ),
                ],
              ),
            ),
            widget.isDropDown
                ? widget.suffixIcon != null
                    ? widget.suffixIcon
                    : Icon(
                        Icons.keyboard_arrow_down,
                        color: widget.dropDownColor != null
                            ? widget.dropDownColor
                            : Colors.black,
                      )
                : Container(),
          ],
        ),
      ),
    );
  }

  BoxDecoration containerBorderDecoration(
      Color containerColor, Color borderColor,
      {double borderWidth = -1}) {
    return BoxDecoration(
      color: containerColor,
      shape: BoxShape.rectangle,
      borderRadius: BorderRadius.all(Radius.circular(12)),
      border: Border.fromBorderSide(BorderSide(
        color: borderColor,
        width: borderWidth == -1 ? 2.0 : borderWidth,
      )),
    );
  }

  static InputDecoration textFieldSelectorDecoration(
      String hintText, String selectedText, bool isDropDown,
      {Icon suffixIcon, Color dropDownColor}) {
    return InputDecoration(
      isDense: true,
      contentPadding: EdgeInsets.symmetric(horizontal: 0, vertical: 0),
      enabledBorder: UnderlineInputBorder(
        borderSide: BorderSide(color: Color(0Xffffffff)),
      ),
      disabledBorder: UnderlineInputBorder(
        borderSide: BorderSide(color: Color(0Xffffffff)),
      ),
      focusedBorder: UnderlineInputBorder(
        borderSide: BorderSide(color: Color(0Xffffffff)),
      ),
      border: UnderlineInputBorder(
        borderSide: BorderSide(color: Color(0Xffffffff)),
      ),
      /*focusedBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Color(0xFFCCCCCC), width: 2.0),
        borderRadius: BorderRadius.circular(8),
      ),
      enabledBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Color(0xFFCCCCCC), width: 2.0),
        borderRadius: BorderRadius.circular(8),
      ),
      disabledBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Color(0xFFCCCCCC), width: 2.0),
        borderRadius: BorderRadius.circular(8),
      ),*/
      hintText: selectedText != null ? selectedText : hintText,
      hintStyle: TextStyle(
        fontSize: 14,
        color: Colors.grey.shade600,
      ),
      /*suffixIcon: isDropDown
          ? suffixIcon != null
          ? suffixIcon
          : Icon(
        Icons.arrow_drop_down,
        color: dropDownColor != null ? dropDownColor : Colors.black,
      )
          : null,*/
      // labelText:
      // labelStyle:
    );
  }
}
