import 'package:boilerplate/core/styles/my_colors.dart';
import 'package:boilerplate/core/styles/my_dimensions.dart';
import 'package:boilerplate/core/styles/my_font.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class IPMTextField extends StatelessWidget {
  final TextEditingController? controller;
  final String? hintText;
  final Widget? suffixIcon;
  final Function(String)? onFieldSubmitted;
  final Color? borderColor;
  final TextStyle? hintStyle;
  final List<TextInputFormatter>? inputFormatters;
  final TextInputType? keyboardType;
  final String? Function(String?)? validator;
  final TextStyle? errorStyle;
  final Function(String)? onChanged;
  final double borderRadius;
  final Color? textColor;
  final bool readOnly;
  final VoidCallback? onTap;
  final TextAlign textAlign;
  final int? minLines;
  final int? maxLines;

  const IPMTextField({
    Key? key,
    this.textColor,
    this.controller,
    this.hintText,
    this.suffixIcon,
    this.onFieldSubmitted,
    this.borderColor,
    this.hintStyle,
    this.inputFormatters,
    this.keyboardType,
    this.validator,
    this.errorStyle,
    this.onChanged,
    this.borderRadius = 50,
    this.readOnly = false,
    this.onTap,
    this.textAlign = TextAlign.start,
    this.minLines,
    this.maxLines,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      child: Theme(
        data: Theme.of(context).copyWith(
          colorScheme: ThemeData().colorScheme.copyWith(
                primary: MyColor.orange,
              ),
        ),
        child: TextFormField(
          controller: controller,
          readOnly: readOnly,
          style: MyFont.regular14.copyWith(
            color: textColor,
          ),
          cursorColor: MyColor.orange,
          onFieldSubmitted: this.onFieldSubmitted,
          inputFormatters: this.inputFormatters,
          keyboardType: this.keyboardType,
          validator: this.validator,
          onChanged: this.onChanged,
          onTap: onTap,
          textAlign: textAlign,
          minLines: minLines,
          maxLines: maxLines,
          decoration: InputDecoration(
            suffixIcon: suffixIcon,
            hintText: this.hintText,
            hintStyle: this.hintStyle ??
                MyFont.regular14.copyWith(
                  color: MyColor.paragraphColor,
                ),
            isDense: true,
            filled: true,
            fillColor: Colors.white,
            contentPadding: EdgeInsets.symmetric(
              vertical: MyDimens.dimen16,
              horizontal: MyDimens.dimen16,
            ),
            border: _border(),
            enabledBorder: _border(),
            disabledBorder: _border(),
            focusedBorder: _border(),
            errorBorder: _border().copyWith(
              borderSide: BorderSide(color: Theme.of(context).errorColor),
            ),
            focusedErrorBorder: _border().copyWith(
              borderSide: BorderSide(color: Theme.of(context).errorColor),
            ),
            suffixIconColor: MyColor.orange,
            iconColor: MyColor.orange,
            focusColor: MyColor.orange,
            hoverColor: MyColor.orange,
            prefixIconColor: MyColor.orange,
            errorStyle: this.errorStyle,
          ),
        ),
      ),
    );
  }

  InputBorder _border() {
    return OutlineInputBorder(
      borderRadius: BorderRadius.circular(this.borderRadius),
      borderSide: BorderSide(color: borderColor ?? Colors.transparent),
    );
  }
}
