import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

import '../controls/image_control.dart';
import '../routes.dart';
import '../services/blocks_manager.dart';
import '../utils/active_profile_selector.dart';

class BlocksScreen extends StatelessWidget {
  const BlocksScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final manager =
        context.watch<ActiveProfileSelector<BlocksManager>>().activeEntry.value;
    final blocks = manager.getBlocks();

    return Scaffold(
      appBar: AppBar(
        title: const Text('Blocks'),
      ),
      body: SafeArea(
        child: ListView.builder(
          itemBuilder: (context, index) {
            final contact = blocks[index];
            return ListTile(
              onTap: () async {
                context.pushNamed(ScreenPaths.userProfile,
                    pathParameters: {'id': contact.id});
              },
              leading: ImageControl(
                imageUrl: contact.avatarUrl.toString(),
                iconOverride: const Icon(Icons.person),
                width: 32.0,
              ),
              title: Text(
                '${contact.name} (${contact.handle})',
                softWrap: true,
              ),
              subtitle: Text(
                'Last Status: ${contact.lastStatus?.toIso8601String() ?? "Unknown"}',
                softWrap: true,
              ),
              trailing: ElevatedButton(
                onPressed: () async => await manager.unblockConnection(contact),
                child: const Text('Unblock'),
              ),
            );
          },
          itemCount: blocks.length,
        ),
      ),
    );
  }
}
