import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:vbvs_app/common/color/appConstants.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';

import 'CustomCard.dart';

class SelectableTagButton extends StatelessWidget {
  final String label;
  final bool selected;
  final VoidCallback onTap;
  final double minWidth; // 最小宽度，单位：dp（会转 rpx）
  final double maxWidth; // 最大宽度，单位：dp（会转 rpx）

  ThemeController themeController = Get.find();

  SelectableTagButton({
    Key? key,
    required this.label,
    required this.selected,
    required this.onTap,
    this.minWidth = 132, // 默认最小宽度
    this.maxWidth = 500, // 默认最大宽度
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final double minWidthRpx = minWidth.rpx;
    final double maxWidthRpx = maxWidth.rpx;
    final double horizontalPadding = 28.rpx * 2; // 左右各 28，总 56
    // 估算文本宽度：每个字符按 14.rpx 字号估一个宽度
    final double estimatedTextWidth = label.length * 33.rpx;

    // 总宽度 = 文本宽度 + padding
    final double totalWidth = estimatedTextWidth + horizontalPadding;

    // 限制在 min 和 max 之间
    final double constrainedWidth = totalWidth.clamp(minWidthRpx, maxWidthRpx);

    return CustomCard(
      onTap: onTap,
      borderRadius: AppConstants().normal_container_radius,
      colors: selected
          ? [themeController.currentColor.sc1, themeController.currentColor.sc2]
          : [Colors.transparent],
      // colors: [Colors.transparent],
      enableGradient: true,
      child: Container(
        decoration: BoxDecoration(
          border: selected
              ? null
              : Border.all(
                  color: themeController.currentColor.sc4,
                  width: 0.5.rpx,
                ), // 未选中时无边框
          borderRadius: BorderRadius.circular(12.0), // 如果需要圆角
        ),
        padding: EdgeInsets.symmetric(horizontal: 28.rpx),
        constraints: BoxConstraints(
          minHeight: 61.rpx,
          maxWidth: constrainedWidth,
        ),
        alignment: Alignment.center,
        child: Text(
          label,
          overflow: TextOverflow.ellipsis,
          maxLines: 1,
          style: TextStyle(
            color: selected
                ? themeController.currentColor.sc3
                : themeController.currentColor.sc4,
            fontSize: AppConstants().normal_text_fontSize, // 字体也用 rpx 控制
          ),
        ),
      ),
    );
  }
}
