import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hoshan/core/utils/strings.dart';
import 'package:hoshan/ui/theme/colors.dart';
import 'package:hoshan/ui/theme/text.dart';
import 'package:hoshan/ui/widgets/components/snackbar/snackbar_manager.dart';

class AuthTextField extends StatefulWidget {
  final TextEditingController? controller;
  final Function(String)? onChange;
  final String? hintText;
  final int? maxLength;
  final String? label;
  final Widget? suffix;
  final Widget? error;
  final Widget? success;
  final bool isPassword;
  final bool enabled;
  final bool justEnglish;
  final int? minLines;
  final int maxLines;
  final TextInputAction textInputAction;
  const AuthTextField(
      {super.key,
      this.hintText,
      this.maxLength,
      this.label,
      this.suffix,
      this.error,
      this.isPassword = false,
      this.controller,
      this.onChange,
      this.minLines,
      this.textInputAction = TextInputAction.done,
      this.justEnglish = false,
      this.maxLines = 1,
      this.success,
      this.enabled = true});

  @override
  State<AuthTextField> createState() => _AuthTextFieldState();
}

class _AuthTextFieldState extends State<AuthTextField> {
  ValueNotifier<bool> isPassword = ValueNotifier(true);
  String text = '';

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder(
        valueListenable: isPassword,
        builder: (context, value, _) {
          return Directionality(
            textDirection: TextDirection.rtl,
            child: TextField(
              maxLength: widget.maxLength,
              obscureText: widget.isPassword && value,
              style: AppTextStyles.body5.copyWith(
                  color: widget.enabled
                      ? Theme.of(context).colorScheme.onSurface
                      : AppColors.gray[700]),
              controller: widget.controller,
              textInputAction: widget.minLines != null && widget.minLines! > 1
                  ? TextInputAction.newline
                  : widget.textInputAction,
              onChanged: (value) {
                setState(() {
                  text = value;
                });
                widget.onChange?.call(value);
              },
              inputFormatters: [
                TextInputFormatter.withFunction((oldValue, newValue) {
                  if (newValue.text.isEmpty) {
                    return newValue;
                  }
                  if (widget.justEnglish &&
                      !newValue.text.containsOnlyEnglish()) {
                    SnackBarManager(context, id: 'justTypeEnglish').show(
                      status: SnackBarStatus.info,
                      message: 'نام کاربری باید فقط شامل حروف انگلیسی باشد',
                    );

                    return oldValue;
                  }
                  return newValue;
                }),
              ],
              minLines: widget.minLines,
              maxLines: widget.maxLines,
              textDirection: text.startsWithEnglish()
                  ? TextDirection.ltr
                  : TextDirection.rtl,
              decoration: InputDecoration(
                alignLabelWithHint: true,
                hintText: widget.hintText,
                hintStyle: AppTextStyles.body5,
                contentPadding: const EdgeInsets.all(18),
                error: widget.error ?? widget.success,
                enabled: widget.enabled,
                labelStyle: AppTextStyles.body5
                    .copyWith(color: Theme.of(context).colorScheme.onSurface),
                floatingLabelStyle: AppTextStyles.body5
                    .copyWith(color: Theme.of(context).colorScheme.primary),
                labelText: widget.label,
                suffixIcon: widget.isPassword
                    ? GestureDetector(
                        child: Icon(
                          value ? CupertinoIcons.eye_slash : CupertinoIcons.eye,
                          color: widget.error != null
                              ? AppColors.red.defaultShade
                              : Theme.of(context).colorScheme.primary,
                        ),
                        onTap: () {
                          isPassword.value = !isPassword.value;
                        },
                      )
                    : widget.suffix,
                fillColor: Theme.of(context).colorScheme.primary,
                focusColor: Theme.of(context).colorScheme.primary,
                enabledBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(16),
                    borderSide: BorderSide(
                        color: Theme.of(context).colorScheme.primary)),
                border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(16),
                    borderSide: BorderSide(
                        color: Theme.of(context).colorScheme.primary)),
                focusedBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(16),
                    borderSide: BorderSide(
                        color: Theme.of(context).colorScheme.primary,
                        width: 2)),
                focusedErrorBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(16),
                    borderSide: BorderSide(
                        color: widget.success != null
                            ? AppColors.green.defaultShade
                            : AppColors.red.defaultShade,
                        width: 2)),
                errorBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(16),
                    borderSide: BorderSide(
                        color: widget.success != null
                            ? AppColors.green.defaultShade
                            : AppColors.red.defaultShade,
                        width: 2)),
              ),
            ),
          );
        });
  }
}
