import 'package:flutter/material.dart';
import 'package:rluv/global/styles.dart';
import 'package:rluv/global/utils.dart';

class UiButton extends StatefulWidget {
  const UiButton({
    super.key,
    required this.text,
    this.color = Styles.deepPurpleNurple,
    required this.onPressed,
    this.height,
    this.width,
    this.icon,
    this.showLoading = false,
  });

  final String text;
  final Function? onPressed;
  final Color color;
  final double? width;
  final double? height;
  final Icon? icon;
  final bool showLoading;

  @override
  State<UiButton> createState() => _UiButtonState();
}

class _UiButtonState extends State<UiButton> {
  final double borderRadius = 12.0;
  bool loading = false;

  @override
  Widget build(BuildContext context) {
    final computedColor =
        widget.onPressed == null ? Styles.disabledButton : widget.color;
    final brightness = getBrightness(computedColor);
    return SizedBox(
      width: widget.width,
      height: widget.height,
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: InkWell(
          borderRadius: BorderRadius.circular(borderRadius),
          onTap: widget.onPressed == null || loading
              ? null
              : widget.showLoading
                  ? () async {
                      setState(() => loading = true);
                      await widget.onPressed!();
                      setState(() => loading = false);
                    }
                  : () => widget.onPressed!(),
          child: Container(
            decoration: BoxDecoration(
              color: computedColor,
              boxShadow: const [
                BoxShadow(
                  color: Colors.black26,
                  blurRadius: 2.0,
                  spreadRadius: 2.0,
                  offset: Offset(2.5, 2.5),
                )
              ],
              borderRadius: BorderRadius.circular(borderRadius),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: loading
                  ? [
                      Padding(
                        padding: const EdgeInsets.all(4.0),
                        child: SizedBox(
                            height: 26,
                            width: 26,
                            child: CircularProgressIndicator(
                                strokeWidth: 2.0,
                                color: brightness == Brightness.dark
                                    ? Styles.lavender
                                    : Styles.electricBlue)),
                      ),
                    ]
                  : [
                      if (widget.icon != null) widget.icon!,
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          widget.text,
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.w600,
                            color: widget.onPressed == null
                                ? Styles.disabledButtonText
                                : brightness == Brightness.dark
                                    ? Styles.washedStone
                                    : Colors.black87,
                          ),
                        ),
                      ),
                    ],
            ),
          ),
        ),
      ),
    );
  }
}
