import 'package:flutter/material.dart';
import '../theme/app_colors.dart';
import '../theme/app_radius.dart';
import '../theme/app_gradients.dart';

enum FSButtonVariant { primary, secondary, ghost, danger, gradient, akinator }

class FSButton extends StatelessWidget {
  final String label;
  final VoidCallback? onPressed;
  final FSButtonVariant variant;
  final bool isLoading;
  final bool fullWidth;
  final IconData? icon;
  final double height;

  const FSButton({
    super.key,
    required this.label,
    this.onPressed,
    this.variant = FSButtonVariant.primary,
    this.isLoading = false,
    this.fullWidth = true,
    this.icon,
    this.height = 48,
  });

  @override
  Widget build(BuildContext context) {
    final isDisabled = onPressed == null || isLoading;

    if (variant == FSButtonVariant.gradient || variant == FSButtonVariant.akinator) {
      return _buildGradientButton(isDisabled);
    }

    return SizedBox(
      width: fullWidth ? double.infinity : null,
      height: height,
      child: _buildStandardButton(isDisabled),
    );
  }

  Widget _buildGradientButton(bool isDisabled) {
    final gradient = variant == FSButtonVariant.akinator
        ? AppGradients.akinatorButton
        : AppGradients.primaryButton;

    return SizedBox(
      width: fullWidth ? double.infinity : null,
      height: height,
      child: Opacity(
        opacity: isDisabled ? 0.5 : 1.0,
        child: DecoratedBox(
          decoration: BoxDecoration(
            gradient: gradient,
            borderRadius: AppRadius.smAll,
          ),
          child: Material(
            color: Colors.transparent,
            child: InkWell(
              onTap: isDisabled ? null : onPressed,
              borderRadius: AppRadius.smAll,
              child: Center(child: _buildChild(Colors.white)),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildStandardButton(bool isDisabled) {
    switch (variant) {
      case FSButtonVariant.primary:
        return ElevatedButton(
          onPressed: isDisabled ? null : onPressed,
          child: _buildChild(Colors.white),
        );
      case FSButtonVariant.secondary:
        return OutlinedButton(
          onPressed: isDisabled ? null : onPressed,
          child: _buildChild(AppColors.textPrimary),
        );
      case FSButtonVariant.ghost:
        return TextButton(
          onPressed: isDisabled ? null : onPressed,
          child: _buildChild(AppColors.textSecondary),
        );
      case FSButtonVariant.danger:
        return ElevatedButton(
          onPressed: isDisabled ? null : onPressed,
          style: ElevatedButton.styleFrom(backgroundColor: AppColors.error),
          child: _buildChild(Colors.white),
        );
      default:
        return ElevatedButton(
          onPressed: isDisabled ? null : onPressed,
          child: _buildChild(Colors.white),
        );
    }
  }

  Widget _buildChild(Color textColor) {
    if (isLoading) {
      return SizedBox(
        width: 20,
        height: 20,
        child: CircularProgressIndicator(
          strokeWidth: 2,
          valueColor: AlwaysStoppedAnimation(textColor),
        ),
      );
    }

    if (icon != null) {
      return Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(icon, size: 18, color: textColor),
          const SizedBox(width: 8),
          Text(label, style: TextStyle(color: textColor, fontWeight: FontWeight.w600, fontSize: 14)),
        ],
      );
    }

    return Text(label, style: TextStyle(color: textColor, fontWeight: FontWeight.w600, fontSize: 14));
  }
}