import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:rasadyar_core/presentation/common/app_color.dart';
import 'package:rasadyar_core/presentation/common/app_fonts.dart';

class UnitTextField extends StatefulWidget {
  const UnitTextField({
    super.key,
    required this.hint,
    required this.unit,
    this.onChanged,
    this.initialValue,
    this.controller,
    this.keyboardType,
    this.textInputAction,
    this.enabled,
    this.readOnly,
    this.maxLines,
    this.minLines,
    this.textStyle,
    this.textColor,
    this.inputFormatters,
    this.validator
  });

  final String hint;
  final String unit;
  final TextStyle? textStyle;
  final Color? textColor;
  final ValueChanged<String>? onChanged;
  final String? initialValue;
  final TextEditingController? controller;
  final TextInputType? keyboardType;
  final TextInputAction? textInputAction;
  final List<TextInputFormatter>? inputFormatters;
  final FormFieldValidator<String>? validator;
  final bool? enabled;
  final bool? readOnly;
  final int? maxLines;
  final int? minLines;

  @override
  State<UnitTextField> createState() => _UnitTextFieldState();
}

class _UnitTextFieldState extends State<UnitTextField> {
  TextEditingController? tmpController;

  @override
  void initState() {
    super.initState();
    if (widget.controller == null) {
      tmpController = TextEditingController(text: widget.initialValue);
      if (widget.initialValue != null) {
        tmpController?.text = widget.initialValue!;
      }
    } else {
      tmpController = widget.controller;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 40,
      width: MediaQuery.of(context).size.width,
      padding: const EdgeInsets.symmetric(horizontal: 12),
      decoration: BoxDecoration(
        color: AppColor.bgLight,
        border: Border.all(color: AppColor.darkGreyLight),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            widget.hint,
            style:
            widget.textStyle?.copyWith(color: widget.textColor) ??
                AppFonts.yekan14.copyWith(color: widget.textColor ?? AppColor.textColorLight),
          ),

          const SizedBox(width: 8),

          Expanded(
            child: TextFormField(
              controller: tmpController,
              keyboardType: TextInputType.number,
              textInputAction: widget.textInputAction,
              onChanged: widget.onChanged,
              enabled: widget.enabled,
              readOnly: widget.readOnly ?? false,
              maxLines: 1,
              textAlign: TextAlign.center,
              textDirection: TextDirection.ltr,
              inputFormatters: widget.inputFormatters,
              validator: widget.validator,
              style: AppFonts.yekan18.copyWith(color: AppColor.darkGreyNormalActive),
              decoration: const InputDecoration(
                isDense: true,
                border: InputBorder.none,
                contentPadding: EdgeInsets.symmetric(vertical: 8),
              ),
            ),
          ),

          const SizedBox(width: 8),

          Text(
            widget.unit,
            style:
                widget.textStyle?.copyWith(color: widget.textColor) ??
                AppFonts.yekan14.copyWith(color: widget.textColor ?? AppColor.textColorLight),
          ),
        ],
      ),
    );
  }
}
