import 'package:flutter/material.dart';
import '../../theme/app_colors.dart';
import '../../models/element.dart';

/// 应用标签组件
class AppTag extends StatelessWidget {
  final String label;
  final ElementType? type;
  final VoidCallback? onDeleted;
  final Color? color;
  final bool closable;

  const AppTag({
    Key? key,
    required this.label,
    this.type,
    this.onDeleted,
    this.color,
    this.closable = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final tagColor = color ?? _getColorByType();

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: tagColor.withOpacity(0.1),
        border: Border.all(color: tagColor.withOpacity(0.3)),
        borderRadius: BorderRadius.circular(4),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            label,
            style: TextStyle(
              color: tagColor,
              fontSize: 12,
              fontWeight: FontWeight.w500,
            ),
          ),
          if (closable && onDeleted != null) ...[
            const SizedBox(width: 4),
            GestureDetector(
              onTap: onDeleted,
              child: Icon(Icons.close, size: 14, color: tagColor),
            ),
          ],
        ],
      ),
    );
  }

  Color _getColorByType() {
    if (type == null) return AppColors.textSecondary;

    switch (type!) {
      case ElementType.amount:
        return AppColors.amount;
      case ElementType.company:
        return AppColors.company;
      case ElementType.person:
        return AppColors.person;
      case ElementType.location:
        return AppColors.location;
      case ElementType.date:
        return AppColors.info;
      default:
        return AppColors.textSecondary;
    }
  }
}
