import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_assets.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/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:smooth_corner/smooth_corner.dart';

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

  final String? labelText;
  final Widget? richText;
  final Color? textColor;
  final Color? backgroundColor;
  final num 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 void Function()? onChipTap;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onChipTap,
      child: ChipTheme(
        data: ChipThemeData(
          padding: EdgeInsets.all(0.0),
          shape: SmoothRectangleBorder(
            side: BorderSide(
              width: 10.0,
              color: Colors.transparent, // Crucially, set color to transparent
              style: BorderStyle.none,
            ),
            borderRadius: BorderRadius.circular(8.h), // Apply a border radius of 16.0
          ),
        ),
        child: icon.isNotEmpty
            ? Chip(
                avatar: icon.isNotEmpty ? Utils.buildSvgWithAssets(icon: icon, width: iconSize.h, height: iconSize.h, iconColor: iconHasColor ? iconColor : null, fit: BoxFit.contain) : SizedBox.shrink(),
                label: richText ?? labelText!.toText10(weight: FontWeight.w500, letterSpacing: 0, color: textColor),
                padding: padding,
                materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                labelPadding: labelPadding ?? EdgeInsetsDirectional.only(start: 0.h, end: deleteIcon?.isNotEmpty == true ? 2.h : 8.h),
                backgroundColor: backgroundColor,
                shape: shape ??
                    SmoothRectangleBorder(
                      borderRadius: BorderRadius.circular(8 ?? 0),
                      smoothness: 10,
                      side: BorderSide(color: AppColors.transparent, width: 1.5),
                    ),
                deleteIcon: deleteIcon?.isNotEmpty == true
                    ? Utils.buildSvgWithAssets(icon: deleteIcon!, width: deleteIconSize!.width!.h, height: deleteIconSize!.height.h, iconColor: deleteIconHasColor ? deleteIconColor : null)
                    : null,
                onDeleted: deleteIcon?.isNotEmpty == true ? () {} : null,
              )
            : Chip(
                materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                label: richText ?? labelText!.toText10(weight: FontWeight.w500, letterSpacing: 0, color: textColor),
                padding: EdgeInsets.all(0.0),
                backgroundColor: backgroundColor,
                shape: shape ??
                    SmoothRectangleBorder(
                      borderRadius: BorderRadius.circular(8 ?? 0),
                      smoothness: 10,
                      side: BorderSide(color: AppColors.transparent, width: 1.5),
                    ),
                labelPadding: EdgeInsetsDirectional.only(start: 8.h, end: deleteIcon?.isNotEmpty == true ? -2.h : 8.h),
                deleteIcon: deleteIcon?.isNotEmpty == true
                    ? Utils.buildSvgWithAssets(icon: deleteIcon!, width: deleteIconSize!.width.h, height: deleteIconSize!.height.h, iconColor: deleteIconHasColor ? deleteIconColor : null)
                    : null,
                onDeleted: deleteIcon?.isNotEmpty == true ? () {} : null,
              ),
      ),
    );
  }
}
