import 'package:flutter/material.dart';
import 'package:frontend_eccp_mobile/app/core/constants/constants.dart';
import 'package:frontend_eccp_mobile/app/core/constants/enum/appbar_type_enum.dart';

class AppButton extends StatelessWidget {
  const AppButton({
    required this.label,
    required this.onPressed,
    super.key,
    this.type = AppButtonType.primary,
    this.width,
    this.height,
    this.isExpanded = false,
    this.radius = AppSizes.s8,
    this.borderWidth = 1.5,
    this.paddingVertical = 14,
    this.paddingHorizontal = 24,
    this.backgroundColor,
    this.borderColor,
    this.textColor,
    this.fontSize = 16,
    this.fontWeight = FontWeight.w600,
    this.isLoading = false,
    this.customContent,
  });

  final String label;
  final VoidCallback? onPressed;
  final AppButtonType type;

  final double? width;
  final double? height;
  final bool isExpanded;

  final double radius;
  final double borderWidth;
  final double paddingVertical;
  final double paddingHorizontal;

  final Color? backgroundColor;
  final Color? borderColor;
  final Color? textColor;

  final double fontSize;
  final FontWeight fontWeight;
  final bool isLoading;

  final Widget? customContent;

  bool get _isPrimary => type == AppButtonType.primary;

  @override
  Widget build(BuildContext context) {
    final bgColor = _isPrimary
        ? (backgroundColor ?? AppColors.primary900)
        : (backgroundColor ?? AppColors.neutralWhite);

    final brColor = _isPrimary
        ? Colors.transparent
        : (borderColor ?? AppColors.primary900);

    final txtColor = _isPrimary
        ? (textColor ?? Colors.white)
        : (textColor ?? AppColors.primary900);

    final defaultChild = Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        AnimatedContainer(
          duration: const Duration(milliseconds: 300),
          width: isLoading ? AppIconSizes.sm : 0,
          margin: EdgeInsets.only(
            right: isLoading ? AppSizes.s12 : 0,
          ),
          child: AnimatedOpacity(
            duration: const Duration(milliseconds: 200),
            opacity: isLoading ? 1 : 0,
            child: SizedBox(
              width: AppIconSizes.sm,
              height: AppIconSizes.sm,
              child: CircularProgressIndicator(
                strokeWidth: 2,
                valueColor: AlwaysStoppedAnimation(txtColor),
              ),
            ),
          ),
        ),
        Text(
          label,
          style: AppTextStyles.button.copyWith(
            fontSize: fontSize,
            fontWeight: fontWeight,
            color: txtColor,
          ),
        ),
      ],
    );

    final button = ElevatedButton(
      onPressed: isLoading ? null : onPressed,
      style: ElevatedButton.styleFrom(
        elevation: 0,
        backgroundColor: bgColor,
        padding: EdgeInsets.symmetric(
          vertical: paddingVertical,
          horizontal: paddingHorizontal,
        ),
        shape: RoundedRectangleBorder(
          side: BorderSide(
            color: brColor,
            width: borderWidth,
          ),
          borderRadius: BorderRadius.circular(radius),
        ),
      ),
      child: AnimatedSize(
        duration: const Duration(milliseconds: 300),
        child: customContent ?? defaultChild,
      ),
    );

    return SizedBox(
      width: isExpanded ? double.infinity : width,
      height: height,
      child: button,
    );
  }
}
