import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.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/theme/colors.dart';
import 'package:smooth_corner/smooth_corner.dart';

class AppCustomChipWidget extends StatelessWidget {
  const AppCustomChipWidget(
      {super.key,
      this.labelText,
      this.textColor,
      this.backgroundColor,
      this.iconSize,
      this.icon = "",
      this.iconColor,
      this.richText,
      this.iconHasColor = true,
      this.shape,
      this.deleteIcon,
      this.deleteIconSize,
      this.deleteIconColor,
      this.deleteIconHasColor = false,
      this.padding = EdgeInsets.zero,
      this.isIconPNG = false,
      this.onChipTap,
      this.labelPadding,
      this.onDeleteTap,
      this.applyThemeColor = true,
      this.isEnglishOnly = false});

  final String? labelText;
  final Widget? richText;
  final Color? textColor;
  final Color? backgroundColor;
  final double? iconSize;
  final String icon;
  final String? deleteIcon;
  final Size? deleteIconSize;
  final Color? iconColor;
  final Color? deleteIconColor;
  final bool iconHasColor;
  final bool deleteIconHasColor;
  final OutlinedBorder? shape;
  final EdgeInsets? padding;
  final EdgeInsetsDirectional? labelPadding;
  final bool isIconPNG;
  final void Function()? onChipTap;
  final void Function()? onDeleteTap;
  final bool applyThemeColor;
  final bool isEnglishOnly;

  @override
  Widget build(BuildContext context) {
    final iconS = iconSize ?? 12.w;
    final resolvedTextColor = textColor ?? AppColors.textColor;
    final resolvedBackgroundColor = backgroundColor ?? AppColors.greyColor;
    final resolvedIconColor = (iconColor ?? AppColors.textColor);
    final resolvedDeleteIconColor = deleteIconColor ?? AppColors.textColor;
    return GestureDetector(
      onTap: onChipTap,
      child: SizedBox(
        child: ChipTheme(
          data: ChipThemeData(
            padding: EdgeInsets.zero,
            shape: SmoothRectangleBorder(
              side: BorderSide(
                width: 10.w,
                color: Colors.transparent, // Crucially, set color to transparent
                style: BorderStyle.none,
              ),
              borderRadius: BorderRadius.circular(8.r), // Apply a border radius of 16.0
            ),
          ),
          child: icon.isNotEmpty
              ? Chip(
                  avatar: icon.isNotEmpty
                      ? isIconPNG
                          ? Utils.buildImgWithAssets(icon: icon, width: iconS, height: iconS)
                          : Utils.buildSvgWithAssets(
                              icon: icon,
                              width: iconS,
                              height: iconS,
                              iconColor: iconHasColor ? resolvedIconColor : null,
                              fit: BoxFit.contain,
                              applyThemeColor: applyThemeColor,
                            )
                      : SizedBox.shrink(),
                  label:
                      richText ?? (labelText ?? "").toText10(isBold: true, letterSpacing: 0, color: resolvedTextColor, isEnglishOnly: isEnglishOnly),
                  padding: padding,
                  materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  labelPadding: labelPadding ?? EdgeInsetsDirectional.only(end: deleteIcon?.isNotEmpty == true ? 2.w : 8.w),
                  backgroundColor: resolvedBackgroundColor,
                  shape: shape ??
                      SmoothRectangleBorder(
                        borderRadius: BorderRadius.circular(8.r),
                        smoothness: 10,
                        side: BorderSide(color: AppColors.transparent, width: 1.5),
                      ),
                  deleteIcon: deleteIcon?.isNotEmpty == true
                      ? InkWell(
                          onTap: onDeleteTap,
                          child: Transform.flip(
                            flipX: getIt.get<AppState>().isArabic(),
                            child: Utils.buildSvgWithAssets(
                              icon: deleteIcon!,
                              width: iconS,
                              height: iconS,
                              iconColor: deleteIconHasColor ? resolvedDeleteIconColor : null,
                              applyThemeColor: applyThemeColor,
                            ),
                          ),
                        )
                      : null,
                  onDeleted: deleteIcon?.isNotEmpty == true ? () {} : null,
                )
              : Chip(
                  materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  label: richText ??
                      (labelText ?? "")
                          .toText10(isBold: true, letterSpacing: 0, color: resolvedTextColor, isCenter: true, isEnglishOnly: isEnglishOnly),
                  padding: EdgeInsets.zero,
                  backgroundColor: resolvedBackgroundColor,
                  shape: shape ??
                      SmoothRectangleBorder(
                        borderRadius: BorderRadius.circular(8.r),
                        smoothness: 10,
                        side: BorderSide(color: AppColors.transparent, width: 1.5),
                      ),
                  labelPadding: labelPadding ?? EdgeInsetsDirectional.only(start: 6.w, end: deleteIcon?.isNotEmpty == true ? 2.w : 8.w),
                  deleteIcon: deleteIcon?.isNotEmpty == true
                      ? InkWell(
                          onTap: onDeleteTap,
                          child: Utils.buildSvgWithAssets(
                              icon: deleteIcon!,
                              width: iconS,
                              height: iconS,
                              iconColor: deleteIconHasColor ? resolvedDeleteIconColor : null,
                              applyThemeColor: applyThemeColor))
                      : null,
                  onDeleted: deleteIcon?.isNotEmpty == true ? () {} : null,
                ),
        ),
      ),
    );
  }
}
