import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:f0ckapp/services/api_service.dart';
import 'package:f0ckapp/models/suggestion_model.dart';
import 'package:f0ckapp/providers/media_provider.dart';

class CustomSearchDelegate extends SearchDelegate<String> {
  Timer? _debounceTimer;
  List<Suggestion>? _suggestions;
  bool _isLoading = false;
  String? _error;
  String _lastFetchedQuery = "";

  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(
        icon: const Icon(Icons.clear),
        onPressed: () {
          query = '';
          _clearResults();
          showSuggestions(context);
        },
      ),
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      icon: const Icon(Icons.arrow_back),
      onPressed: () {
        _debounceTimer?.cancel();
        close(context, 'null');
      },
    );
  }

  @override
  Widget buildResults(BuildContext context) {
    return Center(child: Text('Suchergebnisse für: "$query"'));
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return StatefulBuilder(
      builder: (BuildContext context, void Function(void Function()) setState) {
        if (query.isEmpty) {
          _debounceTimer?.cancel();
          return Container(padding: const EdgeInsets.all(16.0), child: const Text(''));
        }

        if (query != _lastFetchedQuery) {
          _debounceTimer?.cancel();
          _isLoading = true;
          _error = null;
          _suggestions = null;

          _debounceTimer = Timer(Duration(milliseconds: 500), () async {
            try {
              final List<Suggestion> results = await fetchSuggestions(query);
              _lastFetchedQuery = query;
              setState(() {
                _suggestions = results;
                _isLoading = false;
              });
            } catch (e) {
              _lastFetchedQuery = query;
              setState(() {
                _error = e.toString();
                _suggestions = [];
                _isLoading = false;
              });
            }
          });

          return Center(child: _buildLoadingIndicator());
        }

        if (_isLoading) {
          return Center(child: _buildLoadingIndicator());
        }

        if (_error != null) {
          return Center(child: Text("Fehler: $_error"));
        }

        if (_suggestions == null || _suggestions!.isEmpty) {
          return Center(child: const Text("Keine Ergebnisse gefunden."));
        }

        return Consumer(
          builder: (BuildContext context, WidgetRef ref, Widget? child) {
            return ListView.builder(
              itemCount: _suggestions!.length,
              itemBuilder: (BuildContext context, int index) {
                final Suggestion suggestion = _suggestions![index];
                return ListTile(
                  title: Text(suggestion.tag),
                  subtitle: Text(
                    'Getaggt: ${suggestion.tagged}x • Score: ${suggestion.score.toStringAsFixed(2)}',
                    style: TextStyle(fontSize: 12),
                  ),
                  onTap: () {
                    ref.read(mediaProvider.notifier).setTag(suggestion.tag);
                    close(context, suggestion.tag);
                  },
                );
              },
            );
          },
        );
      },
    );
  }

  Widget _buildLoadingIndicator() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        const CircularProgressIndicator(strokeWidth: 3.0),
        const SizedBox(height: 12),
        const Text(
          'Vorschläge werden geladen...',
          style: TextStyle(fontStyle: FontStyle.italic),
        ),
      ],
    );
  }

  void _clearResults() {
    _debounceTimer?.cancel();
    _suggestions = null;
    _isLoading = false;
    _error = null;
    _lastFetchedQuery = "";
  }

  @override
  void close(BuildContext context, String result) {
    _debounceTimer?.cancel();
    super.close(context, result);
  }
}
