import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/extensions/int_extensions.dart';
import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/utils/utils.dart';
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';
import 'package:sizer/sizer.dart';

class TxtField extends StatelessWidget {
  TextEditingController controller = TextEditingController();
  String? value;
  String? hint;
  String? lable;
  Color postFixDataColor;
  IconData? prefixData;
  IconData? postfixData;
  bool isNeedFilterButton;
  bool isNeedClickAll;
  bool isButtonEnable;
  final String errorValue;
  double? elevation;
  VoidCallback? onTap;
  String? buttonTitle;
  int? maxLines;
  bool isSidePaddingZero;
  bool isNeedBorder;
  bool isSearchBar;
  bool? isPasswordEnabled;
  Function(String)? onChanged;
  Function(String)? onSubmitted;
  Function()? onPostFixPressed;
  TextInputType? keyboardType;
  bool isBackgroundEnabled = false;
  Widget? postfixWidget;
  Widget? preFixWidget;
  bool numbersOnly;
  bool allowOnlyOneDigit;

  TxtField({
    Key? key,
    this.value,
    this.lable,
    this.hint,
    this.prefixData,
    this.postfixData,
    this.isNeedClickAll = false,
    this.postFixDataColor = Colors.white,
    this.errorValue = "",
    this.isNeedFilterButton = false,
    this.elevation,
    this.onTap,
    this.isButtonEnable = false,
    this.buttonTitle,
    this.maxLines,
    this.isSidePaddingZero = false,
    this.isNeedBorder = true,
    this.onChanged,
    this.onSubmitted,
    this.isPasswordEnabled,
    this.isSearchBar = false,
    this.keyboardType,
    this.isBackgroundEnabled = false,
    this.postfixWidget,
    this.preFixWidget,
    this.onPostFixPressed,
    this.numbersOnly = false,
    this.allowOnlyOneDigit = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    controller.text = value ?? "";
    controller.selection = TextSelection.fromPosition(TextPosition(offset: controller.text.length));

    return Column(
      children: [
        InkWell(
          onTap: isNeedClickAll == false
              ? null
              : () {
                  onTap!();
                },
          customBorder: Utils.inkWellCorner(),
          child: Row(
            children: [
              Expanded(
                child: Container(
                  decoration: BoxDecoration(
                    color: isBackgroundEnabled ? MyColors.textFieldColor : Colors.white,
                    borderRadius: const BorderRadius.all(Radius.circular(0)),
                  ),
                  child: TextField(
                    style: const TextStyle(color: MyColors.darkTextColor, fontSize: 15, fontWeight: MyFonts.Medium),
                    textInputAction: isSearchBar ? TextInputAction.search : null,
                    keyboardType: keyboardType,
                    autofocus: false,
                    controller: controller,
                    enabled: isNeedClickAll == true ? false : true,
                    maxLines: maxLines,
                    onTap: () {},
                    obscureText: isPasswordEnabled ?? false,
                    onChanged: onChanged,
                    onSubmitted: onSubmitted,
                    inputFormatters: allowOnlyOneDigit && numbersOnly
                        ? <TextInputFormatter>[FilteringTextInputFormatter.allow(RegExp('[0-9]')), LengthLimitingTextInputFormatter(1)]
                        : numbersOnly
                            ? <TextInputFormatter>[FilteringTextInputFormatter.allow(RegExp('[0-9]'))]
                            : [],
                    decoration: InputDecoration(
                      labelText: lable,
                      alignLabelWithHint: true,
                      fillColor: Colors.white,
                      focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: isNeedBorder ? MyColors.darkPrimaryColor : Colors.transparent, width: isNeedBorder ? 2.0 : 0),
                        borderRadius: BorderRadius.circular(0.0),
                      ),
                      enabledBorder: OutlineInputBorder(
                        // borderSide: BorderSide(color: MyColors.textFieldColor, width: isNeedBorder ? 1.0 : 0),
                        borderSide: BorderSide(color: isNeedBorder ? MyColors.darkPrimaryColor : Colors.transparent, width: isNeedBorder ? 2.0 : 0),
                        borderRadius: BorderRadius.circular(0.0),
                      ),
                      disabledBorder: OutlineInputBorder(
                        // borderSide: BorderSide(color: MyColors.textFieldColor, width: isNeedBorder ? 1.0 : 0),
                        borderSide: BorderSide(color: isNeedBorder ? MyColors.darkPrimaryColor : Colors.transparent, width: isNeedBorder ? 2.0 : 0),
                        borderRadius: BorderRadius.circular(0.0),
                      ),
                      suffixIcon: postfixData != null
                          ? InkWell(
                              onTap: onPostFixPressed,
                              child: Icon(postfixData, color: postFixDataColor),
                            )
                          : postfixWidget,
                      prefixIcon: prefixData != null ? Icon(prefixData, color: borderColor) : preFixWidget,
                      labelStyle: const TextStyle(color: borderColor, fontSize: 15, fontWeight: MyFonts.Medium),
                      hintStyle: const TextStyle(color: borderColor, fontSize: 15, fontWeight: MyFonts.Medium),
                      hintText: hint ?? "",
                      contentPadding: prefixData == null
                          ? EdgeInsets.only(
                              left: 12,
                              right: 12,
                              top: maxLines != null ? 12 : 0,
                              bottom: maxLines != null ? 12 : 0,
                            )
                          : EdgeInsets.zero,
                    ),
                  ),
                ),
              ),
              if (isNeedFilterButton) 8.width,
              if (isNeedFilterButton)
                InkWell(
                  onTap: isNeedClickAll
                      ? null
                      : () {
                          controller.clear();
                        },
                  child: SizedBox(
                    width: 55,
                    height: 55,
                    child: Card(
                      color: accentColor,
                      // margin: EdgeInsets.all(4),
                      // shape: cardRadius(0),
                      child: Icon(
                        postfixData ?? Icons.filter_alt,
                        color: postFixDataColor,
                      ),
                    ),
                  ),
                ),
              if (isButtonEnable)
                Material(
                  child: InkWell(
                    onTap: onTap,
                    customBorder: Utils.inkWellCorner(),
                    child: SizedBox(
                      height: 55,
                      child: Card(
                        color: accentColor,
                        // margin: EdgeInsets.all(4),
                        // shape: cardRadius(0),
                        child: Center(
                          child: Padding(
                            padding: const EdgeInsets.only(left: 12, right: 12),
                            child: (buttonTitle ?? "Search").toText(
                              color: Colors.white,
                              fontSize: 16,
                              letterSpacing: -0.64,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
            ],
          ),
        ),
        if (errorValue != "")
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              errorValue.toText(fontSize: 12, color: Colors.red),
            ],
          ).paddingOnly(right: 10),
      ],
    );
  }
}
