import 'package:english_learning/core/utils/styles/theme.dart';
import 'package:flutter/material.dart';

class GlobalButton extends StatelessWidget {
  final String text;
  final VoidCallback? onPressed;
  final double? width;
  final double? height;
  final IconData? icon;
  final double? iconSize;
  final bool spaceBetween;
  final Gradient? gradient;
  final Color? textColor;
  final Color? backgroundColor;
  final Color? borderColor;
  final double borderWidth;
  final bool transparentBackground;
  final bool isLoading;

  const GlobalButton({
    super.key,
    required this.text,
    required this.onPressed,
    this.width = double.infinity,
    this.height = 49.0,
    this.icon,
    this.iconSize = 24.0,
    this.spaceBetween = false,
    this.gradient,
    this.textColor = Colors.white,
    this.backgroundColor,
    this.borderColor,
    this.borderWidth = 1.0,
    this.transparentBackground = false,
    this.isLoading = false,
  });

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: width,
      height: height,
      child: DecoratedBox(
        decoration: BoxDecoration(
          gradient: transparentBackground
              ? null
              : (gradient ??
                  (backgroundColor != null ? null : AppColors.gradientTheme)),
          color: gradient == null
              ? backgroundColor
              : null, // Use solid color if no gradient
          borderRadius: BorderRadius.circular(12),
          border: borderColor != null
              ? Border.all(color: borderColor!, width: borderWidth)
              : null,
        ),
        child: ElevatedButton(
          style: ElevatedButton.styleFrom(
            backgroundColor: Colors.transparent,
            shadowColor: Colors.transparent,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(30.0),
            ),
          ),
          onPressed: onPressed,
          child: isLoading ? _buildLoadingIndicator() : _buildButtonContent(),
        ),
      ),
    );
  }

  Widget _buildLoadingIndicator() {
    return SizedBox(
      width: 24,
      height: 24,
      child: CircularProgressIndicator(
        valueColor: AlwaysStoppedAnimation<Color>(textColor ?? Colors.white),
        strokeWidth: 2.0,
      ),
    );
  }

  Widget _buildButtonContent() {
    if (spaceBetween && icon != null) {
      return Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            text,
            style: AppTextStyles.whiteTextStyle.copyWith(
              fontSize: 14,
              fontWeight: FontWeight.bold,
              color: textColor,
            ),
          ),
          Icon(
            icon,
            size: iconSize,
            color: textColor ?? Colors.white,
          ),
        ],
      );
    } else {
      return Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            text,
            style: AppTextStyles.whiteTextStyle.copyWith(
              fontSize: 14,
              fontWeight: FontWeight.bold,
              color: textColor,
            ),
          ),
          if (icon != null)
            Padding(
              padding: const EdgeInsets.only(left: 8.0),
              child: Icon(
                icon,
                size: iconSize,
                color: textColor ?? Colors.white,
              ),
            ),
        ],
      );
    }
  }
}
