import 'package:flutter/material.dart';
import 'package:hoshan/core/utils/strings.dart';
import 'package:hoshan/data/model/edittext_state_model.dart';
import 'package:hoshan/ui/theme/colors.dart';
import 'package:hoshan/ui/theme/text.dart';

class FilledTextField extends StatefulWidget {
  final Function(String)? onChange;
  final String? Function(String?)? onValid;
  final EdittextStateModel? stateController;

  final String? hintText;

  const FilledTextField(
      {super.key,
      this.onChange,
      this.hintText,
      this.onValid,
      this.stateController});

  @override
  State<FilledTextField> createState() => _FilledTextFieldState();
}

class _FilledTextFieldState extends State<FilledTextField> {
  String text = '';
  bool resetError = false;

  @override
  Widget build(BuildContext context) {
    final defaultBorder = OutlineInputBorder(
        borderRadius: BorderRadius.circular(4),
        borderSide: BorderSide(color: AppColors.gray[600]));
    return Directionality(
      textDirection: TextDirection.rtl,
      child: Form(
        key: widget.stateController?.formState,
        child: TextFormField(
          controller: widget.stateController?.formController,
          validator: (value) {
            if (resetError) {
              resetError = false;
              return null;
            }
            return widget.onValid?.call(value);
          },
          textDirection:
              text.startsWithEnglish() ? TextDirection.ltr : TextDirection.rtl,
          style: AppTextStyles.body4
              .copyWith(color: Theme.of(context).colorScheme.onSurface),
          decoration: InputDecoration(
              border: defaultBorder,
              errorBorder: defaultBorder,
              enabledBorder: defaultBorder,
              // focusedBorder: defaultBorder,
              disabledBorder: defaultBorder,
              // focusedErrorBorder: defaultBorder,
              hintText: widget.hintText,
              fillColor: Theme.of(context).colorScheme.surface,
              filled: true,
              hintStyle:
                  AppTextStyles.body4.copyWith(color: AppColors.gray[700]),
              errorStyle: AppTextStyles.body4),
          onChanged: (value) {
            widget.onChange?.call(value);
            setState(() {
              text = value;
              resetError = true;
              widget.stateController?.formState.currentState?.validate();
            });
          },
        ),
      ),
    );
  }
}
