import 'package:flutter/material.dart';
import '../../core/theme/app_theme.dart';

/// 通用按钮组件 - 明亮工业风格
class CommonButton extends StatelessWidget {
  final String text;
  final VoidCallback? onPressed;
  final bool enabled;
  final Color? backgroundColor;
  final Color? textColor;
  final IconData? icon;
  final bool isLoading;
  final ButtonType type;

  const CommonButton({
    super.key,
    required this.text,
    this.onPressed,
    this.enabled = true,
    this.backgroundColor,
    this.textColor,
    this.icon,
    this.isLoading = false,
    this.type = ButtonType.primary,
  });

  @override
  Widget build(BuildContext context) {
    final bgColor = backgroundColor ?? _getDefaultBackgroundColor();
    final fgColor = textColor ?? _getDefaultTextColor();

    Widget content;
    if (isLoading) {
      content = Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(
            width: 16,
            height: 16,
            child: CircularProgressIndicator(
              strokeWidth: 2,
              color: fgColor,
            ),
          ),
          const SizedBox(width: 8),
          Text(text, style: TextStyle(fontWeight: FontWeight.w500)),
        ],
      );
    } else if (icon != null) {
      content = Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(icon, size: 18),
          const SizedBox(width: 8),
          Text(text, style: TextStyle(fontWeight: FontWeight.w500)),
        ],
      );
    } else {
      content = Text(text, style: TextStyle(fontWeight: FontWeight.w500));
    }

    return ElevatedButton(
      onPressed: enabled && !isLoading ? onPressed : null,
      style: ElevatedButton.styleFrom(
        backgroundColor: bgColor,
        foregroundColor: fgColor,
        disabledBackgroundColor: AppTheme.statusStopped.withValues(alpha: 0.3),
        disabledForegroundColor: AppTheme.textTertiary,
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(AppTheme.radiusSm),
        ),
      ),
      child: content,
    );
  }

  Color _getDefaultBackgroundColor() {
    switch (type) {
      case ButtonType.primary:
        return AppTheme.primaryColor;
      case ButtonType.success:
        return AppTheme.successColor;
      case ButtonType.warning:
        return AppTheme.warningColor;
      case ButtonType.danger:
        return AppTheme.errorColor;
      case ButtonType.secondary:
        return AppTheme.bgSurface;
    }
  }

  Color _getDefaultTextColor() {
    switch (type) {
      case ButtonType.secondary:
        return AppTheme.textPrimary;
      default:
        return AppTheme.textOnPrimary;
    }
  }
}

enum ButtonType { primary, success, warning, danger, secondary }
