import 'package:flutter/material.dart';
import 'package:tstor_ui/api/client.dart';
import 'package:tstor_ui/api/torrent.graphql.dart';
import 'package:tstor_ui/components/download.dart';
import 'package:tstor_ui/components/sliver_header.dart';

class DownloadsScreen extends StatefulWidget {
  const DownloadsScreen({super.key});

  @override
  State<DownloadsScreen> createState() => _DownloadsScreenState();
}

class _DownloadsScreenState extends State<DownloadsScreen> {
  bool filterDownloading = false;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: FutureBuilder(
        key: GlobalKey(),
        future: client.query$ListTorrents(Options$Query$ListTorrents(
            // variables: Variables$Query$ListTorrents(downloading: filterDownloading),
            )),
        builder: (context, snapshot) {
          final torrents = snapshot.data?.parsedData?.torrentDaemon?.torrents;

          return NestedScrollView(
            floatHeaderSlivers: true,
            headerSliverBuilder: (context, innerBoxIsScrolled) => [
              HideableHeaderSliver(
                height: 80,
                body: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Wrap(
                    spacing: 8,
                    runSpacing: 8,
                    children: [
                      FilterChip(
                        label: const Text("Downloading"),
                        selected: filterDownloading,
                        onSelected: (value) => setState(() {
                          filterDownloading = value;
                        }),
                      ),
                    ],
                  ),
                ),
                actions: [
                  IconButton(
                    icon: const Icon(Icons.refresh),
                    onPressed: () => setState(() {}),
                  ),
                ],
              ),
            ],
            body: snapshot.hasData && torrents != null
                ? ListView.builder(
                    itemCount: torrents.length,
                    itemBuilder: (context, index) => TorrentTile(torrent: torrents[index]),
                  )
                : const Center(child: CircularProgressIndicator()),
          );
        },
      ),
    );
  }
}

class TorrentTile extends StatelessWidget {
  final Query$ListTorrents$torrentDaemon$torrents torrent;

  const TorrentTile({super.key, required this.torrent});

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Text(torrent.name),
      isThreeLine: true,
      subtitle: Column(
        children: [
          DownloadProgress(
            torrent.bytesCompleted,
            torrent.bytesCompleted + torrent.bytesMissing,
          ),
          Row(
            children: [
              Text("Peers: ${torrent.peers.length}"),
            ],
          ),
        ],
      ),
      trailing: Column(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          IconButton(
            onPressed: () => client.mutate$MarkTorrentDownload(
              Options$Mutation$MarkTorrentDownload(
                variables: Variables$Mutation$MarkTorrentDownload(
                  infohash: torrent.infohash,
                ),
              ),
            ),
            icon: const Icon(Icons.download),
          )
        ],
      ),
    );
  }
}