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

import '../controls/responsive_max_width.dart';
import '../controls/standard_appbar.dart';
import '../routes.dart';
import '../services/connections_manager.dart';
import '../utils/active_profile_selector.dart';

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

  @override
  Widget build(BuildContext context) {
    final manager = context
        .watch<ActiveProfileSelector<ConnectionsManager>>()
        .activeEntry
        .value;
    final circles = manager.getMyCircles();
    circles.sort((g1, g2) => g1.name.compareTo(g2.name));
    return Scaffold(
        appBar: StandardAppBar.build(
          context,
          'Circles Management',
          withHome: false,
          actions: [
            IconButton(
              onPressed: () => context.push(
                '${ScreenPaths.circleManagement}/new',
              ),
              icon: const Icon(Icons.add),
            ),
          ],
        ),
        body: Center(
          child: RefreshIndicator(
            onRefresh: () async {
              manager.refreshCircles();
            },
            child: ResponsiveMaxWidth(
              child: ListView.separated(
                physics: const AlwaysScrollableScrollPhysics(),
                itemBuilder: (context, index) {
                  final circle = circles[index];
                  return ListTile(
                    title: Text(circle.name),
                    onTap: () => context.push(
                        '${ScreenPaths.circleManagement}/show/${circle.id}'),
                  );
                },
                separatorBuilder: (_, __) => const Divider(),
                itemCount: circles.length,
              ),
            ),
          ),
        ));
  }
}
