import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
// import '../../../theme/app_colors.dart';
import '../../../providers/category_provider.dart';

/// 카테고리 선택 위젯
/// 구독 서비스의 카테고리를 선택할 수 있습니다.
class CategorySelector extends StatelessWidget {
  final List<dynamic> categories;
  final String? selectedCategoryId;
  final ValueChanged<String?> onChanged;
  final Color? baseColor;
  final List<Color>? gradientColors; // deprecated: ignored
  final bool isGlassmorphism; // deprecated: ignored

  const CategorySelector({
    super.key,
    required this.categories,
    required this.selectedCategoryId,
    required this.onChanged,
    this.baseColor,
    this.gradientColors,
    this.isGlassmorphism = false,
  });

  @override
  Widget build(BuildContext context) {
    return Wrap(
      spacing: 8,
      runSpacing: 8,
      children: categories.map((category) {
        final isSelected = selectedCategoryId == category.id;
        return InkWell(
          onTap: () => onChanged(category.id),
          borderRadius: BorderRadius.circular(12),
          child: Container(
            padding: const EdgeInsets.symmetric(
              horizontal: 16,
              vertical: 10,
            ),
            decoration: BoxDecoration(
              color: _getBackgroundColor(context, isSelected),
              borderRadius: BorderRadius.circular(12),
              border: _getBorder(context, isSelected),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Icon(
                  _getCategoryIcon(category),
                  size: 18,
                  color: _getTextColor(context, isSelected),
                ),
                const SizedBox(width: 6),
                Consumer<CategoryProvider>(
                  builder: (context, categoryProvider, child) {
                    return Text(
                      categoryProvider.getLocalizedCategoryName(
                          context, category.name),
                      style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.w500,
                        color: _getTextColor(context, isSelected),
                      ),
                    );
                  },
                ),
              ],
            ),
          ),
        );
      }).toList(),
    );
  }

  IconData _getCategoryIcon(dynamic category) {
    // 카테고리명에 따른 아이콘 반환
    switch (category.name) {
      case 'music':
        return Icons.music_note_rounded;
      case 'ottVideo':
        return Icons.movie_filter_rounded;
      case 'storageCloud':
        return Icons.cloud_outlined;
      case 'telecomInternetTv':
        return Icons.wifi_rounded;
      case 'lifestyle':
        return Icons.home_outlined;
      case 'shoppingEcommerce':
        return Icons.shopping_cart_outlined;
      case 'programming':
        return Icons.code_rounded;
      case 'collaborationOffice':
        return Icons.business_center_outlined;
      case 'aiService':
        return Icons.smart_toy_outlined;
      case 'other':
      default:
        return Icons.category_outlined;
    }
  }

  Color _getBackgroundColor(BuildContext context, bool isSelected) {
    final scheme = Theme.of(context).colorScheme;
    if (isSelected) {
      return baseColor ?? scheme.primary;
    }
    return scheme.surface;
  }

  Border? _getBorder(BuildContext context, bool isSelected) {
    final scheme = Theme.of(context).colorScheme;
    if (isSelected) return null;
    return Border.all(color: scheme.outline.withValues(alpha: 0.6), width: 1);
  }

  Color _getTextColor(BuildContext context, bool isSelected) {
    final scheme = Theme.of(context).colorScheme;
    return isSelected ? scheme.onPrimary : scheme.onSurface;
  }
}
