import 'package:flutter/material.dart';

/// Widget for taxi search filters including corregimiento dropdown, shift dropdown and search input
class TaxiFiltersWidget extends StatefulWidget {
  final List<String> corregimientos; // Changed from districts
  final List<String> shifts; // Added shifts list
  final String? selectedCorregimiento; // Changed from selectedDistrict
  final String? selectedShift; // Added shift selection
  final String searchText;
  final Function(String?)
  onCorregimientoChanged; // Changed from onDistrictChanged
  final Function(String?) onShiftChanged; // Added shift handler
  final Function(String) onSearchChanged;
  final VoidCallback onClearFilters;

  const TaxiFiltersWidget({
    super.key,
    required this.corregimientos, // Changed from districts
    required this.shifts, // Added shifts
    required this.selectedCorregimiento, // Changed from selectedDistrict
    required this.selectedShift, // Added shift selection
    required this.searchText,
    required this.onCorregimientoChanged, // Changed from onDistrictChanged
    required this.onShiftChanged, // Added shift handler
    required this.onSearchChanged,
    required this.onClearFilters,
  });

  @override
  State<TaxiFiltersWidget> createState() => _TaxiFiltersWidgetState();
}

class _TaxiFiltersWidgetState extends State<TaxiFiltersWidget> {
  late TextEditingController _searchController;

  @override
  void initState() {
    super.initState();
    _searchController = TextEditingController(text: widget.searchText);
  }

  @override
  void dispose() {
    _searchController.dispose();
    super.dispose();
  }

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

    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: theme.colorScheme.surface,
        boxShadow: [
          BoxShadow(
            color: theme.shadowColor.withAlpha(26),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        children: [
          // Filter Dropdowns Row
          Row(
            children: [
              // Corregimiento Dropdown
              Expanded(
                child: DropdownButtonFormField<String?>(
                  value:
                      widget
                          .selectedCorregimiento, // Changed from selectedDistrict
                  hint: const Text(
                    'Seleccionar Corregimiento',
                  ), // Updated hint text
                  decoration: InputDecoration(
                    labelText:
                        'Filter by Corregimiento', // Updated label as requested
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                    contentPadding: const EdgeInsets.symmetric(
                      horizontal: 12,
                      vertical: 8,
                    ),
                  ),
                  items: [
                    const DropdownMenuItem<String?>(
                      value: null,
                      child: Text('Todos los corregimientos'), // Updated text
                    ),
                    ...widget.corregimientos.map(
                      // Changed from districts
                      (corregimiento) => DropdownMenuItem<String?>(
                        value: corregimiento,
                        child: Text(corregimiento),
                      ),
                    ),
                  ],
                  onChanged:
                      widget
                          .onCorregimientoChanged, // Changed from onDistrictChanged
                ),
              ),
              const SizedBox(width: 12),
              // Shift Dropdown
              Expanded(
                child: DropdownButtonFormField<String?>(
                  value: widget.selectedShift,
                  hint: const Text('Seleccionar Turno'),
                  decoration: InputDecoration(
                    labelText: 'Filter by Shift', // Label as requested
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                    contentPadding: const EdgeInsets.symmetric(
                      horizontal: 12,
                      vertical: 8,
                    ),
                  ),
                  items: [
                    const DropdownMenuItem<String?>(
                      value: null,
                      child: Text('Todos los turnos'),
                    ),
                    ...widget.shifts.map(
                      (shift) => DropdownMenuItem<String?>(
                        value: shift,
                        child: Text(shift),
                      ),
                    ),
                  ],
                  onChanged: widget.onShiftChanged,
                ),
              ),
              if (widget.selectedCorregimiento !=
                      null || // Changed from selectedDistrict
                  widget.selectedShift != null || // Added shift condition
                  widget.searchText.isNotEmpty) ...[
                const SizedBox(width: 8),
                IconButton(
                  onPressed: widget.onClearFilters,
                  icon: const Icon(Icons.clear),
                  tooltip: 'Clear Filters', // Updated tooltip text as requested
                  style: IconButton.styleFrom(
                    backgroundColor: theme.colorScheme.errorContainer,
                    foregroundColor: theme.colorScheme.onErrorContainer,
                  ),
                ),
              ],
            ],
          ),
          const SizedBox(height: 12),
          // Search Input
          TextField(
            controller: _searchController,
            decoration: InputDecoration(
              labelText: 'Buscar taxi o teléfono',
              hintText: 'Escribe el nombre o número...',
              prefixIcon: const Icon(Icons.search),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
              ),
              contentPadding: const EdgeInsets.symmetric(
                horizontal: 12,
                vertical: 8,
              ),
              suffixIcon:
                  widget.searchText.isNotEmpty
                      ? IconButton(
                        onPressed: () {
                          _searchController.clear();
                          widget.onSearchChanged('');
                        },
                        icon: const Icon(Icons.clear),
                        tooltip: 'Limpiar búsqueda',
                      )
                      : null,
            ),
            onChanged: widget.onSearchChanged,
            textInputAction: TextInputAction.search,
          ),
        ],
      ),
    );
  }
}
