import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../app.dart';

class MyTextFormField extends StatelessWidget {
  final TextEditingController controller;
  final String? label;
  final TextInputType? keyboardType;
  final int maxLines;
  final bool obscureText;
  final bool isPassword;
  final bool hasPrefixIcon;
  final List<TextInputFormatter>? inputFormatters;
  final Widget? suffix;
  final String? prefixTxt;
  final Function(String value)? onChangeCallback;

  MyTextFormField({
    required this.controller,
    this.label,
    this.keyboardType = TextInputType.text,
    this.maxLines = 1,
    this.obscureText = false,
    this.isPassword = false,
    this.hasPrefixIcon = false,
    this.inputFormatters,
    this.suffix,
    this.prefixTxt,
    this.onChangeCallback,
  });

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      inputFormatters: inputFormatters,
      obscureText: this.obscureText,
      controller: this.controller,
      maxLines: this.maxLines,
      cursorColor: ThemeColor.cursorColor,
      keyboardType: keyboardType,
      textInputAction: TextInputAction.done,
      decoration: inputDecoration(),
      validator: (value) {
        if (value == null || value.trim().length == 0) return '$label ' + 'is_required'.tr;
        if (TextInputType.phone == keyboardType) {
          // validate phone
          final phone = value.replaceAll(RegExp(r'\D+'), '');
          return phone.length != 8 ? 'invalid_phone_number'.tr : null;
        }

        if (isPassword) {
          return value.length < 6 ? 'pwd_validation'.tr : null;
        }
        return null;
      },
      // onChanged: (value) => onChangeCallback!(value),
    );
  }

  InputDecoration inputDecoration() {
    final txtStyle = new TextStyle(
      color: ThemeColor.black.withOpacity(0.60),
      fontSize: 14.sp,
    );
    return InputDecoration(
      isDense: hasPrefixIcon,
      prefixIcon: hasPrefixIcon
          ? Container(
              padding: const EdgeInsets.only(left: 0.85),
              child: Container(
                decoration: new BoxDecoration(
                  color: Color(0XFFE5E5E5),
                  borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(8),
                    topLeft: Radius.circular(8),
                  ),
                ),
                padding: EdgeInsets.only(top: 15, bottom: 15, left: 15, right: 15),
                child: Text(
                  '+993',
                  style: TextStyle(
                    fontWeight: FontWeight.w700,
                    fontSize: 14.sp,
                  ),
                ),
              ),
            )
          : null,
      prefixText: this.prefixTxt,
      prefixStyle: txtStyle,
      // contentPadding: suffix != null ? EdgeInsets.only(left: 12, top: 5, bottom: 5) : null,
      // isDense: true,
      // isCollapsed: ,
      alignLabelWithHint: maxLines != 1,
      hintText: this.label,
      hintStyle: txtStyle,
      // labelText: this.label,
      // labelStyle: txtStyle,
      suffixIcon: this.suffix,
      suffixIconConstraints: BoxConstraints(),
      // floatingLabelBehavior: FloatingLabelBehavior.never,
      border: _inputBorder,
      focusedBorder: _inputBorder,
      enabledBorder: _inputBorder,
      disabledBorder: _inputBorder,
    );
  }

  final InputBorder _inputBorder = new OutlineInputBorder(
    borderRadius: new BorderRadius.circular(8.0),
    borderSide: new BorderSide(color: ThemeColor.grey.withOpacity(0.40)),
  );
}
