import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sizer/sizer.dart';

import '../../../core/app_export.dart';
import '../../../theme/app_theme.dart';

class FilterBottomSheet extends StatefulWidget {
  final Map<String, dynamic> currentFilters;
  final Function(Map<String, dynamic>) onFiltersChanged;

  const FilterBottomSheet({
    super.key,
    required this.currentFilters,
    required this.onFiltersChanged,
  });

  @override
  State<FilterBottomSheet> createState() => _FilterBottomSheetState();
}

class _FilterBottomSheetState extends State<FilterBottomSheet> {
  late Map<String, dynamic> _filters;

  final List<String> _categories = [
    'Todos',
    'Restaurantes',
    'Tiendas',
    'Servicios',
    'Entretenimiento',
    'Salud',
    'Belleza',
  ];

  final List<String> _sortOptions = [
    'Más recientes',
    'Por vencer',
    'Mayor descuento',
    'Distancia',
  ];

  @override
  void initState() {
    super.initState();
    _filters = Map<String, dynamic>.from(widget.currentFilters);
  }

  void _applyFilters() {
    HapticFeedback.lightImpact();
    widget.onFiltersChanged(_filters);
    Navigator.of(context).pop();
  }

  void _resetFilters() {
    HapticFeedback.lightImpact();
    setState(() {
      _filters = {
        'category': 'Todos',
        'sortBy': 'Más recientes',
        'showUsed': false,
        'showExpired': false,
      };
    });
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    return Container(
      decoration: BoxDecoration(
        color: theme.colorScheme.surface,
        borderRadius: const BorderRadius.only(
          topLeft: Radius.circular(20),
          topRight: Radius.circular(20),
        ),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // Handle bar
          Container(
            margin: EdgeInsets.only(top: 2.h),
            width: 12.w,
            height: 0.5.h,
            decoration: BoxDecoration(
              color: theme.colorScheme.outline,
              borderRadius: BorderRadius.circular(2),
            ),
          ),
          // Header
          Padding(
            padding: EdgeInsets.all(4.w),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  'Filtros',
                  style: theme.textTheme.titleLarge?.copyWith(
                    fontWeight: FontWeight.w600,
                  ),
                ),
                TextButton(
                  onPressed: _resetFilters,
                  child: Text(
                    'Limpiar',
                    style: theme.textTheme.bodyMedium?.copyWith(
                      color: theme.colorScheme.primary,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
              ],
            ),
          ),
          Divider(height: 1, color: theme.colorScheme.outline),
          // Content
          Flexible(
            child: SingleChildScrollView(
              padding: EdgeInsets.all(4.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // Category Filter
                  Text(
                    'Categoría',
                    style: theme.textTheme.titleMedium?.copyWith(
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                  SizedBox(height: 2.h),
                  Wrap(
                    spacing: 2.w,
                    runSpacing: 1.h,
                    children: _categories.map((category) {
                      final isSelected = _filters['category'] == category;
                      return FilterChip(
                        label: Text(category),
                        selected: isSelected,
                        onSelected: (selected) {
                          HapticFeedback.lightImpact();
                          setState(() {
                            _filters['category'] = category;
                          });
                        },
                        backgroundColor: theme.colorScheme.surface,
                        selectedColor:
                            AppTheme.accentYellow.withValues(alpha: 0.2),
                        checkmarkColor: AppTheme.primaryBlack,
                        labelStyle: theme.textTheme.bodyMedium?.copyWith(
                          color: isSelected
                              ? AppTheme.primaryBlack
                              : theme.colorScheme.onSurface,
                          fontWeight:
                              isSelected ? FontWeight.w600 : FontWeight.w400,
                        ),
                        side: BorderSide(
                          color: isSelected
                              ? AppTheme.accentYellow
                              : theme.colorScheme.outline,
                        ),
                      );
                    }).toList(),
                  ),
                  SizedBox(height: 3.h),
                  // Sort Options
                  Text(
                    'Ordenar por',
                    style: theme.textTheme.titleMedium?.copyWith(
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                  SizedBox(height: 2.h),
                  Column(
                    children: _sortOptions.map((option) {
                      final isSelected = _filters['sortBy'] == option;
                      return RadioListTile<String>(
                        title: Text(
                          option,
                          style: theme.textTheme.bodyMedium?.copyWith(
                            fontWeight:
                                isSelected ? FontWeight.w600 : FontWeight.w400,
                          ),
                        ),
                        value: option,
                        groupValue: _filters['sortBy'],
                        onChanged: (value) {
                          HapticFeedback.lightImpact();
                          setState(() {
                            _filters['sortBy'] = value;
                          });
                        },
                        activeColor: AppTheme.primaryBlack,
                        contentPadding: EdgeInsets.zero,
                      );
                    }).toList(),
                  ),
                  SizedBox(height: 2.h),
                  // Additional Options
                  Text(
                    'Mostrar',
                    style: theme.textTheme.titleMedium?.copyWith(
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                  SizedBox(height: 1.h),
                  SwitchListTile(
                    title: Text(
                      'Cupones usados',
                      style: theme.textTheme.bodyMedium,
                    ),
                    value: _filters['showUsed'] ?? false,
                    onChanged: (value) {
                      HapticFeedback.lightImpact();
                      setState(() {
                        _filters['showUsed'] = value;
                      });
                    },
                    activeColor: AppTheme.accentYellow,
                    activeTrackColor: AppTheme.primaryBlack,
                    contentPadding: EdgeInsets.zero,
                  ),
                  SwitchListTile(
                    title: Text(
                      'Cupones vencidos',
                      style: theme.textTheme.bodyMedium,
                    ),
                    value: _filters['showExpired'] ?? false,
                    onChanged: (value) {
                      HapticFeedback.lightImpact();
                      setState(() {
                        _filters['showExpired'] = value;
                      });
                    },
                    activeColor: AppTheme.accentYellow,
                    activeTrackColor: AppTheme.primaryBlack,
                    contentPadding: EdgeInsets.zero,
                  ),
                ],
              ),
            ),
          ),
          // Apply Button
          Padding(
            padding: EdgeInsets.all(4.w),
            child: SizedBox(
              width: double.infinity,
              height: 6.h,
              child: ElevatedButton(
                onPressed: _applyFilters,
                style: ElevatedButton.styleFrom(
                  backgroundColor: AppTheme.accentYellow,
                  foregroundColor: AppTheme.primaryBlack,
                  elevation: 0,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(12),
                  ),
                ),
                child: Text(
                  'Aplicar Filtros',
                  style: theme.textTheme.titleMedium?.copyWith(
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
            ),
          ),
          SizedBox(height: MediaQuery.of(context).padding.bottom),
        ],
      ),
    );
  }
}
