part of 'button.dart';

enum ButtonStyle { filled, outlined }

class AppElevatedButton extends StatelessWidget {
  const AppElevatedButton.filled({
    super.key,
    required this.onPressed,
    required this.label,
    this.style = ButtonStyle.filled,
    this.color = AppColor.primary,
    this.textColor = Colors.white,
    this.width,
    this.height = 40.0,
    this.borderRadius = 16.0,
    this.icon,
    this.disabled = false,
    this.fontSize = 16.0,
    this.elevation,
    this.labelStyle,
    this.mainAxisAlignment = MainAxisAlignment.center,
    this.crossAxisAlignment = CrossAxisAlignment.center,
    this.isLoading = false,
  });

  const AppElevatedButton.outlined({
    super.key,
    required this.onPressed,
    required this.label,
    this.style = ButtonStyle.outlined,
    this.color = Colors.transparent,
    this.textColor = AppColor.primary,
    this.width,
    this.height = 40.0,
    this.borderRadius = 16.0,
    this.icon,
    this.disabled = false,
    this.fontSize = 16.0,
    this.elevation,
    this.labelStyle,
    this.mainAxisAlignment = MainAxisAlignment.center,
    this.crossAxisAlignment = CrossAxisAlignment.center,
    this.isLoading = false,
  });

  final Function()? onPressed;
  final String label;
  final ButtonStyle style;
  final Color color;
  final Color textColor;
  final double? width;
  final double height;
  final double borderRadius;
  final double? elevation;
  final Widget? icon;
  final bool disabled;
  final double fontSize;
  final TextStyle? labelStyle;
  final MainAxisAlignment mainAxisAlignment;
  final CrossAxisAlignment crossAxisAlignment;
  final bool isLoading;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: height,
      width: width,
      child: style == ButtonStyle.filled
          ? ElevatedButton(
              onPressed: disabled ? null : onPressed,
              style: ElevatedButton.styleFrom(
                backgroundColor: color,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(borderRadius),
                ),
                elevation: elevation,
                padding: const EdgeInsets.symmetric(horizontal: 16.0),
              ),
              child: isLoading
                  ? Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        SpinKitFadingCircle(color: textColor, size: fontSize),
                        const SpaceWidth(10.0),
                        Text(
                          'Loading...',
                          style:
                              labelStyle ??
                              TextStyle(
                                color: disabled ? Colors.grey : textColor,
                                fontSize: fontSize,
                                fontWeight: FontWeight.bold,
                              ),
                        ),
                      ],
                    )
                  : Row(
                      mainAxisAlignment: mainAxisAlignment,
                      crossAxisAlignment: crossAxisAlignment,
                      children: [
                        icon ?? const SizedBox.shrink(),
                        if (icon != null) const SizedBox(width: 10.0),
                        Flexible(
                          child: FittedBox(
                            fit: BoxFit.scaleDown,
                            child: Text(
                              label,
                              style:
                                  labelStyle ??
                                  TextStyle(
                                    color: disabled ? Colors.grey : textColor,
                                    fontSize: fontSize,
                                    fontWeight: FontWeight.bold,
                                  ),
                              textAlign: TextAlign.center,
                            ),
                          ),
                        ),
                      ],
                    ),
            )
          : OutlinedButton(
              onPressed: disabled ? null : onPressed,
              style: OutlinedButton.styleFrom(
                backgroundColor: color,
                side: const BorderSide(color: AppColor.primary),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(borderRadius),
                ),
                padding: const EdgeInsets.symmetric(horizontal: 16.0),
              ),
              child: isLoading
                  ? Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        SpinKitFadingCircle(color: textColor, size: fontSize),
                        const SpaceWidth(10.0),
                        Text(
                          'Loading...',
                          style:
                              labelStyle ??
                              TextStyle(
                                color: disabled ? Colors.grey : textColor,
                                fontSize: fontSize,
                                fontWeight: FontWeight.bold,
                              ),
                        ),
                      ],
                    )
                  : Row(
                      mainAxisAlignment: mainAxisAlignment,
                      crossAxisAlignment: crossAxisAlignment,
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        icon ?? const SizedBox.shrink(),
                        if (icon != null) const SizedBox(width: 10.0),
                        Flexible(
                          child: FittedBox(
                            fit: BoxFit.scaleDown,
                            child: Text(
                              label,
                              style:
                                  labelStyle ??
                                  TextStyle(
                                    color: disabled ? Colors.grey : textColor,
                                    fontSize: fontSize,
                                    fontWeight: FontWeight.w600,
                                  ),
                              textAlign: TextAlign.center,
                            ),
                          ),
                        ),
                      ],
                    ),
            ),
    );
  }
}
