import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_export.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';

class CustomButton extends StatelessWidget {
  final String text;
  final String? icon;
  final VoidCallback? onPressed;
  final Color backgroundColor;
  final Color borderColor;
  final Color textColor;
  final double? borderRadius;
  final double borderWidth;
  final EdgeInsetsGeometry padding;
  final double? fontSize;
  final String? fontFamily;
  final FontWeight fontWeight;
  final bool isDisabled;
  final Color? iconColor;
  final double? height;
  final double? width;
  final double? iconSize;
  final TextOverflow? textOverflow;
  final BorderSide? borderSide;
  final bool applyThemeColor;
  final bool isBold;

  const CustomButton({
    super.key,
    required this.text,
    required this.onPressed,
    this.backgroundColor = const Color(0xFFED1C2B),
    this.borderColor = const Color(0xFFED1C2B),
    this.textColor = Colors.white,
    this.borderRadius,
    this.borderWidth = 2,
    this.padding = const EdgeInsets.fromLTRB(8, 10, 8, 10),
    this.fontSize,
    this.fontFamily,
    this.fontWeight = FontWeight.w700,
    this.isDisabled = false,
    this.isBold = false,
    this.icon,
    this.iconColor = Colors.white,
    this.height,
    this.width,
    this.iconSize,
    this.textOverflow,
    this.borderSide,
    this.applyThemeColor = true,
  });

  @override
  Widget build(BuildContext context) {
    final radius = borderRadius ?? (12.r);
    final iconS = iconSize ?? (24.h);
    final fontS = fontSize ?? (16.f);
    final isButtonDisabled = isDisabled || onPressed == null;

    return GestureDetector(
        onTap: isButtonDisabled ? null : onPressed,
        child: Container(
          height: height == null
              ? 56.h
              : (isFoldable || isTablet)
                  ? height! + 10.h
                  : height,
          width: width,
          padding: padding,
          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
              color: isButtonDisabled ? AppColors.inputLabelTextColor.withValues(alpha: 0.3) : backgroundColor,
              borderRadius: radius,
              customBorder: BorderRadius.circular(radius),
              side: borderSide ?? BorderSide(width: borderWidth.h, color: isButtonDisabled ? Colors.transparent : borderColor)),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              if (icon != null)
                Padding(
                  padding: text.isNotEmpty ? EdgeInsets.only(right: 4.w, left: 4.w) : EdgeInsets.zero,
                  child: Utils.buildSvgWithAssets(icon: icon!, iconColor: iconColor, isDisabled: isButtonDisabled, width: iconS, height: iconS, applyThemeColor: applyThemeColor),
                ),
              if (text.isNotEmpty && icon != null)
                Flexible(
                  child:
                  Text(
                    text,
                    overflow: textOverflow ?? TextOverflow.ellipsis,
                    maxLines: 1,
                    textAlign: TextAlign.center,
                    style: context.dynamicTextStyle(
                      fontSize: fontS,
                      color: isButtonDisabled ? AppColors.greyTextColor : textColor,
                      letterSpacing: 0,
                      fontWeight: isBold ? FontWeight.w700 : fontWeight,
                    ),
                  ),
                ),
              if (text.isNotEmpty && icon == null)
                Text(
                  text,
                  overflow: textOverflow ?? TextOverflow.ellipsis,
                  maxLines: 1,
                  textAlign: TextAlign.center,
                  style: context.dynamicTextStyle(
                    fontSize: fontS,
                    color: isButtonDisabled ? AppColors.greyTextColor : textColor,
                    letterSpacing: 0,
                    fontWeight: isBold ? FontWeight.w700 : fontWeight,
                  ),
                ),
            ],
          ),
        ));
  }
}
