import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class CustomTextField extends StatefulWidget{
  bool? showPassword;
  final bool numeric;
  final String hint;
  final String label;
  final int? lines;
  final ValueChanged<String>? onChanged;
  final int? length;

  CustomTextField({
    super.key,
    this.showPassword,
    this.numeric = false,
    this.hint = '',
    this.label = '',
    this.lines,
    this.length,
    this.onChanged,
  });

  @override
  State<CustomTextField> createState() => CustomTextFieldState();
}

class CustomTextFieldState extends State<CustomTextField>{

  @override
  Widget build(BuildContext context) {

    return Column(
      spacing: 8,
      children: [
          ?widget.label == '' ? null : Align(
          alignment: Alignment.bottomLeft,
          child: Text(
            widget.label,
            style: TextStyle(fontSize: 14, letterSpacing: 0),
          )
        ),
        TextFormField(
          keyboardType: widget.numeric? TextInputType.number : TextInputType.text,
          obscureText: !(widget.showPassword ?? true),
          enableSuggestions: widget.showPassword ?? true,
          autocorrect: !(widget.showPassword ?? false),
          style: TextStyle(color: Color(0xFF4B4B4B)),
          inputFormatters: widget.numeric? [
           FilteringTextInputFormatter.digitsOnly
          ] : [],
          decoration: InputDecoration(
            counterText: "",
            hintText: widget.hint,
            //labelText: widget.label,
            //floatingLabelBehavior: FloatingLabelBehavior.always,
            border: OutlineInputBorder(
              borderRadius: BorderRadius.all(Radius.circular(12)),
              borderSide: BorderSide(color: Color(0xFF4B4B4B)),
              gapPadding: 16
            ),
            suffixIcon: widget.showPassword!=null ? IconButton(
              icon: Icon(widget.showPassword!
              ? Icons.visibility_off
                : Icons.visibility),
              onPressed: () {
                setState(() {
                  widget.showPassword = !widget.showPassword!;
                });
              },
            ) : null,
          ),
          minLines: widget.lines ?? 1,
          maxLines: widget.lines ?? 1,
          maxLength: widget.length,
          onChanged: widget.onChanged ?? (_)=>{},
        )
      ],
    );
  }
}