import 'package:flutter/material.dart';
import 'package:ui_kit/ui_kit.dart';

class InputWidget extends StatefulWidget {
  const InputWidget({
    super.key,
    this.label,
    required this.controller,
    required this.hintText,
    this.isError = 'false',
    this.errorText,
    this.isPassword = 'false',
  });
  final TextEditingController controller;
  final String? label;
  final String hintText;
  final String isError;
  final String? errorText;
  final String? isPassword;

  @override
  State<InputWidget> createState() => _InputWidgetState();
}

class _InputWidgetState extends State<InputWidget> {
  bool obscureText = false;
  @override
  void initState() {
    super.initState();
    widget.isPassword == 'true' ? obscureText = true : obscureText = false;
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        widget.label != null
            ? Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    widget.label ?? '',
                    style: captionRegular.copyWith(color: descColor),
                  ),
                  SizedBox(height: 4),
                ],
              )
            : SizedBox(),
        SizedBox(
          height: 48,
          child: TextFormField(
            controller: widget.controller,
            decoration: InputDecoration(
              fillColor: widget.isError == 'true' ? errorBgColor : inputBgColor,
              filled: true,
              contentPadding: EdgeInsets.symmetric(
                horizontal: 14,
                vertical: 14,
              ),
              hintText: widget.hintText,
              suffix: widget.isPassword == 'true'
                  ? Image.asset(
                      width: 20,
                      height: 20,
                      obscureText == true
                          ? 'assets/hidden.png'
                          : 'assets/hidden.png',
                    )
                  : null,
              hintStyle: textRegular.copyWith(color: captionColor),
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(10),
                borderSide: BorderSide(
                  color: widget.isError == 'true'
                      ? errorColor
                      : inputStrokeColor,
                  width: 1,
                ),
              ),
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(10),
                borderSide: BorderSide(color: primaryHoverColor, width: 1),
              ),
            ),
          ),
        ),
        widget.isError == 'true'
            ? Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(height: 8),
                  Text(
                    widget.errorText ?? "",
                    style: captionRegular.copyWith(color: errorColor),
                  ),
                ],
              )
            : SizedBox(),
      ],
    );
  }
}
