import 'package:cargocalculaterapp/core/extension/build_context_extension.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';
import '../../theme/app_text_styles.dart';
import '../../theme/colors/app_colors.dart';
import '../../utils/app_utils.dart';

class CustomTextFieldName extends StatelessWidget {
  const CustomTextFieldName({
    super.key,
    this.name,
    required this.hint,
    this.readOnly = false,
    this.onTap,
    this.prefixWidget,
    this.controller,
    this.format,
    this.inputType,
    this.onchange,
    this.style,
    this.suffix,
    this.errorText,
    this.isError,
    this.isRequired = false,
    this.maxLines,
    this.minLines,
    this.inputStyle,
    this.textCapitalization = TextCapitalization.sentences,
    this.fillColor,
    this.obscureText = false,
  });

  final String? name;
  final String hint;
  final bool readOnly;
  final Function()? onTap;
  final Widget? prefixWidget;
  final TextEditingController? controller;
  final List<TextInputFormatter>? format;
  final TextInputType? inputType;
  final bool isRequired;
  final Function(String)? onchange;
  final TextStyle? style;
  final Widget? suffix;
  final String? errorText;
  final bool? isError;
  final int? maxLines;
  final int? minLines;
  final TextStyle? inputStyle;
  final TextCapitalization textCapitalization;
  final Color? fillColor;
  final bool obscureText;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            if (name?.isNotEmpty ?? false)
              Text(name ?? '', style: style ?? context.text.categoryName),
            AppUtils.kBoxWidth4,
            if (isRequired) SvgPicture.asset("assets/svg/ic_required.svg"),
          ],
        ),
        AppUtils.kBoxHeight8,
        TextField(
          style: inputStyle ?? TextStyle(color: context.color.textColor),
          keyboardType: inputType,
          controller: controller,
          onTap: onTap,
          readOnly: readOnly,
          onChanged: onchange,
          inputFormatters: format,
          maxLines: maxLines,
          minLines: minLines,
          obscureText: obscureText,
          textCapitalization: textCapitalization,
          decoration: InputDecoration(
            fillColor: fillColor,
            hintText: hint,
            prefixIcon: prefixWidget,
            suffixIcon: suffix,
            focusedBorder: OutlineInputBorder(
              borderRadius: AppUtils.kBorderRadius16,
              borderSide: BorderSide(
                color:
                    !(isError ?? false)
                        ? context.color.accentColor
                        : ThemeColors.timerRed,
              ),
            ),
          ),
        ),
        if (isError ?? false) AppUtils.kBoxHeight4,
        if (isError ?? false)
          Text(
            errorText ?? "",
            style: AppTextStyles.timer.copyWith(fontSize: 12),
          ),
      ],
    );
  }
}
