import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:frontend_eccp_mobile/app/core/constants/constants.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

class AppShadInput extends StatefulWidget {
  const AppShadInput({
    required this.title,
    required this.hint,
    super.key,
    this.showTitle = true,
    this.isRequired = false,
    this.isEnabled = true,
    this.width,
    this.isPassword = false,
    this.maxLines = 1,
    this.maxLength,
    this.keyboardType,
    this.inputFormatters,
    this.onChanged,
    this.leading,
    this.trailing,
    this.focusNode,
    this.textInputAction,
    this.autofocus = false,
    this.readOnly = false,
    this.padding,
    this.inputPadding = const EdgeInsets.symmetric(vertical: 7, horizontal: 5),
    this.bgColor = Colors.white,
    this.decoration,
    this.controller,
    this.isForm = false,
    this.id,
    this.validator,
    this.description,
    this.radius,
  });

  final String title;
  final String hint;

  final bool showTitle;
  final bool isRequired;
  final bool isEnabled;
  final double? width;
  final double? radius;

  final bool isPassword;
  final int maxLines;
  final int? maxLength;

  final TextInputType? keyboardType;
  final List<TextInputFormatter>? inputFormatters;
  final void Function(String)? onChanged;

  final Widget? leading;
  final Widget? trailing;

  final FocusNode? focusNode;
  final TextInputAction? textInputAction;
  final bool autofocus;
  final bool readOnly;

  final EdgeInsetsGeometry? padding;
  final EdgeInsetsGeometry inputPadding;
  final Color bgColor;
  final ShadDecoration? decoration;

  final TextEditingController? controller;

  final bool isForm;
  final String? id;
  final String? Function(String value)? validator;
  final Widget? description;

  @override
  State<AppShadInput> createState() => _AppShadInputState();
}

class _AppShadInputState extends State<AppShadInput> {
  bool _obscure = true;

  @override
  void initState() {
    super.initState();

    assert(
      widget.isForm ? widget.id != null : widget.controller != null,
      'Jika isForm=true maka id wajib diisi, jika isForm=false maka controller wajib diisi',
    );
  }

  ShadDecoration get _decoration =>
      widget.decoration ??
      ShadDecoration(
        color: widget.bgColor,
        border: ShadBorder.all(
          color: AppColors.neutral200,
          radius: BorderRadius.circular(widget.radius ?? AppSizes.s12),
        ),
        secondaryFocusedBorder: ShadBorder.all(
          color: AppColors.neutral700,
          width: 2,
          radius: BorderRadius.circular(
            (widget.radius ?? AppSizes.s12) + 4,
          ),
        ),
      );

  @override
  Widget build(BuildContext context) {
    final trailing = widget.isPassword
        ? ShadIconButton(
            width: 24,
            height: 24,
            backgroundColor: AppColors.neutral200,
            pressedBackgroundColor: AppColors.neutral200,
            icon: Icon(
              _obscure ? LucideIcons.eyeOff : LucideIcons.eye,
              size: 18,
              color: AppColors.neutral600,
            ),
            onPressed: () {
              setState(() => _obscure = !_obscure);
            },
          )
        : widget.trailing;

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (widget.showTitle)
          Padding(
            padding: const EdgeInsets.only(bottom: AppSizes.s12),
            child: Row(
              children: [
                Text(
                  widget.title,
                  style: AppTextStyles.label.copyWith(
                    color: AppColors.supportSteel600,
                  ),
                ),
                if (widget.isRequired)
                  const Text(
                    ' *',
                    style: TextStyle(
                      fontSize: 16,
                      color: Colors.red,
                    ),
                  ),
              ],
            ),
          ),
        SizedBox(
          width: widget.width,
          child: widget.isForm
              ? ShadInputFormField(
                  id: widget.id,
                  enabled: widget.isEnabled,
                  validator: widget.validator,
                  description: widget.description,
                  placeholder: Text(
                    widget.hint,
                    style: AppTextStyles.body.copyWith(
                      color: AppColors.neutral900.withOpacity(0.5),
                    ),
                  ),
                  decoration: _decoration,
                  obscureText: widget.isPassword && _obscure,
                  maxLines: widget.maxLines,
                  maxLength: widget.maxLength,
                  keyboardType: widget.keyboardType,
                  inputFormatters: widget.inputFormatters,
                  onChanged: widget.onChanged,
                  leading: widget.leading,
                  onPressedOutside: (event) => FocusScope.of(context).unfocus(),
                  trailing: trailing,
                  inputPadding: widget.inputPadding,
                  style: AppTextStyles.body,
                )
              : ShadInput(
                  controller: widget.controller,
                  enabled: widget.isEnabled,
                  focusNode: widget.focusNode,
                  readOnly: widget.readOnly,
                  autofocus: widget.autofocus,
                  keyboardType: widget.keyboardType,
                  textInputAction: widget.textInputAction,
                  maxLines: widget.maxLines,
                  maxLength: widget.maxLength,
                  inputFormatters: widget.inputFormatters,
                  obscureText: widget.isPassword && _obscure,
                  onChanged: widget.onChanged,
                  padding: widget.padding,
                  inputPadding: widget.inputPadding,
                  onPressedOutside: (event) => FocusScope.of(context).unfocus(),
                  decoration: _decoration,
                  placeholder: Text(
                    widget.hint,
                    style: AppTextStyles.body.copyWith(
                      color: AppColors.neutral900.withOpacity(0.5),
                    ),
                  ),
                  style: AppTextStyles.body,
                  leading: widget.leading,
                  trailing: trailing,
                ),
        ),
      ],
    );
  }
}
