import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hmg_flutter_template/classes/colors.dart';
import 'package:hmg_flutter_template/extensions/int_extensions.dart';
import 'package:hmg_flutter_template/extensions/widget_extensions.dart';
import 'package:sizer/sizer.dart';

class CustomTextField extends StatefulWidget {
  final String labelText;
  final String? hintText;
  final TextEditingController controller;
  final VoidCallback? suffixTap;
  final IconData? suffixIcon;
  final bool isEnable;
  final bool hasSelection;
  final int? lines;
  final bool isInputTypeNum;
  final bool isTextIsPassword;
  final bool isBackgroundEnable;
  final bool isEnableBorder;
  final double verticalPadding;
  final double horizontalPadding;
  final Function(String)? onChange;
  final Function()? onEditComplete;
  final Function(String)? onSubmit;
  final Function? onClick;
  final FocusNode? focusNode;
  List<TextInputFormatter>? inputFormatters;

  CustomTextField(
    this.labelText,
    this.controller, {
    Key? key,
    this.isTextIsPassword = false,
    this.suffixTap,
    this.suffixIcon,
    this.hintText,
    this.isEnable = true,
    this.hasSelection = false,
    this.isEnableBorder = true,
    this.lines = 1,
    this.onChange,
    this.onEditComplete,
    this.onSubmit,
    this.onClick,
    this.isInputTypeNum = false,
    this.isBackgroundEnable = false,
    this.focusNode,
    this.verticalPadding = 15,
    this.horizontalPadding = 16,
    this.inputFormatters,
  }) : super(key: key);

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

class CustomTextFieldState extends State<CustomTextField> {
  late bool isObscureText;
  late FocusNode focusNode;

  @override
  void initState() {
    super.initState();
    focusNode = FocusNode();
    isObscureText = widget.isTextIsPassword;
  }

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

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        focusNode.requestFocus();
        if (widget.hasSelection) widget.onClick!();
      },
      child: Container(
        padding: EdgeInsets.only(left: widget.horizontalPadding, right: widget.horizontalPadding, bottom: widget.verticalPadding, top: widget.verticalPadding),
        alignment: Alignment.center,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(15),
          color: widget.isBackgroundEnable ? const Color(0xffF7F7F7) : Colors.white,
          border: Border.all(color: widget.isEnableBorder ? Colors.grey.shade300 : Colors.transparent, width: 1),
        ),
        child: Row(
          children: [
            Expanded(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Text(
                    widget.labelText,
                    style: TextStyle(
                      fontSize: 9.sp,
                      fontWeight: FontWeight.w600,
                      color: const Color(0xff2B353E),
                      letterSpacing: -0.44,
                    ),
                  ),
                  TextField(
                    focusNode: focusNode,
                    autofocus: false,
                    enabled: widget.isEnable,
                    scrollPadding: EdgeInsets.zero,
                    keyboardType: widget.isInputTypeNum ? TextInputType.number : TextInputType.text,
                    controller: widget.controller,
                    maxLines: widget.lines,
                    obscuringCharacter: "*",
                    obscureText: isObscureText,
                    onChanged: widget.onChange,
                    onEditingComplete: widget.onEditComplete,
                    onSubmitted: widget.onSubmit,
                    inputFormatters: widget.inputFormatters,
                    style: const TextStyle(
                      fontSize: 14,
                      height: 21 / 14,
                      fontWeight: FontWeight.w400,
                      color: Color(0xff2B353E),
                      letterSpacing: -0.44,
                    ),
                    decoration: InputDecoration(
                      isDense: true,
                      hintText: widget.hintText,
                      hintStyle: const TextStyle(
                        fontSize: 14,
                        height: 21 / 14,
                        fontWeight: FontWeight.w400,
                        color: Color(0xff575757),
                        letterSpacing: -0.56,
                      ),
                      suffixIconConstraints: const BoxConstraints(minWidth: 50),
                      suffixIcon: widget.suffixTap == null ? null : IconButton(icon: Icon(Icons.mic, color: MyColors.darkTextColor), onPressed: widget.suffixTap),
                      contentPadding: EdgeInsets.zero,
                      border: InputBorder.none,
                      focusedBorder: InputBorder.none,
                      enabledBorder: InputBorder.none,
                    ),
                  ),
                ],
              ),
            ),
            if (widget.isTextIsPassword) ...[
              16.width,
              Icon(isObscureText ? Icons.visibility_rounded : Icons.visibility_off_rounded).onPress(() {
                setState(() {
                  isObscureText = !isObscureText;
                });
              })
            ],
            if (widget.hasSelection) const Icon(Icons.keyboard_arrow_down_outlined),
            if (widget.suffixIcon != null && widget.suffixTap == null) Icon(widget.suffixIcon!),
          ],
        ),
      ),
    );
  }
}
